From 9709271ae3c4e3ba186051b02a25c6985f040c5d Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Mon, 13 Mar 2023 10:21:50 +0800 Subject: [PATCH] log --- config/config.yaml | 3 +- pkg/common/config/config.go | 1 + pkg/common/log/ctx_logrus.go | 87 ++++++++++++++++++++++++++++++++++++ pkg/common/log/logrus.go | 87 +----------------------------------- 4 files changed, 92 insertions(+), 86 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index e6282ec15..3230ab3d2 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -24,7 +24,6 @@ mysql: mongo: dbUri: ""#当dbUri值不为空则直接使用该值 - #dbAddress: 127.0.0.1:37017 #单机时为mongo地址,使用分片集群时,为mongos地址 默认即可 dbAddress: [ 127.0.0.1:37017 ] #单机时为mongo地址,使用分片集群时,为mongos地址 默认即可 dbDirect: false dbTimeout: 60 @@ -161,6 +160,8 @@ log: remainRotationCount: 2 #日志数量 #日志级别 6表示全都打印,测试阶段建议设置为6 remainLogLevel: 6 + stderr: false + elasticSearchSwitch: false elasticSearchAddr: [ 127.0.0.1:9201 ] elasticSearchUser: "" diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 11c358ecf..07af58166 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -182,6 +182,7 @@ type config struct { RotationTime int `yaml:"rotationTime"` RemainRotationCount uint `yaml:"remainRotationCount"` RemainLogLevel uint `yaml:"remainLogLevel"` + Stderr bool `yaml:"stderr"` ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"` ElasticSearchAddr []string `yaml:"elasticSearchAddr"` ElasticSearchUser string `yaml:"elasticSearchUser"` diff --git a/pkg/common/log/ctx_logrus.go b/pkg/common/log/ctx_logrus.go index 7330d5405..4054ca7e5 100644 --- a/pkg/common/log/ctx_logrus.go +++ b/pkg/common/log/ctx_logrus.go @@ -1 +1,88 @@ package log + +import ( + "OpenIM/pkg/common/tracelog" + "context" + "github.com/sirupsen/logrus" +) + +func ShowLog(ctx context.Context) { + t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) + OperationID := tracelog.GetOperationID(ctx) + for _, v := range *t.Funcs { + if v.Err != nil { + ctxLogger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": ctxLogger.Pid, + "FilePath": v.File, + }).Errorln("func: ", v.FuncName, " args: ", v.Args, v.Err.Error()) + } else { + switch v.LogLevel { + case logrus.InfoLevel: + ctxLogger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": ctxLogger.Pid, + "FilePath": v.File, + }).Infoln("func: ", v.FuncName, " args: ", v.Args) + case logrus.DebugLevel: + ctxLogger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": ctxLogger.Pid, + "FilePath": v.File, + }).Debugln("func: ", v.FuncName, " args: ", v.Args) + case logrus.WarnLevel: + ctxLogger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": ctxLogger.Pid, + "FilePath": v.File, + }).Warnln("func: ", v.FuncName, " args: ", v.Args) + } + } + } +} + +func InfoWithCtx(ctx context.Context, args ...interface{}) { + t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) + OperationID := tracelog.GetOperationID(ctx) + for _, v := range *t.Funcs { + logger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": logger.Pid, + }).Infoln(v.Args, args) + } +} + +func DebugWithCtx(ctx context.Context, args ...interface{}) { + t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) + OperationID := tracelog.GetOperationID(ctx) + for _, v := range *t.Funcs { + logger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": logger.Pid, + }).Debugln(v.Args, args) + } +} + +func ErrorWithCtx(ctx context.Context, args ...interface{}) { + t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) + OperationID := tracelog.GetOperationID(ctx) + for _, v := range *t.Funcs { + if v.Err != nil { + logger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": logger.Pid, + }).Errorln(v.Err, v.Args, args) + } + } +} + +func WarnWithCtx(ctx context.Context, args ...interface{}) { + t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) + OperationID := tracelog.GetOperationID(ctx) + for _, v := range *t.Funcs { + logger.WithFields(logrus.Fields{ + "OperationID": OperationID, + "PID": logger.Pid, + }).Warnln(v.Args, args) + } +} diff --git a/pkg/common/log/logrus.go b/pkg/common/log/logrus.go index f77e2e54e..19d3c3c49 100644 --- a/pkg/common/log/logrus.go +++ b/pkg/common/log/logrus.go @@ -2,9 +2,7 @@ package log import ( "OpenIM/pkg/common/config" - "OpenIM/pkg/common/tracelog" "bufio" - "context" //"bufio" "fmt" "os" @@ -16,8 +14,8 @@ import ( "github.com/sirupsen/logrus" ) -var logger Logger = *LogrusLogger -var ctxLogger Logger = *LogrusLogger +var logger *LogrusLogger +var ctxLogger *LogrusLogger type LogrusLogger struct { *logrus.Logger @@ -193,84 +191,3 @@ func NewWarn(OperationID string, args ...interface{}) { "PID": logger.Pid, }).Warnln(args) } - -func ShowLog(ctx context.Context) { - t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) - OperationID := tracelog.GetOperationID(ctx) - for _, v := range *t.Funcs { - if v.Err != nil { - ctxLogger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": ctxLogger.Pid, - "FilePath": v.File, - }).Errorln("func: ", v.FuncName, " args: ", v.Args, v.Err.Error()) - } else { - switch v.LogLevel { - case logrus.InfoLevel: - ctxLogger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": ctxLogger.Pid, - "FilePath": v.File, - }).Infoln("func: ", v.FuncName, " args: ", v.Args) - case logrus.DebugLevel: - ctxLogger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": ctxLogger.Pid, - "FilePath": v.File, - }).Debugln("func: ", v.FuncName, " args: ", v.Args) - case logrus.WarnLevel: - ctxLogger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": ctxLogger.Pid, - "FilePath": v.File, - }).Warnln("func: ", v.FuncName, " args: ", v.Args) - } - } - } -} - -func InfoWithCtx(ctx context.Context, args ...interface{}) { - t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) - OperationID := tracelog.GetOperationID(ctx) - for _, v := range *t.Funcs { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Infoln(v.Args, args) - } -} - -func DebugWithCtx(ctx context.Context, args ...interface{}) { - t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) - OperationID := tracelog.GetOperationID(ctx) - for _, v := range *t.Funcs { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Debugln(v.Args, args) - } -} - -func ErrorWithCtx(ctx context.Context, args ...interface{}) { - t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) - OperationID := tracelog.GetOperationID(ctx) - for _, v := range *t.Funcs { - if v.Err != nil { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Errorln(v.Err, v.Args, args) - } - } -} - -func WarnWithCtx(ctx context.Context, args ...interface{}) { - t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos) - OperationID := tracelog.GetOperationID(ctx) - for _, v := range *t.Funcs { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Warnln(v.Args, args) - } -}