Bootstrap FreeKB - HAProxy (Load Balance) - Logging to a custom log file
HAProxy (Load Balance) - Logging to a custom log file

Updated:   |  HAProxy (Load Balance) articles

By default, HAProxy is configured to log events to the systems deamon, which typically means the journalctl command (on Linux) would be used to view the HAProxy events. If you instead would like to have HAProxy log events to it's own log file, you will first need to determine the system logger being used. 

The ps command can be used to determine if your system is using syslogd, rsyslogd or syslog-ng. In this example, rsyslogd is being used.

~]# ps -ef | grep -i syslog
root      1287     1  0 Aug17 ?        00:13:30 /usr/sbin/rsyslogd -n

 

If rsyslog is being used, then you would create a file in the /etc/rsyslog.d directory.

touch /etc/rsyslog.d/haproxy.conf

 

And append the following to /etc/rsyslog.d/haproxy.conf.

local2.*  /var/log/haproxy.log

 

Then you would add the following to /etc/rsyslog.conf.

$ModLoad imudp
$UDPServerRun 514
$UDPServerAddress 127.0.0.1

 

And restart the rsyslog daemon for this change to take effect.

systemctl restart rsyslog

 

Now, the /var/log/haproxy.log should exists and contain HAProxy events.


Log Level & Log Format

By default, events will be logged at log level INFO. If you have requests that are being load balanced to a cluster of web servers, the httploghttp-request and log-format directives can be used to control the log level and log format of the events that get appended to /var/log/haproxy.log. Here is an example of how you could use the httploghttp-request and log-format directives in a frontend block.

  • %hr logs request headers
  • %hs logs response headers
  • %r logs HTTP requests
frontend main
    bind *:80
    balance roundrobin
    option httplog
    http-request capture req.hdrs len 1024
    log-format "%ci:%cp [%tr] %ft [[%hr]] %hs %{+Q}r"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a1f50b in the box below so that we can be sure you are a human.