Error log information supports the Stack feature, which can automatically print the stack information of the current invocation of log component methods. This stack information can be triggered by error log output methods like Notice*/Warning*/Error*/Critical*/Panic*/Fatal*, or obtained/printed via GetStack/PrintStack. The stack information of error messages is quite useful for debugging.

Example 1, Triggered by Error Method

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/os/glog"
  5. )
  6. func Test(ctx context.Context) {
  7. glog.Error(ctx, "This is error!")
  8. }
  9. func main() {
  10. ctx := context.TODO()
  11. Test(ctx)
  12. }

The printed result is as follows:

  1. 2022-01-05 15:08:54.998 [ERRO] This is error!
  2. Stack:
  3. 1. main.Test
  4. C:/hailaz/test/main.go:10
  5. 2. main.main
  6. C:/hailaz/test/main.go:15

Example 2, Print via Stack Method

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/glog"
  6. )
  7. func main() {
  8. ctx := context.TODO()
  9. glog.PrintStack(ctx)
  10. glog.New().PrintStack(ctx)
  11. fmt.Println(glog.GetStack())
  12. fmt.Println(glog.New().GetStack())
  13. }

After execution, the output is:

  1. 2019-07-12 22:20:28.070 Stack:
  2. 1. main.main
  3. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:11
  4. 2019-07-12 22:20:28.070 Stack:
  5. 1. main.main
  6. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:12
  7. 1. main.main
  8. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:14
  9. 1. main.main
  10. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_stack.go:15

Example 3, Print gerror.Error

The glog logging module supports stack printing for both standard errors and gerror errors.

  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. "github.com/gogf/gf/v2/os/glog"
  7. )
  8. func MakeError() error {
  9. return errors.New("connection closed with normal error")
  10. }
  11. func MakeGError() error {
  12. return gerror.New("connection closed with gerror")
  13. }
  14. func TestGError(ctx context.Context) {
  15. err1 := MakeError()
  16. err2 := MakeGError()
  17. glog.Error(ctx, err1)
  18. glog.Errorf(ctx, "%+v", err2)
  19. }
  20. func main() {
  21. ctx := context.TODO()
  22. TestGError(ctx)
  23. }

After execution, the terminal output is:

  1. 2019-07-12 22:23:11.467 [ERRO] connection closed with normal error
  2. Stack:
  3. 1. main.TestGError
  4. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:20
  5. 2. main.main
  6. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:25
  7. 2019-07-12 22:23:11.467 [ERRO] connection closed with gerror
  8. 1. connection closed with gerror
  9. 1). main.MakeGError
  10. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:14
  11. 2). main.TestGError
  12. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:19
  13. 3). main.main
  14. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:25
  15. Stack:
  16. 1. main.TestGError
  17. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:21
  18. 2. main.main
  19. /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_gerror.go:25