zap.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package logger
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. "gopkg.in/natefinch/lumberjack.v2"
  10. )
  11. type ZapConfig struct {
  12. Level zap.AtomicLevel `json:"level" yaml:"level"`
  13. Development bool `json:"development" yaml:"development"`
  14. DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
  15. DisableStacktrace bool `json:"disableStacktrace" yaml:"disableStacktrace"`
  16. Encoding string `json:"encoding" yaml:"encoding"`
  17. OutputPath string `json:"outputPath" yaml:"outputPath"`
  18. }
  19. // init log
  20. func InitLogger(runmode, logPath string, maxSize, maxBackups, maxAge int, disableStacktrace bool) *zap.Logger {
  21. level := "debug"
  22. encoding := "json"
  23. development := true
  24. if runmode == "prod" {
  25. level = "info"
  26. development = false
  27. }
  28. js := fmt.Sprintf(`{
  29. "level": "%s",
  30. "encoding": "%s",
  31. "outputPath": "%s"
  32. }`, level, encoding, logPath)
  33. var zcfg ZapConfig
  34. if err := json.Unmarshal([]byte(js), &zcfg); err != nil {
  35. panic(err)
  36. }
  37. zcfg.Development = development
  38. zcfg.DisableStacktrace = disableStacktrace
  39. return zcfg.New(maxSize, maxBackups, maxAge)
  40. }
  41. func NewInfoLogger(runmode, logPath string, maxSize, maxBackups, maxAge int) *zap.Logger {
  42. level := "info"
  43. encoding := "json"
  44. development := true
  45. if runmode == "prod" {
  46. development = false
  47. }
  48. js := fmt.Sprintf(`{
  49. "level": "%s",
  50. "encoding": "%s",
  51. "outputPath": "%s"
  52. }`, level, encoding, logPath)
  53. var zcfg ZapConfig
  54. if err := json.Unmarshal([]byte(js), &zcfg); err != nil {
  55. panic(err)
  56. }
  57. zcfg.Development = development
  58. zcfg.DisableStacktrace = true
  59. return zcfg.New(maxSize, maxBackups, maxAge)
  60. }
  61. func (zc *ZapConfig) New(maxSize, maxBackups, maxAge int) *zap.Logger {
  62. // if nil, default the values
  63. if maxSize == 0 {
  64. maxSize = 100
  65. }
  66. if maxBackups == 0 {
  67. maxBackups = 7
  68. }
  69. if maxAge == 0 {
  70. maxAge = 30
  71. }
  72. hook := lumberjack.Logger{
  73. Filename: zc.OutputPath,
  74. MaxSize: maxSize, // megabytes
  75. MaxBackups: maxBackups, // backups number
  76. MaxAge: maxAge, // days
  77. Compress: true, // disabled by default
  78. }
  79. fileWriter := zapcore.AddSync(&hook)
  80. var EncoderConfig zapcore.EncoderConfig
  81. if zc.Development {
  82. EncoderConfig = zap.NewDevelopmentEncoderConfig()
  83. } else {
  84. EncoderConfig = zap.NewProductionEncoderConfig()
  85. }
  86. // 自定义
  87. EncoderConfig.EncodeTime = CustomTimeEncoder
  88. var encoder zapcore.Encoder
  89. if strings.ToLower(zc.Encoding) == "console" {
  90. encoder = zapcore.NewConsoleEncoder(EncoderConfig)
  91. } else {
  92. encoder = zapcore.NewJSONEncoder(EncoderConfig)
  93. }
  94. core := zapcore.NewCore(encoder, fileWriter, zc.Level)
  95. opts := zc.buildOptions(fileWriter)
  96. return zap.New(core, opts...)
  97. }
  98. func (zc *ZapConfig) buildOptions(fileWriter zapcore.WriteSyncer) []zap.Option {
  99. opts := []zap.Option{zap.ErrorOutput(fileWriter)}
  100. if !zc.DisableCaller {
  101. opts = append(opts, zap.AddCaller())
  102. }
  103. if !zc.DisableStacktrace {
  104. stackLevel := zap.ErrorLevel
  105. if zc.Development {
  106. stackLevel = zap.WarnLevel
  107. }
  108. opts = append(opts, zap.AddStacktrace(stackLevel))
  109. }
  110. return opts
  111. }
  112. // 自定义时间格式
  113. func CustomTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  114. enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
  115. }