For a complete method list, refer to the API documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/glog

The glog module supports a very convenient chaining operation method, with the main chaining methods as follows:

  1. // Redirect log output interface
  2. func To(writer io.Writer) *Logger
  3. // Log content output to directory
  4. func Path(path string) *Logger
  5. // Set log file category
  6. func Cat(category string) *Logger
  7. // Set log file format
  8. func File(file string) *Logger
  9. // Set log print level
  10. func Level(level int) *Logger
  11. // Set log print level (string)
  12. func LevelStr(levelStr string) *Logger
  13. // Set file backtrack value
  14. func Skip(skip int) *Logger
  15. // Enable trace printing
  16. func Stack(enabled bool) *Logger
  17. // Enable trace printing with filter string
  18. func StackWithFilter(filter string) *Logger
  19. // Enable terminal output
  20. func Stdout(enabled...bool) *Logger
  21. // Enable log header information
  22. func Header(enabled...bool) *Logger
  23. // Output log line number information
  24. func Line(long...bool) *Logger
  25. // Asynchronous log output
  26. func Async(enabled...bool) *Logger

Example 1, Basic Usage

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gfile"
  6. )
  7. func main() {
  8. ctx := context.TODO()
  9. path := "/tmp/glog-cat"
  10. g.Log().SetPath(path)
  11. g.Log().Stdout(false).Cat("cat1").Cat("cat2").Print(ctx, "test")
  12. list, err := gfile.ScanDir(path, "*", true)
  13. g.Dump(err)
  14. g.Dump(list)
  15. }

After execution, the output is:

  1. [
  2. "/tmp/glog-cat/cat1",
  3. "/tmp/glog-cat/cat1/cat2",
  4. "/tmp/glog-cat/cat1/cat2/2018-10-10.log",
  5. ]

Example 2, Print Call Line Number

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func main() {
  7. ctx := context.TODO()
  8. g.Log().Line().Print(ctx, "this is the short file name with its line number")
  9. g.Log().Line(true).Print(ctx, "lone file name with line number")
  10. }

After execution, the terminal output is:

  1. 2019-05-23 09:22:58.141 glog_line.go:8: this is the short file name with its line number
  2. 2019-05-23 09:22:58.142 /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/os/glog/glog_line.go:9: lone file name with line number

Example 3, File Backtrack Skip

Sometimes we encapsulate the glog module using some modules to print logs, such as encapsulating a logger package to print logs via logger.Print. In this case, the printed call line number is always the same location because, for glog, its caller is always the logger.Print method. At this time, we can set the backtrack value to skip the backtracked file count, using SetStackSkip or the chaining method Skip.

The setting of the file backtrack value also affects the Stack call backtrack print result.

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func PrintLog(ctx context.Context, content string) {
  7. g.Log().Skip(1).Line().Print(ctx, "line number with skip:", content)
  8. g.Log().Line().Print(ctx, "line number without skip:", content)
  9. }
  10. func main() {
  11. ctx := context.TODO()
  12. PrintLog(ctx, "just test")
  13. }

After execution, the terminal output is:

  1. 2019-05-23 19:30:10.984 glog_line2.go:13: line number with skip: just test
  2. 2019-05-23 19:30:10.984 glog_line2.go:9: line number without skip: just test