diff --git a/log/fields_http.go b/log/fields_http.go index 04e7923..d49704b 100644 --- a/log/fields_http.go +++ b/log/fields_http.go @@ -48,6 +48,9 @@ const ( // HTTPTrackingIDKey represents the HTTP tracking id if known (e.g. from X-Tracking-ID). HTTPTrackingIDKey = "http_tracking_id" + + // HTTPRefererKey identifies the address of the web page (i.e., the URI or IRI), from which the resource has been requested. + HTTPRefererKey = "http_referer" ) func FHTTPServerName(id string) zap.Field { @@ -98,6 +101,10 @@ func FHTTPUserAgent(userAgent string) zap.Field { return zap.String(HTTPUserAgentKey, userAgent) } +func FHTTPReferer(host string) zap.Field { + return zap.String(HTTPRefererKey, host) +} + func FHTTPHost(host string) zap.Field { return zap.String(HTTPHostKey, host) } diff --git a/log/with.go b/log/with.go index 1322792..d95886f 100644 --- a/log/with.go +++ b/log/with.go @@ -77,6 +77,16 @@ func WithHTTPRequestID(l *zap.Logger, r *http.Request) *zap.Logger { } } +func WithHTTPReferer(l *zap.Logger, r *http.Request) *zap.Logger { + if value := r.Header.Get("X-Referer"); value != "" { + return With(l, FHTTPReferer(value)) + } else if value := r.Referer(); value != "" { + return With(l, FHTTPHost(value)) + } else { + return l + } +} + func WithHTTPHost(l *zap.Logger, r *http.Request) *zap.Logger { if value := r.Header.Get("X-Forwarded-Host"); value != "" { return With(l, FHTTPHost(value)) @@ -120,6 +130,7 @@ func WithHTTPClientIP(l *zap.Logger, r *http.Request) *zap.Logger { func WithHTTPRequest(l *zap.Logger, r *http.Request) *zap.Logger { l = WithHTTPHost(l, r) + l = WithHTTPReferer(l, r) l = WithHTTPRequestID(l, r) l = WithHTTPSessionID(l, r) l = WithHTTPTrackingID(l, r)