zap.go 3.4 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, appname, level string, maxSize, maxBackups, maxAge int, disableStacktrace bool) *zap.Logger {
  21. encoding := "json"
  22. development := true
  23. if runmode == "prod" {
  24. level = "info"
  25. development = false
  26. }
  27. js := fmt.Sprintf(`{
  28. "level": "%s",
  29. "encoding": "%s",
  30. "outputPath": "%s"
  31. }`, level, encoding, logPath)
  32. var zcfg ZapConfig
  33. if err := json.Unmarshal([]byte(js), &zcfg); err != nil {
  34. panic(err)
  35. }
  36. zcfg.Development = development
  37. zcfg.DisableStacktrace = disableStacktrace
  38. return zcfg.New(appname, maxSize, maxBackups, maxAge)
  39. }
  40. func NewInfoLogger(runmode, logPath string, appname, level string, maxSize, maxBackups, maxAge int) *zap.Logger {
  41. encoding := "json"
  42. development := true
  43. if runmode == "prod" {
  44. development = false
  45. }
  46. js := fmt.Sprintf(`{
  47. "level": "%s",
  48. "encoding": "%s",
  49. "outputPath": "%s"
  50. }`, level, encoding, logPath)
  51. var zcfg ZapConfig
  52. if err := json.Unmarshal([]byte(js), &zcfg); err != nil {
  53. panic(err)
  54. }
  55. zcfg.Development = development
  56. zcfg.DisableStacktrace = true
  57. return zcfg.New(appname, maxSize, maxBackups, maxAge)
  58. }
  59. func (zc *ZapConfig) New(appname string, maxSize, maxBackups, maxAge int) *zap.Logger {
  60. // if nil, default the values
  61. if maxSize == 0 {
  62. maxSize = 100
  63. }
  64. if maxBackups == 0 {
  65. maxBackups = 7
  66. }
  67. if maxAge == 0 {
  68. maxAge = 30
  69. }
  70. hook := lumberjack.Logger{
  71. Filename: zc.OutputPath,
  72. MaxSize: maxSize, // megabytes
  73. MaxBackups: maxBackups, // backups number
  74. MaxAge: maxAge, // days
  75. Compress: true, // disabled by default
  76. }
  77. fileWriter := zapcore.AddSync(&hook)
  78. var EncoderConfig zapcore.EncoderConfig
  79. if zc.Development {
  80. EncoderConfig = zap.NewDevelopmentEncoderConfig()
  81. } else {
  82. EncoderConfig = zap.NewProductionEncoderConfig()
  83. }
  84. // 自定义
  85. EncoderConfig.EncodeTime = CustomTimeEncoder
  86. var encoder zapcore.Encoder
  87. if strings.ToLower(zc.Encoding) == "console" {
  88. encoder = zapcore.NewConsoleEncoder(EncoderConfig)
  89. } else {
  90. encoder = zapcore.NewJSONEncoder(EncoderConfig)
  91. }
  92. core := zapcore.NewCore(encoder, fileWriter, zc.Level)
  93. opts := zc.buildOptions(fileWriter)
  94. // 设置初始化字段
  95. opts = append(opts, zap.Fields(zap.String("appname", appname)))
  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. }