Wednesday, January 29, 2014

How to enable logging for HAProxy in Linux

I am using Ubuntu12.04LTS for the installation, I assume that HAProxy is installed on system if not then do it from here

I follow the below step to setting up the logging:

Step1. In Global Section of HAProxy config file (haproxy.cfg) but the below

log 127.9.9.1 local0

Step2.  Create config file in /etc/rsyslog.d

$ sudo vim /etc/rsyslog.d/haproxy.conf

$ModLoad imudp
$UDPServerRun 514 
$template Haproxy,"%msg%\n"
local0.=info -/var/log/haproxy/haproxy.log;Haproxy
local0.notice -/var/log/haproxy/haproxy-status.log;Haproxy
### keep logs in localhost ##
local0.* ~ 

Step3.  Restart the HAProxy Server

$ sudo /etc/init.d/haproxy restart

Step 4: Now create the log rotate file for HAProxy, this keep on rotating and compress file itself. If this is not present in /etc/logrotate.d directory then create it as below:

$ sudo vim /etc/logrotate.d/haproxy

/var/log/haproxy/haproxy.log {
    missingok
    notifempty
    sharedscripts
    rotate 120
    daily
    compress
    postrotate
        reload rsyslog >/dev/null 2>&1 || true
    endscript

}

Step5. Restart the syslog

$ sudo restart rsyslog

you are done, check the HAProxy logs:

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

Note:
120 are the days that this will overwrite after 120 days.
local0.=info -/var/log/haproxy.log defines the http log will be saved in haproxy.log
local0.notice -/var/log/haproxy-status.log defines the Server status like start,stop,restart,down,up etc. will be saved in haproxy-status.log

UDPServerRun 514 means opening UDP port no. 514 to listen haproxy messages