git » gofer » commit 94fe1ac

reqlog: Use "-" when HTTP.Request.Host is empty

author Alberto Bertogli
2025-06-06 09:48:02 UTC
committer Alberto Bertogli
2025-06-06 09:48:02 UTC
parent 93594c609fb53a49009345c7258747a63b911425

reqlog: Use "-" when HTTP.Request.Host is empty

Today, we print HTTP.Request.Host as-is. So if it is empty, we just skip
the field in the logs.

A log line with Host value: `... HTTP/1.1 example.com GET ...`.

And with Host empty: `... HTTP/1.1  GET ...` (note the double spaces).

This makes the logs more difficult to read and parse.

To fix this, this patch makes gofer log `-` when the host is empty.

After this change, the a log line will look like:
`... HTTP/1.1 - GET ...` (note the `-` in the host field).

reqlog/reqlog.go +2 -1

diff --git a/reqlog/reqlog.go b/reqlog/reqlog.go
index 8e41ce6..82af810 100644
--- a/reqlog/reqlog.go
+++ b/reqlog/reqlog.go
@@ -61,7 +61,8 @@ const lighttpdFormat = "{{.H.RemoteAddr}} {{.H.Host}} - [{{.T.Format \"02/Jan/20
 
 // gofer format, this is the default, and can handle both raw and HTTP events.
 const goferFormat = "{{.T.Format \"2006-01-02 15:04:05.000\"}}" +
-	"{{if .H}} {{.H.RemoteAddr}} {{.H.Proto}} {{.H.Host}} {{.H.Method}}" +
+	"{{if .H}} {{.H.RemoteAddr}} {{.H.Proto}}" +
+	" {{if .H.Host}}{{.H.Host}}{{else}}-{{end}} {{.H.Method}}" +
 	" {{.H.URL}} {{.H.Header.Referer|q}} {{index .H.Header \"User-Agent\"|q}}{{end}}" +
 	"{{if .R}} {{.R.RemoteAddr}} raw {{.R.LocalAddr}}{{end}}" +
 	" = {{.Status}} {{.Length}}b {{.Latency.Milliseconds}}ms\n"