diff --git a/pkg/common/db/relation/mysql_init.go b/pkg/common/db/relation/mysql_init.go index 388ac62c2..789949a20 100644 --- a/pkg/common/db/relation/mysql_init.go +++ b/pkg/common/db/relation/mysql_init.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "gorm.io/driver/mysql" + "strings" "time" "gorm.io/gorm" @@ -67,6 +68,11 @@ func NewGormDB() (*gorm.DB, error) { type Writer struct{} func (w Writer) Printf(format string, args ...interface{}) { - sql := fmt.Sprintf(format, args...) - log.ZDebug(context.Background(), "", "sql", sql) + s := fmt.Sprintf(format, args...) + l := strings.Split(s, "\n") + if len(l) == 2 { + log.ZDebug(context.Background(), "sql exec detail", "gorm", l[0], "sql", l[1]) + } else { + log.ZDebug(context.Background(), "sql exec detail", "sql", s) + } } diff --git a/pkg/common/log/zap.go b/pkg/common/log/zap.go index c0fe38b6a..7d3dc9232 100644 --- a/pkg/common/log/zap.go +++ b/pkg/common/log/zap.go @@ -6,6 +6,7 @@ import ( "OpenIM/pkg/common/tracelog" "context" rotatelogs "github.com/lestrrat-go/file-rotatelogs" + "os" "path/filepath" "time" @@ -58,12 +59,13 @@ func NewZapLogger() (*ZapLogger, error) { Development: true, Encoding: "json", EncoderConfig: zap.NewProductionEncoderConfig(), + InitialFields: map[string]interface{}{"PID": os.Getegid()}, } zl := &ZapLogger{} if config.Config.Log.Stderr { zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr") } - zapConfig.EncoderConfig.EncodeTime = zl.timeEncoder + zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder zapConfig.EncoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder opts, err := zl.cores() @@ -115,12 +117,12 @@ func (l *ZapLogger) ToZap() *zap.SugaredLogger { } func (l *ZapLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) { - keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...) + keysAndValues = l.kvAppend(ctx, keysAndValues) l.zap.Debugw(msg, keysAndValues...) } func (l *ZapLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) { - keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...) + keysAndValues = l.kvAppend(ctx, keysAndValues) l.zap.Infow(msg, keysAndValues...) } @@ -128,7 +130,7 @@ func (l *ZapLogger) Warn(ctx context.Context, msg string, err error, keysAndValu if err != nil { keysAndValues = append(keysAndValues, "error", err) } - keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...) + keysAndValues = l.kvAppend(ctx, keysAndValues) l.zap.Warnw(msg, keysAndValues...) } @@ -140,6 +142,11 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal l.zap.Errorw(msg, keysAndValues...) } +func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} { + keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx), constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...) + return keysAndValues +} + func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger { dup := *l dup.zap = l.zap.With(keysAndValues...)