Haproxy 开启日志方法

haproxy 开日日志方法

注:考虑性能等因素,建议使用在测试环境。

配置 haproxy

配置 /etc/haproxy/haproxy.cfg,添加如下:

global
    log 127.0.0.1 local2 [emerg|alert|crit|err|warning|notice|info|debug]

配置 rsyslog

配置 /etc/sysconfig/rsyslog,添加-r 参数:

# SYSLOGD_OPTIONS="" ## 修改前配置
SYSLOGD_OPTIONS="-c 2 -r -m 0"

SYSLOGD_OPTIONS 参数说明:

  • -c 2 使用兼容模式,默认是 -c 5
  • -r 开启远程日志
  • -m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能

配置 /etc/rsyslog.conf,取消注释:

$ModLoad imudp
$UDPServerRun 514

解除注释:

$IncludeConfig /etc/rsyslog.d/*.conf

创建新 haproxy 配置文件 /etc/rsyslog.d/haproxy.conf,内容如下:

vim /etc/rsyslog.d/haproxy.conf
# Save haproxy log
local2.*                                                /var/log/haproxy.log

重启服务

systemctl restart rsyslog.service
systemctl restart haproxy.service

上述方法,参考haproxy 默认配置文件haproxy.cfg,内容如下:

#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
 
...

参考

  • https://www.percona.com/blog/2014/10/03/haproxy-give-me-some-logs-on-centos-6-5/
  • http://jainpiyush95.blogspot.com/2016/01/enable-logging-in-haproxy.html

完毕。