错误日志信息支持 Stack 特性,该特性可以自动打印出当前调用日志组件方法的堆栈信息,该堆栈信息可以通过 Notice*/Warning*/Error*/Critical*/Panic*/Fatal* 等错误日志输出方法触发,也可以通过 GetStack/PrintStack 获取/打印。错误信息的 stack 信息对于调试来说相当有用。

示例1,通过 Error 方法触发

  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. }

打印出的结果如下:

  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

示例2,通过 Stack 方法打印

  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. }

执行后,输出结果为:

  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

示例3,打印 gerror.Error

glog 日志模块支持对标准错误以及 gerror 错误的堆栈打印支持。

  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. }

执行后,终端输出:

  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