Write haproxy frontend and backend logs to local log files in linux server

Hi All,

I started working on haproxy while i am having doubt on how to write the haproxy frontend and backend logs into a local log files to know what logs are being sent through haproxy.

Can someone help me how to do that? Thanks.

Below is my sample haproxy configuration.


global
log localhost local0
daemon

defaults
log global
mode tcp
balance roundrobin
option tcplog
timeout connect 5s
timeout client 50s
timeout server 50s
retries 3
maxconn 15000

IN

#Accepts logs from TCP 1514 port
frontend server_in_1514
bind *:1514
use_backend b_server_in_1514
backend b_server_in_1514
option tcp-check
server :1514 check port 1514 send-proxy

Stat

listen stats
bind :9000
mode http
stats enable
stats hide-version
stats realm Haproxy\ Statistics
stats uri /haproxy_stats
stats auth admin:nimda

So, just to make sure I understand the question, you are asking how HAProxy can write its logs directly to a log file (on the file-system) without going through a SysLog server?

In that case, based on what I know from the documentation, at the moment this is not supported directly.

However you do have a few options:

  • if you use systemd (most likely, since most Linux distributions have switched to it), then you can configure HAProxy to use /dev/log (with for example log '/dev/log' len 65535 format 'rfc3164' daemon info err), and have that be picked-up by journald and accessible through journalctl -u haproxy;
  • you could configure HAProxy to log over to stdout (available in newer versions, check the manual for syntax), and have that redirected to a file in your startup script;
  • use a proper SysLog server like rsyslog, and filter and route the logs to proper files;

On the other hand, logging to files (not properly handled by a logging infrastructure) means that you’ll have to rotate the files yourserf.


That being said, if you want to get logging solely for debugging purposes, you could start HAProxy in the foreground and enable logging dump with haproxy -d; or if that is to verbose, use logging to stdout and start it manually in foreground with haproxy -db.

Thanks alot for your detailed answer.

Actually I am okay to use rsyslog as my server is already having installed and running rsyslog.

Could you please help with configuration on how to route haproxy to logs to any file using rsyslog ? Any example configuration would be much helpful. Thanks.