基于 `zap` 包封装。除了实现 `Go` 日志包的基本功能外,还实现了很多高级功能 本包基于`github.com/tkestack/tke/pkg/util/log`裁剪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.7 KiB

2 years ago
  1. # pkg/util/log
  2. 本包基于`github.com/tkestack/tke/pkg/util/log`开发
  3. `pkg/util/log` 是一个生产可用的日志包,基于 `zap` 包封装。除了实现 `Go` 日志包的基本功能外,还实现了很多高级功能,`pkg/util/log`具有如下特性:
  4. - 支持日志级别:`Debug`、`Info`、`Warn`、`Error`、`Panic`、`Fatal`。
  5. - 支持自定义配置。
  6. - 支持文件名和行号。
  7. - 支持输出掉标准输出和文件,可以同时输出到多个地方。
  8. - 支持 `JSON``Text` 两种日志格式。
  9. - 支持颜色输出。
  10. - 高性能。
  11. - 支持结构化日志记录。
  12. - **兼容标准库 `log` 包**。
  13. - **支持Context(业务定制)**
  14. ## 使用方法
  15. ### 一个简单的示例
  16. 创建一个 `example2.go` 文件,内容如下:
  17. ```go
  18. package main
  19. func main() {
  20. defer log.Flush()
  21. // Debug、Info(with field)、Warnf、Errorw使用
  22. log.Debug("This is a debug message")
  23. log.Info("This is a info message", log.Int32("int_key", 10))
  24. log.Warnf("This is a formatted %s message", "warn")
  25. }
  26. ```
  27. 执行代码:
  28. ```bash
  29. $ go run example2.go
  30. 2020-12-05 07:56:37.154 info example/example2.go:12 This is a info message {"int_key": 10}
  31. 2020-12-05 07:56:37.154 warn example/example2.go:13 This is a formatted warn message
  32. ```
  33. 上述代码使用 `pkg/util/log` 包默认的全局 `logger`,分别在 `Debug` 、`Info` 和 `Warn` 级别打印了一条日志。
  34. ### 初始化日志包
  35. 可以使用 `Init` 来初始化一个日志包,如下:
  36. ```go
  37. // logger配置
  38. opts := &log.Options{
  39. Level: "debug",
  40. Format: "console",
  41. EnableColor: true,
  42. EnableCaller: true,
  43. OutputPaths: []string{"test.log", "stdout"},
  44. ErrorOutputPaths: []string{"error.log"},
  45. }
  46. // 初始化全局logger
  47. log.Init(opts)
  48. ```
  49. Format 支持 `console``json` 2 种格式:
  50. - console:输出为 text 格式。例如:`2020-12-05 08:12:02.324 DEBUG example/example.go:43 This is a debug message`
  51. - json:输出为 json 格式,例如:`{"level":"debug","time":"2020-12-05 08:12:54.113","caller":"example/example.go:43","msg":"This is a debug message"}`
  52. OutputPaths,可以设置日志输出:
  53. - stdout:输出到标准输出。
  54. - stderr:输出到标准错误输出。
  55. - /var/log/test.log:输出到文件。
  56. 支持同时输出到多个输出。
  57. EnableColor 为 `true` 开启颜色输出,为 `false` 关闭颜色输出。
  58. ### 结构化日志输出
  59. `pkg/util/log` 也支持结构化日志打印,例如:
  60. ```go
  61. log.Info("This is a info message", log.Int32("int_key", 10))
  62. log.Infow("Message printed with Errorw", "X-Request-ID", "fbf54504-64da-4088-9b86-67824a7fb508")
  63. ```
  64. 对应的输出结果为:
  65. ```
  66. 2020-12-05 08:16:18.749 INFO example/example.go:44 This is a info message {"int_key": 10}
  67. 2020-12-05 08:16:18.749 ERROR example/example.go:46 Message printed with Errorw {"X-Request-ID": "fbf54504-64da-4088-9b86-67824a7fb508"}
  68. ```
  69. log.Info 这类函数需要指定具体的类型,以最大化的 提高日志的性能。log.Infow 这类函数,不用指定具体的类型,底层使用了反射,性能会差些。建议用在低频调用的函数中。
  70. ## 支持V level
  71. 创建 `v_level.go`,内容如下:
  72. ```go
  73. package main
  74. func main() {
  75. defer log.Flush()
  76. log.V(0).Info("This is a V level message")
  77. log.V(0).Infow("This is a V level message with fields", "X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
  78. }
  79. ```
  80. 执行如上代码:
  81. ```bash
  82. $ go run v_level.go
  83. 2020-12-05 08:20:37.763 info example/v_level.go:10 This is a V level message
  84. 2020-12-05 08:20:37.763 info example/v_level.go:11 This is a V level message with fields {"X-Request-ID": "7a7b9f24-4cae-4b2a-9464-69088b45b904"}
  85. ```
  86. ## 完整的示例
  87. 一个完整的示例请参考[example.go](./example/example.go)。