zap.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package internal
  2. import (
  3. "fmt"
  4. "mtp20access/global"
  5. "time"
  6. "go.uber.org/zap"
  7. "go.uber.org/zap/zapcore"
  8. )
  9. var Zap = new(_zap)
  10. type _zap struct{}
  11. // GetEncoder 获取 zapcore.Encoder
  12. func (z *_zap) GetEncoder() zapcore.Encoder {
  13. if global.M2A_CONFIG.Zap.Format == "json" {
  14. return zapcore.NewJSONEncoder(z.GetEncoderConfig())
  15. }
  16. return zapcore.NewConsoleEncoder(z.GetEncoderConfig())
  17. }
  18. // GetEncoderConfig 获取zapcore.EncoderConfig
  19. func (z *_zap) GetEncoderConfig() zapcore.EncoderConfig {
  20. return zapcore.EncoderConfig{
  21. MessageKey: "message",
  22. LevelKey: "level",
  23. TimeKey: "time",
  24. NameKey: "logger",
  25. CallerKey: "caller",
  26. StacktraceKey: global.M2A_CONFIG.Zap.StacktraceKey,
  27. LineEnding: zapcore.DefaultLineEnding,
  28. EncodeLevel: global.M2A_CONFIG.Zap.ZapEncodeLevel(),
  29. EncodeTime: z.CustomTimeEncoder,
  30. EncodeDuration: zapcore.SecondsDurationEncoder,
  31. EncodeCaller: zapcore.FullCallerEncoder,
  32. }
  33. }
  34. // GetEncoderCore 获取Encoder的 zapcore.Core
  35. func (z *_zap) GetEncoderCore(l zapcore.Level, level zap.LevelEnablerFunc) zapcore.Core {
  36. writer, err := FileRotatelogs.GetWriteSyncer(l.String()) // 使用file-rotatelogs进行日志分割
  37. if err != nil {
  38. fmt.Printf("Get Write Syncer Failed err:%v", err.Error())
  39. return nil
  40. }
  41. return zapcore.NewCore(z.GetEncoder(), writer, level)
  42. }
  43. // CustomTimeEncoder 自定义日志输出时间格式
  44. func (z *_zap) CustomTimeEncoder(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {
  45. encoder.AppendString(t.Format(global.M2A_CONFIG.Zap.Prefix + "2006/01/02 - 15:04:05.000"))
  46. }
  47. // GetZapCores 根据配置文件的Level获取 []zapcore.Core
  48. func (z *_zap) GetZapCores() []zapcore.Core {
  49. cores := make([]zapcore.Core, 0, 7)
  50. for level := global.M2A_CONFIG.Zap.TransportLevel(); level <= zapcore.FatalLevel; level++ {
  51. cores = append(cores, z.GetEncoderCore(level, z.GetLevelPriority(level)))
  52. }
  53. return cores
  54. }
  55. // GetLevelPriority 根据 zapcore.Level 获取 zap.LevelEnablerFunc
  56. func (z *_zap) GetLevelPriority(level zapcore.Level) zap.LevelEnablerFunc {
  57. switch level {
  58. case zapcore.DebugLevel:
  59. return func(level zapcore.Level) bool { // 调试级别
  60. return level == zap.DebugLevel
  61. }
  62. case zapcore.InfoLevel:
  63. return func(level zapcore.Level) bool { // 日志级别
  64. return level == zap.InfoLevel
  65. }
  66. case zapcore.WarnLevel:
  67. return func(level zapcore.Level) bool { // 警告级别
  68. return level == zap.WarnLevel
  69. }
  70. case zapcore.ErrorLevel:
  71. return func(level zapcore.Level) bool { // 错误级别
  72. return level == zap.ErrorLevel
  73. }
  74. case zapcore.DPanicLevel:
  75. return func(level zapcore.Level) bool { // dpanic级别
  76. return level == zap.DPanicLevel
  77. }
  78. case zapcore.PanicLevel:
  79. return func(level zapcore.Level) bool { // panic级别
  80. return level == zap.PanicLevel
  81. }
  82. case zapcore.FatalLevel:
  83. return func(level zapcore.Level) bool { // 终止级别
  84. return level == zap.FatalLevel
  85. }
  86. default:
  87. return func(level zapcore.Level) bool { // 调试级别
  88. return level == zap.DebugLevel
  89. }
  90. }
  91. }