mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 20:30:51 +08:00
@@ -73,17 +73,15 @@ func GinLogrusLogger() gin.HandlerFunc {
|
|||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String()
|
errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String()
|
||||||
|
|
||||||
|
if requestID == "" {
|
||||||
|
requestID = "--------"
|
||||||
|
}
|
||||||
logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s \"%s\"", statusCode, latency, clientIP, method, path)
|
logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s \"%s\"", statusCode, latency, clientIP, method, path)
|
||||||
if errorMessage != "" {
|
if errorMessage != "" {
|
||||||
logLine = logLine + " | " + errorMessage
|
logLine = logLine + " | " + errorMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
var entry *log.Entry
|
entry := log.WithField("request_id", requestID)
|
||||||
if requestID != "" {
|
|
||||||
entry = log.WithField("request_id", requestID)
|
|
||||||
} else {
|
|
||||||
entry = log.WithField("request_id", "--------")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case statusCode >= http.StatusInternalServerError:
|
case statusCode >= http.StatusInternalServerError:
|
||||||
|
|||||||
@@ -40,22 +40,22 @@ func (m *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
|
|||||||
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
||||||
message := strings.TrimRight(entry.Message, "\r\n")
|
message := strings.TrimRight(entry.Message, "\r\n")
|
||||||
|
|
||||||
reqID := ""
|
reqID := "--------"
|
||||||
if id, ok := entry.Data["request_id"].(string); ok && id != "" {
|
if id, ok := entry.Data["request_id"].(string); ok && id != "" {
|
||||||
reqID = id
|
reqID = id
|
||||||
}
|
}
|
||||||
|
|
||||||
levelStr := fmt.Sprintf("%-5s", entry.Level.String())
|
level := entry.Level.String()
|
||||||
|
if level == "warning" {
|
||||||
|
level = "warn"
|
||||||
|
}
|
||||||
|
levelStr := fmt.Sprintf("%-5s", level)
|
||||||
|
|
||||||
var formatted string
|
var formatted string
|
||||||
if reqID != "" && entry.Caller != nil {
|
if 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)
|
formatted = fmt.Sprintf("[%s] [%s] [%s] [%s:%d] %s\n", timestamp, reqID, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, 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)
|
|
||||||
} else {
|
} 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)
|
buffer.WriteString(formatted)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user