]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: config: add environment variables for default log format
authorSébastien Gross <sgross@haproxy.com>
Wed, 30 Nov 2022 21:36:50 +0000 (22:36 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 4 Jan 2023 07:23:43 +0000 (08:23 +0100)
This patch provides a convenient way to override the default TCP, HTTP
and HTTP log formats. Instead of having a look into the documentation
to figure out what is the appropriate default log format three new
environment variables can be used: HAPROXY_TCP_LOG_FMT,
HAPROXY_HTTP_LOG_FMT and HAPROXY_HTTPS_LOG_FMT. Their content are
substituted verbatim.

These variables are set before parsing the configuration and are unset
just after all configuration files are successful parsed.

Example:

    # Instead of writing this long log-format line...
    log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC \
                %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r \
                lr=last_rule_file:last_rule_line"

    # ..the HAPROXY_HTTP_LOG_FMT can be used to provide the default
    # http log-format string
    log-format "${HAPROXY_HTTP_LOG_FMT} lr=last_rule_file:last_rule_line"

Please note that nothing prevents users to unset the variables or
override their content in a global section.

Signed-off-by: Sébastien Gross <sgross@haproxy.com>
doc/configuration.txt
src/haproxy.c

index 93f78cb8e9ceb8c08596e83ee9148b75a28e4db9..fafaa3470886f878ec82299f6014d29df3943958 100644 (file)
@@ -756,6 +756,20 @@ file, or could be inherited by a program (See 3.7. Programs):
   separated by semicolons. Can be useful in the case you specified a
   directory.
 
+* HAPROXY_HTTP_LOG_FMT: contains the value of the default HTTP log format as
+  defined in section 8.2.3 "HTTP log format". It can be used to override the
+  default log format without having to copy the whole original definition.
+
+  Example:
+    # Add the rule that gave the final verdict to the log
+    log-format "${HAPROXY_TCP_LOG_FMT} lr=last_rule_file:last_rule_line"
+
+* HAPROXY_HTTPS_LOG_FMT: similar to HAPROXY_HTTP_LOG_FMT but for HTTPS log
+  format as defined in section 8.2.4 "HTTPS log format".
+
+* HAPROXY_TCP_LOG_FMT: similar to HAPROXY_HTTP_LOG_FMT but for TCP log format
+  as defined in section 8.2.2 "TCP log format".
+
 * HAPROXY_MWORKER: In master-worker mode, this variable is set to 1.
 
 * HAPROXY_CLI: configured listeners addresses of the stats socket for every
@@ -21747,11 +21761,14 @@ not be logged if "option dontlog-normal" is specified in the frontend.
 
 The TCP log format is internally declared as a custom log format based on the
 exact following string, which may also be used as a basis to extend the format
-if required. Refer to section 8.2.6 "Custom log format" to see how to use this:
+if required. Additionally the HAPROXY_TCP_LOG_FMT variable can be used instead.
+Refer to section 8.2.6 "Custom log format" to see how to use this:
 
     # strict equivalent of "option tcplog"
     log-format "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts \
                 %ac/%fc/%bc/%sc/%rc %sq/%bq"
+    # or using the HAPROXY_TCP_LOG_FMT variable
+    log-format "${HAPROXY_TCP_LOG_FMT}"
 
 A few fields may slightly vary depending on some configuration options, those
 are marked with a star ('*') after the field name below.
@@ -21930,7 +21947,8 @@ is specified in the frontend.
 
 The HTTP log format is internally declared as a custom log format based on the
 exact following string, which may also be used as a basis to extend the format
-if required. Refer to section 8.2.6 "Custom log format" to see how to use this:
+if required. Additionally the HAPROXY_HTTP_LOG_FMT variable can be used
+instead. Refer to section 8.2.6 "Custom log format" to see how to use this:
 
     # strict equivalent of "option httplog"
     log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC \
@@ -21943,6 +21961,8 @@ this exact string:
     log-format "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp \
                 %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc \
                 %bc %sc %rc %sq %bq %CC %CS %hrl %hsl"
+    # or using the HAPROXY_HTTP_LOG_FMT variable
+    log-format "${HAPROXY_HTTP_LOG_FMT}"
 
 Most fields are shared with the TCP log, some being different. A few fields may
 slightly vary depending on some configuration options. Those ones are marked
@@ -22194,13 +22214,16 @@ dontlognull" in the frontend. Successful connections will not be logged if
 
 The HTTPS log format is internally declared as a custom log format based on the
 exact following string, which may also be used as a basis to extend the format
-if required. Refer to section 8.2.6 "Custom log format" to see how to use this:
+if required. Additionally the HAPROXY_HTTPS_LOG_FMT variable can be used
+instead. Refer to section 8.2.6 "Custom log format" to see how to use this:
 
     # strict equivalent of "option httpslog"
     log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC \
                %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r \
                %[fc_err]/%[ssl_fc_err,hex]/%[ssl_c_err]/\
                %[ssl_c_ca_err]/%[ssl_fc_is_resumed] %[ssl_fc_sni]/%sslv/%sslc"
+    # or using the HAPROXY_HTTPS_LOG_FMT variable
+    log-format "${HAPROXY_HTTPS_LOG_FMT}"
 
 This format is basically the HTTP one (see section 8.2.3) with new fields
 appended to it. The new fields (lines 17 and 18) will be detailed here. For the
index 68c78427d5a53a0b1f5fdf9fbc638d3947f37a0e..c454c803dc6a5e93997fcad6f83c5c1c4fd53613 100644 (file)
@@ -2073,7 +2073,13 @@ static void init(int argc, char **argv)
                if (LIST_ISEMPTY(&cfg_cfgfiles))
                        usage(progname);
 
-
+               /* temporary create environment variables with default
+                * values to ease user configuration. Do not forget to
+                * unset them after the list_for_each_entry loop.
+                */
+               setenv("HAPROXY_HTTP_LOG_FMT", default_http_log_format, 1);
+               setenv("HAPROXY_HTTPS_LOG_FMT", default_https_log_format, 1);
+               setenv("HAPROXY_TCP_LOG_FMT", default_tcp_log_format, 1);
                list_for_each_entry(wl, &cfg_cfgfiles, list) {
                        int ret;
 
@@ -2099,6 +2105,10 @@ static void init(int argc, char **argv)
                                exit(1);
                        }
                }
+               /* remove temporary environment variables. */
+               unsetenv("HAPROXY_HTTP_LOG_FMT");
+               unsetenv("HAPROXY_HTTPS_LOG_FMT");
+               unsetenv("HAPROXY_TCP_LOG_FMT");
 
                /* do not try to resolve arguments nor to spot inconsistencies when
                 * the configuration contains fatal errors caused by files not found