github.com/wfusion/gofusion@v1.1.14/log/customlogger/mq.go (about) 1 package customlogger 2 3 import ( 4 "context" 5 "reflect" 6 "strings" 7 8 "github.com/spf13/cast" 9 10 "github.com/wfusion/gofusion/common/infra/watermill" 11 "github.com/wfusion/gofusion/common/utils" 12 "github.com/wfusion/gofusion/config" 13 "github.com/wfusion/gofusion/log" 14 ) 15 16 var ( 17 // MQLogger FIXME: should not be deleted to avoid compiler optimized 18 MQLogger = reflect.TypeOf(mqLogger{}) 19 mqFields = log.Fields{"component": strings.ToLower(config.ComponentMessageQueue)} 20 ) 21 22 // mqLogger implements watermill.LoggerAdapter with *zap.Logger. 23 type mqLogger struct { 24 log log.Loggable 25 appName string 26 confName string 27 enabled bool 28 fields watermill.LogFields 29 } 30 31 // NewLogger returns new watermill.LoggerAdapter using passed *zap.Logger as backend. 32 func NewLogger() watermill.LoggerAdapter { 33 return new(mqLogger) 34 } 35 36 func (m *mqLogger) Init(log log.Loggable, appName, name string) { 37 m.log = log 38 m.appName = appName 39 m.confName = name 40 m.reloadConfig() 41 } 42 43 // Error writes error log with message, error and some fields. 44 func (m *mqLogger) Error(msg string, err error, fields watermill.LogFields) { 45 if !m.isLoggable() { 46 return 47 } 48 ctx, fs := m.parseLogFields(fields) 49 if err != nil { 50 m.logger().Error(ctx, msg+": %s", err, fs) 51 } else { 52 m.logger().Error(ctx, msg, err, fs) 53 } 54 } 55 56 // Info writes info log with message and some fields. 57 func (m *mqLogger) Info(msg string, fields watermill.LogFields) { 58 if !m.isLoggable() { 59 return 60 } 61 ctx, fs := m.parseLogFields(fields) 62 m.logger().Info(ctx, msg, fs) 63 } 64 65 // Debug writes debug log with message and some fields. 66 func (m *mqLogger) Debug(msg string, fields watermill.LogFields) { 67 if !m.isLoggable() { 68 return 69 } 70 ctx, fs := m.parseLogFields(fields) 71 m.logger().Debug(ctx, msg, fs) 72 } 73 74 // Trace writes debug log instead of trace log because zap does not support trace level logging. 75 func (m *mqLogger) Trace(msg string, fields watermill.LogFields) { 76 if !m.isLoggable() { 77 return 78 } 79 ctx, fs := m.parseLogFields(fields) 80 m.logger().Debug(ctx, msg, fs) 81 } 82 83 // With returns new LoggerAdapter with passed fields. 84 func (m *mqLogger) With(fields watermill.LogFields) watermill.LoggerAdapter { 85 return &mqLogger{fields: m.fields.Add(fields)} 86 } 87 88 func (m *mqLogger) logger() log.Loggable { 89 if m.log != nil { 90 return m.log 91 } 92 return log.Use(config.DefaultInstanceKey, log.AppName(m.appName)) 93 } 94 95 func (m *mqLogger) parseLogFields(fields watermill.LogFields) (ctx context.Context, fs log.Fields) { 96 ctx = context.Background() 97 fields = m.fields.Add(fields) 98 99 fs = make(log.Fields, len(fields)+1) 100 for k, v := range fields { 101 if k == watermill.ContextLogFieldKey { 102 ctx = v.(context.Context) 103 continue 104 } 105 fs[k] = v 106 } 107 fs = utils.MapMerge(fs, mqFields) 108 return 109 } 110 111 func (m *mqLogger) isLoggable() bool { 112 if m.confName == "" { 113 return true 114 } 115 116 m.reloadConfig() 117 return m.enabled 118 } 119 120 func (m *mqLogger) reloadConfig() { 121 var cfgs map[string]map[string]any 122 _ = config.Use(m.appName).LoadComponentConfig(config.ComponentMessageQueue, &cfgs) 123 if len(cfgs) == 0 { 124 return 125 } 126 127 cfg, ok := cfgs[m.confName] 128 if !ok { 129 return 130 } 131 enabled := cast.ToBool(cfg["enable_logger"]) 132 m.enabled = enabled 133 }