github.com/wfusion/gofusion@v1.1.14/mq/log.go (about) 1 package mq 2 3 import ( 4 "context" 5 "fmt" 6 "syscall" 7 8 "github.com/wfusion/gofusion/common/infra/watermill" 9 "github.com/wfusion/gofusion/common/utils" 10 "github.com/wfusion/gofusion/config" 11 12 fusCtx "github.com/wfusion/gofusion/context" 13 ) 14 15 func logError(ctx context.Context, l watermill.LoggerAdapter, app, n, m string, args ...any) { 16 l.Error(formatLogMsg(app, n, m, args...), nil, formatFields(ctx)) 17 } 18 func logInfo(ctx context.Context, l watermill.LoggerAdapter, app, n, m string, args ...any) { 19 l.Info(formatLogMsg(app, n, m, args...), formatFields(ctx)) 20 } 21 func logDebug(ctx context.Context, l watermill.LoggerAdapter, app, n, m string, args ...any) { 22 l.Debug(formatLogMsg(app, n, m, args...), formatFields(ctx)) 23 } 24 func logTrace(ctx context.Context, l watermill.LoggerAdapter, app, n, m string, args ...any) { 25 l.Trace(formatLogMsg(app, n, m, args...), formatFields(ctx)) 26 } 27 28 func formatFields(ctx context.Context) (fs watermill.LogFields) { 29 fs = make(watermill.LogFields, 4) 30 if userID := fusCtx.GetUserID(ctx); utils.IsStrNotBlank(userID) { 31 fs["user_id"] = userID 32 } 33 if traceID := fusCtx.GetTraceID(ctx); utils.IsStrNotBlank(traceID) { 34 fs["trace_id"] = traceID 35 } 36 if taskID := fusCtx.GetCronTaskID(ctx); utils.IsStrNotBlank(taskID) { 37 fs["cron_task_id"] = taskID 38 } 39 if taskName := fusCtx.GetCronTaskName(ctx); utils.IsStrNotBlank(taskName) { 40 fs["cron_task_name"] = taskName 41 } 42 return 43 } 44 45 func formatLogMsg(app, n, src string, args ...any) (dst string) { 46 appName := config.Use(app).AppName() 47 return fmt.Sprintf("%v [Gofusion] %s %s %s %s", 48 syscall.Getpid(), appName, config.ComponentMessageQueue, n, fmt.Sprintf(src, args...)) 49 }