diff --git a/pkg/common/log/zap.go b/pkg/common/log/zap.go index 7d3dc9232..1646bfd03 100644 --- a/pkg/common/log/zap.go +++ b/pkg/common/log/zap.go @@ -143,7 +143,14 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal } func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} { - keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx), constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...) + operationID := tracelog.GetOperationID(ctx) + opUserID := tracelog.GetOpUserID(ctx) + if opUserID != "" { + keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...) + } + if operationID != "" { + keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...) + } return keysAndValues } diff --git a/pkg/common/tracelog/ctx.go b/pkg/common/tracelog/ctx.go index 233493dbe..d45b13fba 100644 --- a/pkg/common/tracelog/ctx.go +++ b/pkg/common/tracelog/ctx.go @@ -1,6 +1,7 @@ package tracelog import ( + "OpenIM/pkg/common/constant" "OpenIM/pkg/utils" "context" "github.com/sirupsen/logrus" @@ -40,12 +41,17 @@ func GetOperationID(ctx context.Context) string { return f.OperationID } } - return utils.GetFuncName(3) + return "" } func GetOpUserID(ctx context.Context) string { - s, _ := ctx.Value("opUserID").(string) - return s + if ctx.Value(constant.OpUserID) != "" { + s, ok := ctx.Value(constant.OpUserID).(string) + if ok { + return s + } + } + return "" } func Unwrap(err error) error {