fix: reduce RequestLogger cyclomatic complexity
This commit is contained in:
@@ -24,15 +24,8 @@ func RequestLogger() gin.HandlerFunc {
|
|||||||
|
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
status := c.Writer.Status()
|
status := c.Writer.Status()
|
||||||
fields := map[string]any{
|
|
||||||
"client_ip": c.ClientIP(),
|
|
||||||
"duration_ms": float64(duration.Microseconds()) / 1000,
|
|
||||||
"method": c.Request.Method,
|
|
||||||
"path": path,
|
|
||||||
"request_id": c.Writer.Header().Get(requestIDHeader),
|
|
||||||
"status": status,
|
|
||||||
}
|
|
||||||
privateErrors := c.Errors.ByType(gin.ErrorTypePrivate)
|
privateErrors := c.Errors.ByType(gin.ErrorTypePrivate)
|
||||||
|
privateErrorText := privateErrors.String()
|
||||||
var logErr error
|
var logErr error
|
||||||
if len(privateErrors) > 0 {
|
if len(privateErrors) > 0 {
|
||||||
logErr = privateErrors.Last().Err
|
logErr = privateErrors.Last().Err
|
||||||
@@ -43,18 +36,6 @@ func RequestLogger() gin.HandlerFunc {
|
|||||||
if c.FullPath() == "" && status == http.StatusSeeOther {
|
if c.FullPath() == "" && status == http.StatusSeeOther {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if route != path {
|
|
||||||
fields["route"] = route
|
|
||||||
}
|
|
||||||
if query != "" {
|
|
||||||
fields["query"] = redactSensitiveQuery(query)
|
|
||||||
}
|
|
||||||
if size := c.Writer.Size(); size >= 0 {
|
|
||||||
fields["bytes"] = size
|
|
||||||
}
|
|
||||||
if errors := privateErrors.String(); errors != "" {
|
|
||||||
fields["errors"] = errors
|
|
||||||
}
|
|
||||||
|
|
||||||
observability.LogContext(
|
observability.LogContext(
|
||||||
c.Request.Context(),
|
c.Request.Context(),
|
||||||
@@ -62,7 +43,7 @@ func RequestLogger() gin.HandlerFunc {
|
|||||||
"http_request",
|
"http_request",
|
||||||
"http",
|
"http",
|
||||||
c.Request.Method+" "+path,
|
c.Request.Method+" "+path,
|
||||||
fields,
|
requestLogFields(c, path, query, route, duration, status, privateErrorText),
|
||||||
logErr,
|
logErr,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user