Cloud Computing‎ > ‎Load Balancing‎ > ‎HAProxy‎ > ‎

Logging


rsyslog format

$template myFormat,"%timegenerated% %HOSTNAME% %syslogtag%%msg%\n"

$ActionFileDefaultTemplate myFormat
The %timegenerated% properly is what you are asking for. From man page

timegenerated timestamp when the message was RECEIVED. Always in high resolution
You can see all option with man rsyslog.conf.


Create haproxy syslog config

$ cat /etc/rsyslog.d/49-haproxy.conf

$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%msg%\n"
local1.info -/var/log/haproxy.log;Haproxy
local1.notice -/var/log/haproxy-status.log;Haproxy

### keep logs in localhost ##
local1.* stop

# & ~ or & stop means not to put what matched in the above line anywhere else for the rest of the rules

# http://serverfault.com/questions/214312/how-to-keep-haproxy-log-messages-out-of-var-log-syslog

# The & ~ means not to put what matched in the above line anywhere else for the rest of the rules.

# Worth noting that if you do this, your local0.* line must be processed before the "." line in /etc/rsyslog.d/50-default.conf. I created a file called /etc/rsyslog.d/haproxy.conf to contain my haproxy-specific logging config, but it logged to syslog despite having a "& ~" at the end. This is because (of course) 50-default.conf is loaded before haproxy.conf so the catchall "." was matched before my "local0.*" line. The solution was to rename my haproxy-specific file to /etc/rsyslog.d/49-haproxy.conf


Restart rsyslog service

$ sudo service rsyslog restart

haproxy config

global
    # log level : emerg  alert  crit   err    warning notice info  debug
    log         127.0.0.1 local1


Notes


You could also do the following which will make it so they don't go in any other logs: 

local0.* -/var/log/haproxy.log 
& ~ 

The & ~ or & stop means not to put what matched in the above line anywhere else for the rest of the rules.

Worth noting that if you do this, your local0.* line must be processed before the "." line in /etc/rsyslog.d/50-default.conf. I created a file called /etc/rsyslog.d/haproxy.conf to contain my haproxy-specific logging config, but it logged to syslog despite having a "& ~" at the end. This is because (of course) 50-default.conf is loaded before haproxy.conf so the catchall "." was matched before my "local0.*" line. The solution was to rename my haproxy-specific file to /etc/rsyslog.d/49-haproxy.conf 
 
FYI a small update rsyslogd-2307: warning: ~ action is deprecated, consider using the 'stop' statement instead [try http://www.rsyslog.com/e/2307 ] 
 
This discusses the use of & stop instead of & ~: rsyslog.com/doc/v8-stable/compatibility/v7compatibility.html.



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

$ tail -f /var/log/haproxy*.log
Don't forget to tweak the debug level in /etc/haproxy/haproxy.cfg, and maybe set up a logrotate right away in /etc/logrotate.d/haproxy:

/var/log/haproxy*.log
{
    rotate 4
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        reload rsyslog >/dev/null 2>&1 || true
    endscript
}


References:




Comments