[master] 1e03c7b93 varnishncsa: Add %{Varnish:default_format}x

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Feb 7 08:24:07 UTC 2024


commit 1e03c7b938bcef71559467888a27bf990f482433
Author: Walid Boudebouda <walid.boudebouda at gmail.com>
Date:   Tue Jan 30 15:33:41 2024 +0100

    varnishncsa: Add %{Varnish:default_format}x
    
    This is an extended variable that has the same format as the default one
    used when no format is specified.  The variable is useful for appending
    or prepending other formatters to the default format.

diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index 0ad6a8708..8789adbe7 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -162,6 +162,8 @@ static struct ctx {
 	int64_t			vxid;
 } CTX;
 
+static void parse_format(const char *format);
+
 static void
 openout(int append)
 {
@@ -681,6 +683,10 @@ parse_x_format(char *buf)
 		addf_vsl((enum VSL_tag_e)slt, lval, r);
 		return;
 	}
+	if (!strcmp(buf, "Varnish:default_format")) {
+		parse_format(FORMAT);
+		return;
+	}
 	VUT_Error(vut, 1, "Unknown formatting extension: %s", buf);
 }
 
diff --git a/bin/varnishtest/tests/u00003.vtc b/bin/varnishtest/tests/u00003.vtc
index 197ee8919..2f6cc7569 100644
--- a/bin/varnishtest/tests/u00003.vtc
+++ b/bin/varnishtest/tests/u00003.vtc
@@ -182,4 +182,14 @@ shell -expect ${localhost} {
 	varnishncsa -n ${v1_name} -d -q 'RespStatus == 400'
 }
 
+shell {
+	(
+		varnishncsa -n ${v1_name} -d -k 1
+		varnishncsa -n ${v1_name} -d -k 1 -F "%{User-Agent}i"
+	) >def_then_ua.txt
+	varnishncsa -n ${v1_name} -d -k 1 >def_with_ua.txt \
+		-F "%{Varnish:default_format}x\n%{User-Agent}i"
+	diff -u def_then_ua.txt def_with_ua.txt
+}
+
 # ESI coverage in e00003.vtc
diff --git a/doc/sphinx/reference/varnishncsa.rst b/doc/sphinx/reference/varnishncsa.rst
index 173ea8a63..dc09410f7 100644
--- a/doc/sphinx/reference/varnishncsa.rst
+++ b/doc/sphinx/reference/varnishncsa.rst
@@ -173,6 +173,10 @@ Supported formatters are:
 %{X}x
   Extended variables.  Supported variables are:
 
+  Varnish:default_format
+    The log format used when neither -f nor -F options are specified.
+    Useful for appending/prepending with other formatters.
+
   Varnish:time_firstbyte
     Time from when the request processing starts until the first
     byte is sent to the client, in seconds.  For backend mode: Time


More information about the varnish-commit mailing list