Skip to content

Commit

Permalink
Add more logger improvements (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrowski authored Jan 24, 2025
1 parent a0b6a69 commit 05fa0cd
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions zap/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,30 @@ func (w *Logger) EchoMiddleware() echo.MiddlewareFunc {

log := w.Ctx(c.Request().Context()).With(fields...)
n := res.Status

if err != nil {
log = log.Err(err)
}

// Do not log the error when request canceled by the client
if errors.Is(err, context.Canceled) {
log.Info("Request canceled")

return nil
}

// Status 5XX is logged as error as this should not happen in production.
if n >= 500 {
// Do not log the error when request canceled by the client
if errors.Is(err, context.Canceled) {
log.Err(err).Info("Request canceled")
} else {
log.Err(err).Error("Error")
}
} else {
log.Info("Incoming request")
log.Error("Error")

return nil
}
if n >= 400 {
log.Warn("Warn")

return nil
}
log.Info("Info")

return nil
}
Expand Down

0 comments on commit 05fa0cd

Please sign in to comment.