From: Valentine Krasnobaeva Date: Tue, 4 Mar 2025 15:35:05 +0000 (+0100) Subject: MINOR: sample: allow custom date format in error-log-format X-Git-Tag: v3.2-dev7~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b46b81949fc9d100eb1c023a7c7b5036f13226bc;p=thirdparty%2Fhaproxy.git MINOR: sample: allow custom date format in error-log-format Sample fetches %[accept_date] and %[request_date] with converters can be used in error-log-format string. But in the most error cases they fetches nothing, as error logs are produced on SSL handshake issues or when invalid PROXY protocol header is used. Stream object is never allocated in such cases and smp_fetch_accept_date() just simply returns 0. There is a need to have a custom date format (ISO8601) also in the error logs, along with normal logs. When sess_build_logline_orig() builds log line it always copies the accept date to strm_logs structure. When stream is absent, accept date is copied from the session object. So, if the steam object wasn't allocated, let's use the session date info in smp_fetch_accept_date(). This allows then, in sample_process(), to apply to the fetched date different converters and formats. This fixes the issue #2884. --- diff --git a/src/tcp_sample.c b/src/tcp_sample.c index 109119070..a552ebd4f 100644 --- a/src/tcp_sample.c +++ b/src/tcp_sample.c @@ -499,18 +499,26 @@ smp_fetch_accept_date(const struct arg *args, struct sample *smp, const char *kw struct strm_logs *logs; struct timeval tv; - if (!smp->strm) + if (smp->strm) { + logs = &smp->strm->logs; + + if (kw[0] == 'r') { /* request_date */ + tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); + } else { /* accept_date */ + tv.tv_sec = logs->accept_date.tv_sec; + tv.tv_usec = logs->accept_date.tv_usec; + } + /* case of error-log-format */ + } else if (smp->sess) { + if (kw[0] == 'r') { /* request_date */ + tv_ms_add(&tv, &smp->sess->accept_date, smp->sess->t_idle >= 0 ? smp->sess->t_idle + smp->sess->t_handshake : 0); + } else { /* accept_date */ + tv.tv_sec = smp->sess->accept_date.tv_sec; + tv.tv_usec = smp->sess->accept_date.tv_usec; + } + } else return 0; - logs = &smp->strm->logs; - - if (kw[0] == 'r') { /* request_date */ - tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); - } else { /* accept_date */ - tv.tv_sec = logs->accept_date.tv_sec; - tv.tv_usec = logs->accept_date.tv_usec; - } - smp->data.u.sint = tv.tv_sec; /* report in milliseconds */