style: cleanup code
This commit is contained in:
+12
-8
@@ -118,9 +118,10 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
|
|||||||
|
|
||||||
end := time.Now()
|
end := time.Now()
|
||||||
latency := end.Sub(start)
|
latency := end.Sub(start)
|
||||||
|
status := c.Writer.Status()
|
||||||
|
|
||||||
attributes := []slog.Attr{
|
attributes := []slog.Attr{
|
||||||
slog.Int("status", c.Writer.Status()),
|
slog.Int("status", status),
|
||||||
slog.String("method", c.Request.Method),
|
slog.String("method", c.Request.Method),
|
||||||
slog.String("path", path),
|
slog.String("path", path),
|
||||||
slog.String("route", c.FullPath()),
|
slog.String("route", c.FullPath()),
|
||||||
@@ -176,14 +177,17 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
level := config.DefaultLevel
|
||||||
case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError:
|
msg := "Incoming request"
|
||||||
logger.LogAttrs(context.Background(), config.ClientErrorLevel, c.Errors.String(), attributes...)
|
if status >= http.StatusBadRequest && status < http.StatusInternalServerError {
|
||||||
case c.Writer.Status() >= http.StatusInternalServerError:
|
level = config.ClientErrorLevel
|
||||||
logger.LogAttrs(context.Background(), config.ServerErrorLevel, c.Errors.String(), attributes...)
|
msg = c.Errors.String()
|
||||||
default:
|
} else if status >= http.StatusInternalServerError {
|
||||||
logger.LogAttrs(context.Background(), config.DefaultLevel, "Incoming request", attributes...)
|
level = config.ServerErrorLevel
|
||||||
|
msg = c.Errors.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.LogAttrs(context.Background(), level, msg, attributes...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user