3

I am trying to log my info and error logs into the log/dir directory in my project instead of writing it to the stderr.I tried the solution given here:How do I set the log directory of glog from code but this isn't creating log files in my directory.....

This is my code

func init() {
    myDir, _ := os.Getwd()
    flag.Set("logtostderr", "true")
    flag.Set("log_dir", myDir+"/log/dir")
    flag.Parse()
        //Some other stuff
}

needhelp
  • 31
  • 3

1 Answers1

1

I think you're trying to set the log directory for the glog package. so my answer will be based on this overview. From your code, it looks like you're setting the logtostderr flag to "true", which means the logs will be written to the standard error output (stderr) instead of files. If you want to write the logs to files, you should set this flag to "false".

Also, make sure that the directory you're specifying (myDir+"/log/dir") actually exists. If the directory doesn't exist, you may need to create it before running your code.

Here's an updated version of your code that sets the log directory and disables writing logs to stderr:

hadirezaei
  • 152
  • 5
  • import ( "flag" "os" "github.com/golang/glog" ) func init() { myDir, _ := os.Getwd() flag.Set("logtostderr", "false") flag.Set("log_dir", myDir+"/log/dir") flag.Parse() glog.Info("Logging initialized") // Some other stuff } – hadirezaei Sep 02 '23 at 15:47
  • thanks.It works now! – needhelp Sep 03 '23 at 03:20