You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/log/ctx_logrus.go

89 lines
2.5 KiB

2 years ago
package log
2 years ago
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)
}
}