Log Files

By default, log file names are named with the current date and time, formatted as YYYY-MM-DD.log. We can use the SetFile method to set the format of the file name, and the file name format supports Time time format. Simple example:

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gfile"
  6. "github.com/gogf/gf/v2/os/glog"
  7. )
  8. // Set log level
  9. func main() {
  10. ctx := context.TODO()
  11. path := "./glog"
  12. glog.SetPath(path)
  13. glog.SetStdoutPrint(false)
  14. // Use the default file name format
  15. glog.Print(ctx, "Standard file name format, using the current date and time")
  16. // Set file name format via SetFile
  17. glog.SetFile("stdout.log")
  18. glog.Print(ctx, "Set the log output file name to a single file")
  19. // Chained operation to set file name format
  20. glog.File("stderr.log").Print(ctx, "Supports chained operations")
  21. glog.File("error-{Ymd}.log").Print(ctx, "File name supports gtime date format")
  22. glog.File("access-{Ymd}.log").Print(ctx, "File name supports gtime date format")
  23. list, err := gfile.ScanDir(path, "*")
  24. g.Dump(err)
  25. g.Dump(list)
  26. }

After execution, the output is:

  1. <nil>
  2. [
  3. "C:\hailaz\test\glog\2021-12-31.log",
  4. "C:\hailaz\test\glog\access-20211231.log",
  5. "C:\hailaz\test\glog\error-20211231.log",
  6. "C:\hailaz\test\glog\stderr.log",
  7. "C:\hailaz\test\glog\stdout.log",
  8. ]

As you can see, if you need to use the gtime time format in the file name format, you need to include the format content in {xxx}. This example also uses the feature of chained operations. For details, see the subsequent explanation.

Log Directory

By default, glog will output log content to the standard output. We can set the directory path for log output through the SetPath method, so that log content will be written to the log file. Thanks to the internal use of the gfpool file pointer pool, the efficiency of file writing is quite excellent. Simple example:

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gfile"
  6. "github.com/gogf/gf/v2/os/glog"
  7. )
  8. // Set log level
  9. func main() {
  10. ctx := context.TODO()
  11. path := "./glog"
  12. glog.SetPath(path)
  13. glog.Print(ctx, "Log content")
  14. list, err := gfile.ScanDir(path, "*")
  15. g.Dump(err)
  16. g.Dump(list)
  17. }

After execution, the output is:

  1. 2021-12-31 11:32:16.742 Log content
  2. <nil>
  3. [
  4. "C:\hailaz\test\glog\2021-12-31.log",
  5. ]

When setting the output directory of the log through SetPath, if the directory does not exist, it will recursively create the directory path. As you can see, after executing Println, the log directory glog was created under /tmp, and a log file was generated under it. Also, we can see that the log content is output not only to the file but by default also to the terminal. We can disable terminal log output using the SetStdoutPrint(false) method, so the log content will only be output to the log file.