feat: record request_id

This commit is contained in:
JustSong
2025-01-31 17:54:04 +08:00
parent 1fead8e7f7
commit d0402f9086
11 changed files with 96 additions and 55 deletions

View File

@@ -113,16 +113,16 @@ func logHelper(ctx context.Context, level loggerLevel, msg string) {
if level == loggerINFO {
writer = gin.DefaultWriter
}
var logId string
var requestId string
if ctx != nil {
rawLogId := ctx.Value(helper.RequestIdKey)
if rawLogId != nil {
logId = fmt.Sprintf(" | %s", rawLogId.(string))
rawRequestId := helper.GetRequestID(ctx)
if rawRequestId != "" {
requestId = fmt.Sprintf(" | %s", rawRequestId)
}
}
lineInfo, funcName := getLineInfo()
now := time.Now()
_, _ = fmt.Fprintf(writer, "[%s] %v%s%s %s%s \n", level, now.Format("2006/01/02 - 15:04:05"), logId, lineInfo, funcName, msg)
_, _ = fmt.Fprintf(writer, "[%s] %v%s%s %s%s \n", level, now.Format("2006/01/02 - 15:04:05"), requestId, lineInfo, funcName, msg)
SetupLogger()
if level == loggerFatal {
os.Exit(1)