MINOR: config: add environment variables for default log format

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: Sbastien Gross <sgross@haproxy.com>
This commit is contained in:
Sbastien Gross 2022-11-30 22:36:50 +01:00 committed by Willy Tarreau
parent 081091187e
commit 537b9e7f36
2 changed files with 37 additions and 4 deletions

View 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 separated by semicolons. Can be useful in the case you specified a
directory. 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_MWORKER: In master-worker mode, this variable is set to 1.
* HAPROXY_CLI: configured listeners addresses of the stats socket for every * 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 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 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" # strict equivalent of "option tcplog"
log-format "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts \ log-format "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts \
%ac/%fc/%bc/%sc/%rc %sq/%bq" %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 A few fields may slightly vary depending on some configuration options, those
are marked with a star ('*') after the field name below. 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 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 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" # strict equivalent of "option httplog"
log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC \ 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 \ log-format "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp \
%ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc \ %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc \
%bc %sc %rc %sq %bq %CC %CS %hrl %hsl" %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 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 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 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 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" # strict equivalent of "option httpslog"
log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC \ 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 \ %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r \
%[fc_err]/%[ssl_fc_err,hex]/%[ssl_c_err]/\ %[fc_err]/%[ssl_fc_err,hex]/%[ssl_c_err]/\
%[ssl_c_ca_err]/%[ssl_fc_is_resumed] %[ssl_fc_sni]/%sslv/%sslc" %[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 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 appended to it. The new fields (lines 17 and 18) will be detailed here. For the

View File

@ -2073,7 +2073,13 @@ static void init(int argc, char **argv)
if (LIST_ISEMPTY(&cfg_cfgfiles)) if (LIST_ISEMPTY(&cfg_cfgfiles))
usage(progname); 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) { list_for_each_entry(wl, &cfg_cfgfiles, list) {
int ret; int ret;
@ -2099,6 +2105,10 @@ static void init(int argc, char **argv)
exit(1); 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 /* do not try to resolve arguments nor to spot inconsistencies when
* the configuration contains fatal errors caused by files not found * the configuration contains fatal errors caused by files not found