HAProxy JSON logs with log-format %{+json}o and +utf8
Put %{+json}o in an HAProxy log-format to JSON-encode named items. Seven requests compare it with a hand-built string; non-ASCII text needs +utf8.
For JSON logs from HAProxy, start log-format with %{+json}o and give every item a name with %(name). Building a JSON-looking string by hand with your own quotes breaks as soon as a User-Agent contains a quote or a tab. To keep UTF-8 text such as café readable, add +utf8.
[1] Define the format. %{+json}o applies the encoding to every item on the line, and only named items become keys (section 8.2.6 of the 3.2 manual).
global log stdout format raw local0 defaults mode http log global timeout client 5s timeout connect 5s timeout server 5s frontend fe_json_utf8 bind :8082 http-request capture req.hdr(User-Agent) len 100 log-format "%{+json,+utf8}o %(time)t %(client_ip)ci %(frontend)f %(status)ST %(bytes)B %(request)r %(user_agent)[capture.req.hdr(0)]"This format assumes an HTTP-mode frontend that inherits its log target through log global (the defaults above). On a running setup, do not create a new frontend: add just the http-request capture and log-format lines to your existing HTTP frontend, and confirm it has mode http and log global (or its own log line). Copy the configuration file first so that rolling back is restoring that copy, then check and reload. The test had HAProxy answer directly (http-request return); a real service keeps its existing default_backend.
Spaces are delimiters in log-format, so wrap the whole format in double quotes; the manual recommends quoted formats too.
[2] Check the configuration.
$ haproxy -c -f /etc/haproxy/haproxy.cfg[3] Compare three formats on the same requests. Three frontends differed only in the format: +json, +json,+utf8, and a hand-built string without encoding, such as '{"client_ip":"%ci",…,"user_agent":"%[capture.req.hdr(0)]"}'. Seven raw-socket requests went to each (a plain one, a User-Agent with quotes and a backslash, café naïve, Korean text, a tab, invalid UTF-8 \xff\xfe, and é in the path), and each line went through jq -e .:
json.log: 7/7 lines parsed by jqjson_utf8.log: 7/7 lines parsed by jqhandmade.log: 5/7 lines parsed by jq| User-Agent | +json | +json,+utf8 | Hand-built string |
|---|---|---|---|
Mozilla/5.0 "quoted" \back | \"quoted\" \\back | Same | Raw quotes, jq fails |
café naïve | café … | café naïve | café naïve |
| Korean text | ê²\u0080… | Kept as UTF-8 | Kept as UTF-8 |
| Tab | \t | \t | Raw tab, jq fails |
bad\xff\xfe | badÿþ | badÿþ | Raw bytes |
[4] Without +utf8, non-ASCII values are split into bytes. The JSON is valid but the text decodes to mojibake; the manual itself says U+00E9 is logged as "é".
$ sed -n 3p json.log | jq -r .user_agentcafé naïve$ sed -n 3p json_utf8.log | jq -r .user_agentcafé naïve+utf8 passes only valid UTF-8 through and escapes invalid bytes one at a time as \u00XX, so the output is always valid UTF-8. The manual warns that characters that change terminal rendering (direction changes, zero-width characters) also pass through, and recommends it for trusted input or logs that are not shown on a terminal.
[5] See how the hand-built string breaks. Without an encoding flag, values from %[…] sample expressions were not escaped:
$ sed -n 2p handmade.log | jq .jq: parse error: Invalid numeric literal at line 1, column 170$ sed -n 5p handmade.log | jq .jq: parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 1, column 159Section 8.6 of the manual says characters outside 32 to 126 and " are written as #XX (such as #22) in logs, but in this test the quote and tab from the capture.req.hdr(0) sample came out raw. Avoid hand-built JSON formats.
[6] Know what a bad request looks like. A request with an unencoded é in the path was rejected with 400 and logged as:
$ sed -n 7p json.log | jq -c "{status, request, user_agent}"{"status":400,"request":"<BADREQ>","user_agent":""}+json logs an empty value as ""; the hand-built format logged -. Update dashboard filters that look for -. Use the termination state field to see why a 400 happened (reading termination states).
[7] When logging to files, check for a prefix. This test used log stdout format raw and got bare messages. Through a syslog daemon, a date and host may be prepended, so strip that before jq or parse only the message field in your shipper (the syslog path was not tested).
Analyze your HAProxy log in the browser ↗ — nothing is uploaded.
Check it with the JSON · YAML formatter and converter tool ↗ — calculated in your browser.