At Transloadit, we use HAProxy "The Reliable, High Performance TCP/HTTP Load Balancer", so that we can offer different services on one port. For instance, depending on the hostname, a request to port 80 can be routed to either Node.js (in case of or Nginx (in case of HAProxy has been good to us and setting it up was a breeze. But getting HAProxy to log on Ubuntu Lucid has proven harder than I thought. All of the tutorials I found either didn't cover logging or contained deprecated information. Google suddenly stopped being my friend.

HAProxy Wants to Log

For performance and maintenance reasons, HAProxy doesn't log directly to files. Instead, it wants to log to a syslog server. This is a separate Linux daemon with which most servers are already equipped. HAProxy, however, requires it to listen on UDP port 514 and that is usually not enabled.

A syslog server receives log entries, decides which ones are interesting and writes those to disk in a highly optimized manner. These aspect can all be configured to your liking.

If we look at the top of your current /etc/haproxy/haproxy.cfg file, we may find something like:

maxconn 10000
ulimit-n 65536
log local0
log local1 notice

As you can see, is where it will try to find a syslog server to log to. On Ubuntu Lucid, the default syslog daemon is rsyslogd, so let's make that one accept HAProxy log entries.

Rsyslogd Welcomes HAProxy

The majority of Google results that cover logging with HAProxy told me to change the /etc/default/rsyslog file, but that file is now completely ignored under the new upstart system. Moreover, even if you make HAProxy adhere to the default file (yep, I tried), it will force rsyslogd into compatibility mode. That is not only a shame, but also unnecessary as it turns out.

By using the following config lines:

$ModLoad imudp
$UDPServerRun 514

rsyslogd will open up its UDP port.

Where to put these lines, you say? Well, if you are only using the UDP syslog port for the HAProxy service, you can put/uncomment the lot in just one /etc/rsyslog.d/haproxy.conf file:

# .. otherwise consider putting these two in /etc/rsyslog.conf instead:
$ModLoad imudp
$UDPServerRun 514

# ..and in any case, put these two in /etc/rsyslog.d/haproxy.conf:
local0.* -/var/log/haproxy_0.log
local1.* -/var/log/haproxy_1.log

Now do a quick:

restart rsyslog

And you're done. Check for HAProxy logs in:

tail -f /var/log/haproxy*.log

And don't forget to tweak the debug level in /etc/haproxy/haproxy.cfg.

Happy logging!