Enable Logging in Haproxy

0
2672

HAProxy can emit log message for processing by a syslog server. This is compatible with familiar syslog tools like Rsyslog, as well as the newer systemd service journald. You can also utilize various log forwarders like Logstash and Fluentd to receive Syslog messages from HAProxy and ship them to a central log aggregator.

If you are working in a container environment, HAProxy supports Cloud Native Logging which allows you to send the log messages to stdout and stderr. In that case, skip to the next section where you’ll see how.

vi /etc/rsyslog.d/haproxy.conf
# Collect log with UDP
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514

# Creating separate log files based on the severity
local0.* /var/log/haproxy-traffic.log
local0.notice /var/log/haproxy-admin.log

Now in haproxy.cfg add log 127.0.0.1:514 local0 under global

vi /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1:514 local0

Save and restart haproxy

systemctl restart haproxy
[root@haproxy ~]# tail -f /var/log/haproxy-traffic.log
Jul  6 03:49:10 localhost haproxy[11182]: 176.31.20.253:43664 [06/Jul/2020:03:49:10.215] https~ app-main/nginx3 0/0/2/26/28 200 379 - - ---- 1/1/0/0/0 0/0 "POST /wp-cron.php?doing_wp_cron=1594021749.8861470222473144531250 HTTP/1.1"
Jul  6 03:49:10 localhost haproxy[11181]: 196.207.127.213:34788 [06/Jul/2020:03:49:09.067] https~ app-main/nginx1 0/0/5/1317/1322 200 7274 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jul  6 03:49:15 localhost haproxy[11180]: 196.207.127.213:43138 [06/Jul/2020:03:49:15.301] https~ app-main/nginx1 0/0/5/247/252 200 8140 - - ---- 1/1/0/0/0 0/0 "GET /hello-world/ HTTP/1.1"
Jul  6 03:49:19 localhost haproxy[11182]: 196.207.127.213:48570 [06/Jul/2020:03:49:19.747] https~ app-main/nginx1 0/0/4/165/169 200 7274 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jul  6 03:49:28 localhost haproxy[11182]: 196.207.127.213:57796 [06/Jul/2020:03:49:26.572] https~ app-main/nginx2 0/0/1/1750/1751 200 5911 - - ---- 1/1/0/0/0 0/0 "GET /category/uncategorized/ HTTP/1.1"
Jul  6 03:49:31 localhost haproxy[11182]: 196.207.127.213:61352 [06/Jul/2020:03:49:29.382] https~ app-main/nginx3 1/0/1/1694/1696 200 5911 - - ---- 2/2/0/0/0 0/0 "GET /category/uncategorized/ HTTP/1.1"
Jul  6 03:49:34 localhost haproxy[11181]: 196.207.127.213:9496 [06/Jul/2020:03:49:32.901] https~ app-main/nginx2 0/0/1/1646/1647 200 7343 - - ---- 1/1/0/0/0 0/0 "GET /2020/06/ HTTP/1.1"