Log json escaped string

Hello all,

I noticed that I can use json converter in the log-format directive.

This seems to work as expected →

 log-format '{"root_req_id":"%[capture.req.hdr(0),json(utf8p)]"}'

However how this can be used for e.g. cookies ?

Following doesn’t work for sure:

 log-format '{"rsp_cookie":"%[CS,json(ascii)]"}'


Parsing [/etc/haproxy/haproxy.cfg:88]: failed to parse log-format : failed to parse sample expression <CC,json(ascii)> : unknown fetch method 'CC'.

Per documentation a converter is a function that always follows a fetch.
Based on this I don’t see a way how to do it but I would like to ask:
Can be json converter used for any log flag ? or it isn’t necessary ?



the %CS var seems to return the value of the captured cookie. As it have to be defined, you will only capture the same cookie. Is it what you are trying to do ? If so, you can try to log it the var capture.res.hdr(<idx>)

Yes I’m trying to log cookie as / in JSON and I would like to make sure that the cookie is valid JSON string.

you’re combining two different types of log format interpolation. %CS is a log variable, but %[CS,json(utf8p)] is trying to use a “CS” fetch and then convert it using json. But the CS fetch doesnt exist.

Unfortunately, I don’t see a fetch that is equivalent to %CS. You do have some options, though. You could use %{+E}CS which will escape some characters, notable ", although it isn’t exactly what you need for json. Or if you just need a specific cookie you could do something like %[res.cook(mycookie),json(utf8p)].

Ok guys, thx for suggestions.