HAProxy Enterprise Documentation 2.1r1

Log Custom Data

Although HAProxy Enterprise ships with premade log formats for TCP and HTTP, you can also define your own format and record a custom set of information about connections or HTTP requests. To do so, you will define log format variables, or use predefined log format variables, and arrange them in a new log format rule.

Log format variables

A log format variable is a string prefixed by the character %:


You can use them to create custom log formats that record custom data in your HAProxy Enterprise logs.

Avoid spaces in the variable name. A space is considered as a separator. If a variable is between square brackets ([ .. ]), then it can be an expression, such as to call a fetch method to get a value. Below, we get the value of the Host HTTP header by using the req.hdr fetch method:


A common way to use this is to record a variable in the logs by using the var fetch method as shown below:

frontend www
   bind :80

   # store the 'path' in a variable named 'txn.mypath'
   http-request set-var(txn.mypath) path

   # logs will show the path value
   log-format %[var(txn.mypath)]

You can prefix a variable with a plus or minus sign and a flag. A plus adds the flag, while a minus removes it. There are three flags available:

  • Q: adds double quotes around the value

  • X: represents the value as hexadecimal

  • E: escapes characters with a backslash, such as double quotes and backslashes

In the example below, we add double quotes around the Host header value:


You can use a special variable %o to propagate its flags to all following variables in the same format string.


To emit a verbatim %, it must be preceded by another % to result in %%. HAProxy Enterprise automatically merges consecutive separators.

Check the list of predefined log format variables in the Reference Guide.

Define a new log format

You will use the log-format directive to create a new log format. This line accepts log format variables.

If you were to replicate the format you get with option tcplog, it would look like this:

   log-format "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"

The option httplog format would look like this:

   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"

You can define your own format. For example, below, we collect only the client's IP address and port, the server's IP address and port, the URL path, and the response status code:

   log-format "%ci:%cp %si:%sp %HU %ST"
   # logs: / 304

Store the log format in an environment variable

Often, you will want to keep the existing information collected by the premade log format rules (option tcplog and option httplog) and append new information to them. To make that easier, you can store the original format in an environment variable and then reference it in your log-format line.

  1. Add the premade log format to an environment variable by using setenv in the global section. Below, we store the HTTP log format in a variable named HTTP_LOG:

       setenv HTTP_LOG "%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"
  2. Reference the environment variable in your log-format line. In the example below, we append a variable named txn.myvariable to the end of the original HTTP log format:

       log-format "${HTTP_LOG} %[var(txn.myvariable)]"

Next up

Log with Docker