diff --git a/internal/logging/gin_logger.go b/internal/logging/gin_logger.go index b45a03d7..2dfbcfc2 100644 --- a/internal/logging/gin_logger.go +++ b/internal/logging/gin_logger.go @@ -76,12 +76,12 @@ func GinLogrusLogger() gin.HandlerFunc { if requestID == "" { requestID = "--------" } - logLine := fmt.Sprintf("%3d | %13v | %15s | %s | %-7s \"%s\"", statusCode, latency, clientIP, requestID, method, path) + logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s \"%s\"", statusCode, latency, clientIP, method, path) if errorMessage != "" { logLine = logLine + " | " + errorMessage } - entry := log.NewEntry(log.StandardLogger()) + entry := log.WithField("request_id", requestID) switch { case statusCode >= http.StatusInternalServerError: diff --git a/internal/logging/global_logger.go b/internal/logging/global_logger.go index 6ece7240..6ea1963a 100644 --- a/internal/logging/global_logger.go +++ b/internal/logging/global_logger.go @@ -40,7 +40,7 @@ func (m *LogFormatter) Format(entry *log.Entry) ([]byte, error) { timestamp := entry.Time.Format("2006-01-02 15:04:05") message := strings.TrimRight(entry.Message, "\r\n") - reqID := "" + reqID := "--------" if id, ok := entry.Data["request_id"].(string); ok && id != "" { reqID = id } @@ -52,14 +52,10 @@ func (m *LogFormatter) Format(entry *log.Entry) ([]byte, error) { levelStr := fmt.Sprintf("%-5s", level) var formatted string - if reqID != "" && entry.Caller != nil { - formatted = fmt.Sprintf("[%s] [%s] [%s:%d] | %s | %s\n", timestamp, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, reqID, message) - } else if reqID != "" { - formatted = fmt.Sprintf("[%s] [%s] | %s | %s\n", timestamp, levelStr, reqID, message) - } else if entry.Caller != nil { - formatted = fmt.Sprintf("[%s] [%s] [%s:%d] %s\n", timestamp, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, message) + if entry.Caller != nil { + formatted = fmt.Sprintf("[%s] [%s] [%s] [%s:%d] %s\n", timestamp, reqID, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, message) } else { - formatted = fmt.Sprintf("[%s] [%s] %s\n", timestamp, levelStr, message) + formatted = fmt.Sprintf("[%s] [%s] [%s] %s\n", timestamp, reqID, levelStr, message) } buffer.WriteString(formatted)