git » gofer » commit 7b3d0c0

reqlog: Log referer and user agent in the default log

author Alberto Bertogli
2020-06-08 00:57:28 UTC
committer Alberto Bertogli
2020-06-08 00:57:28 UTC
parent 27b84b36946e439106f5ad22afcc8494e379583a

reqlog: Log referer and user agent in the default log

reqlog/reqlog.go +17 -4
test/test.sh +12 -12

diff --git a/reqlog/reqlog.go b/reqlog/reqlog.go
index a4774b4..9a26320 100644
--- a/reqlog/reqlog.go
+++ b/reqlog/reqlog.go
@@ -6,6 +6,7 @@ import (
 	"net"
 	"net/http"
 	"os"
+	"strings"
 	"text/template"
 	"time"
 
@@ -48,7 +49,7 @@ const commonFormat = "{{.H.RemoteAddr}} - - [{{.T.Format \"02/Jan/2006:15:04:05
 // Combined log format, extension of the Common Log Format, and used by a lot
 // of servers (e.g. Apache).
 // https://httpd.apache.org/docs/2.4/logs.html#combined
-const combinedFormat = "{{.H.RemoteAddr}} - - [{{.T.Format \"02/Jan/2006:15:04:05 -0700\"}}] \"{{.H.Method}} {{.H.URL}} {{.H.Proto}}\" {{.Status}} {{.Length}} {{.H.Header.Referer|q}} {{.H.Header.User-agent|q}}\n"
+const combinedFormat = "{{.H.RemoteAddr}} - - [{{.T.Format \"02/Jan/2006:15:04:05 -0700\"}}] \"{{.H.Method}} {{.H.URL}} {{.H.Proto}}\" {{.Status}} {{.Length}} {{.H.Header.Referer|q}} {{index .H.Header \"User-Agent\"|q}}\n"
 
 // Extension of the combined log format, prepending the virtual host.
 // https://httpd.apache.org/docs/2.4/logs.html#virtualhost
@@ -60,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}} {{.H.URL}}{{end}}" +
+	"{{if .H}} {{.H.RemoteAddr}} {{.H.Proto}} {{.H.Host}} {{.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"
 
@@ -181,6 +183,17 @@ func FromContext(ctx context.Context) *Log {
 	return v.(*Log)
 }
 
-func quoteString(s string) string {
-	return fmt.Sprintf("%q", s)
+func quoteString(i interface{}) string {
+	if i == nil {
+		return `""`
+	}
+
+	switch v := i.(type) {
+	case string:
+		return fmt.Sprintf("%q", v)
+	case []string:
+		return fmt.Sprintf("%q", strings.Join(v, ", "))
+	default:
+		return fmt.Sprintf("unknown-type-%T", v)
+	}
 }
diff --git a/test/test.sh b/test/test.sh
index b641a0b..3a28c18 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -205,14 +205,14 @@ done
 
 echo "### Request log"
 function logtest() {
-		exp http://localhost:8441/cgi/logtest
-		for f in .01-be.requests.log .01-fe.requests.log; do
-				EXPECT="localhost:8441 GET /cgi/logtest = 200"
-				if ! waitgrep -q "$EXPECT" $f; then
-						echo "$f: log entry not found"
-						exit 1
-				fi
-		done
+	exp http://localhost:8441/cgi/logtest
+	for f in .01-be.requests.log .01-fe.requests.log; do
+		EXPECT='localhost:8441 GET /cgi/logtest "" "Go-http-client/1.1" = 200'
+		if ! waitgrep -q "$EXPECT" $f; then
+			echo "$f: log entry not found"
+			exit 1
+		fi
+	done
 }
 
 # Check that the entry appears.
@@ -226,10 +226,10 @@ kill -HUP $FE_PID $BE_PID
 # Expect the entry again, and make sure it's the only one.
 logtest
 for f in .01-be.requests.log .01-fe.requests.log; do
-		if [ "$(wc -l < $f)" != 1 ]; then
-			echo "$f: unexpected number of entries"
-			exit 1
-		fi
+	if [ "$(wc -l < $f)" != 1 ]; then
+		echo "$f: unexpected number of entries"
+		exit 1
+	fi
 done