Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/samber/slog-echo
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Aug 9, 2023
2 parents 3914231 + 9edf55d commit abf3a84
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {

err = next(c)

if err != nil {
c.Error(err)
}

requestID := req.Header.Get(echo.HeaderXRequestID)
if requestID == "" {
requestID = res.Header().Get(echo.HeaderXRequestID)
Expand Down Expand Up @@ -92,9 +96,17 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {

switch {
case status >= http.StatusInternalServerError:
logger.LogAttrs(context.Background(), config.ServerErrorLevel, err.Error(), attributes...)
var errMsg string
if err != nil {
errMsg = err.Error()
}
logger.LogAttrs(context.Background(), config.ServerErrorLevel, errMsg, attributes...)
case status >= http.StatusBadRequest && status < http.StatusInternalServerError:
logger.LogAttrs(context.Background(), config.ClientErrorLevel, err.Error(), attributes...)
var errMsg string
if err != nil {
errMsg = err.Error()
}
logger.LogAttrs(context.Background(), config.ClientErrorLevel, errMsg, attributes...)
case status >= http.StatusMultipleChoices && status < http.StatusBadRequest:
logger.LogAttrs(context.Background(), config.DefaultLevel, "Redirection", attributes...)
default:
Expand Down

0 comments on commit abf3a84

Please sign in to comment.