Skip to content

Commit

Permalink
add logging for x-forwarded-for header if present
Browse files Browse the repository at this point in the history
  • Loading branch information
nullbio committed May 19, 2021
1 parent bd05cbe commit 31506b9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
11 changes: 8 additions & 3 deletions abcmiddleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func (z zapLogger) writeZap(zw *zapResponseWriter, r *http.Request, startTime ti
}
}

// log all the fields
logger.Info(fmt.Sprintf("%s request", protocol),
fields := []zap.Field{
zap.Int("status", zw.status),
zap.Int("size", zw.size),
zap.Bool("hijacked", zw.hijacked),
Expand All @@ -69,5 +68,11 @@ func (z zapLogger) writeZap(zw *zapResponseWriter, r *http.Request, startTime ti
zap.String("host", r.Host),
zap.String("remote_addr", r.RemoteAddr),
zap.Duration("elapsed", elapsed),
)
}

if ff := r.Header.Get("X-Forwarded-For"); ff != "" {
fields = append(fields, zap.String("x_forwarded_for", ff))
}

logger.Info(fmt.Sprintf("%s request", protocol), fields...)
}
Loading

0 comments on commit 31506b9

Please sign in to comment.