Log custom information in the log file (backend)

I found this example configuration code online:

http-request set-var(txn.ratelimited) str(RATE-LIMITED) if too_many_requests
http-request capture var(txn.ratelimited) len 12

It works well for the frontend definition, but does not work with the backend definition. I would like to log some custom information in my backend configuration. Is there something compareable to http-request capture for the backend?

I am using 2.4.22

Thank you so much!


Hi Ralph:

From http-request capture doc:

  When using this action in a backend, double check that the relevant
  frontend(s) have the required capture slots otherwise, this rule will be
  ignored at run time. This can't be detected at configuration parsing time
  due to HAProxy's ability to dynamically resolve backend name at runtime.

I guess that you need to set declare capture request len 12 on the related frontend.

Thank you so much. Can you share a link to the documentation? I have searched the documentation and did not find any hints like the above. I am an absolute newbie with haproxy.

Different to the hints above, the configuration reload fails with

Jan 22 00:12:19 elmratelimha11.fyre.ibm.com haproxy[22525]: [ALERT] (22525) : parsing [/etc/haproxy/haproxy.cfg:121] : error detected in backend ‘elm_default’ while parsing ‘http-request capture’ rule : proxy ‘elm_default’ has no frontend capability.
Jan 22 00:12:19 elmratelimha11.fyre.ibm.com haproxy[22525]: [ALERT] (22525) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
Jan 22 00:12:19 elmratelimha11.fyre.ibm.com systemd[1]: haproxy.service: Control process exited, code=exited, status=1/FAILURE

I found another way to modify logging to be able to log the data I need.

Configure in the frontend

log-format “${HAPROXY_HTTP_LOG_FMT} limit:%{+Q}[var(txn.test)] agent:%{+Q}[var(txn.ua)] backend:%{+Q}[var(txn.back)] path:%{+Q}[var(txn.path)]”

Then use the next statement in the frontend or the backend.
http-request set-var(txn.ua) hdr(user-agent)

Hi Ralph,

I think that you can do that in a simple way, only setting this line on the frontend:

capture request header user-agent len 30

Anyway, the documentation is in https://www.haproxy.org/download/2.9/doc/configuration.txt

Hi Ricardo,

Thank you for the link and the answer. This is what I was using too. I am still a bit lost in the documentation.

I am fine with the customized logging, it works and is easy enough to use and understand.

The use case I had was where the data was only available in the backend.
The documentation mentions capture is only available in the frontend.

Thanks again for your help.