mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
zap
This commit is contained in:
parent
8c931818fd
commit
eeffefc9fc
@ -6,6 +6,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@ -67,6 +68,11 @@ func NewGormDB() (*gorm.DB, error) {
|
|||||||
type Writer struct{}
|
type Writer struct{}
|
||||||
|
|
||||||
func (w Writer) Printf(format string, args ...interface{}) {
|
func (w Writer) Printf(format string, args ...interface{}) {
|
||||||
sql := fmt.Sprintf(format, args...)
|
s := fmt.Sprintf(format, args...)
|
||||||
log.ZDebug(context.Background(), "", "sql", sql)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"OpenIM/pkg/common/tracelog"
|
"OpenIM/pkg/common/tracelog"
|
||||||
"context"
|
"context"
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -58,12 +59,13 @@ func NewZapLogger() (*ZapLogger, error) {
|
|||||||
Development: true,
|
Development: true,
|
||||||
Encoding: "json",
|
Encoding: "json",
|
||||||
EncoderConfig: zap.NewProductionEncoderConfig(),
|
EncoderConfig: zap.NewProductionEncoderConfig(),
|
||||||
|
InitialFields: map[string]interface{}{"PID": os.Getegid()},
|
||||||
}
|
}
|
||||||
zl := &ZapLogger{}
|
zl := &ZapLogger{}
|
||||||
if config.Config.Log.Stderr {
|
if config.Config.Log.Stderr {
|
||||||
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
|
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
|
||||||
}
|
}
|
||||||
zapConfig.EncoderConfig.EncodeTime = zl.timeEncoder
|
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
zapConfig.EncoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
|
zapConfig.EncoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
|
||||||
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
opts, err := zl.cores()
|
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{}) {
|
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...)
|
l.zap.Debugw(msg, keysAndValues...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
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...)
|
l.zap.Infow(msg, keysAndValues...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +130,7 @@ func (l *ZapLogger) Warn(ctx context.Context, msg string, err error, keysAndValu
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
keysAndValues = append(keysAndValues, "error", err)
|
keysAndValues = append(keysAndValues, "error", err)
|
||||||
}
|
}
|
||||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
keysAndValues = l.kvAppend(ctx, keysAndValues)
|
||||||
l.zap.Warnw(msg, 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...)
|
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 {
|
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
|
||||||
dup := *l
|
dup := *l
|
||||||
dup.zap = l.zap.With(keysAndValues...)
|
dup.zap = l.zap.With(keysAndValues...)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user