Skip to content

Commit

Permalink
fix: remove recreating zap logger; change time field to latency (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanbolat authored Jun 30, 2021
1 parent 794c1c9 commit 117d663
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func ZapLogger(log *zap.Logger) echo.MiddlewareFunc {

err := next(c)
if err != nil {
log = log.With(zap.Error(err))
c.Error(err)
}

Expand All @@ -26,7 +25,7 @@ func ZapLogger(log *zap.Logger) echo.MiddlewareFunc {

fields := []zapcore.Field{
zap.String("remote_ip", c.RealIP()),
zap.String("time", time.Since(start).String()),
zap.String("latency", time.Since(start).String()),
zap.String("host", req.Host),
zap.String("request", fmt.Sprintf("%s %s", req.Method, req.RequestURI)),
zap.Int("status", res.Status),
Expand All @@ -43,9 +42,9 @@ func ZapLogger(log *zap.Logger) echo.MiddlewareFunc {
n := res.Status
switch {
case n >= 500:
log.Error("Server error", fields...)
log.With(zap.Error(err)).Error("Server error", fields...)
case n >= 400:
log.Warn("Client error", fields...)
log.With(zap.Error(err)).Warn("Client error", fields...)
case n >= 300:
log.Info("Redirection", fields...)
default:
Expand Down

0 comments on commit 117d663

Please sign in to comment.