至于为什么开启日志记录,即使我不说,你们这群老司机也懂。

开启日志分割,主要涉及到两个知识点:

    ①HAproxy关于日志记录的配置 

    ②Rsyslog的配置


环境:

        CentOS6.5

        haproxy-1.6.11


配置日志记录的前提是HAproxy可以正常使用,操作者熟悉Haproxy的配置文件


配置HAproxy的日志记录选项

[root@node1 haproxy]# vim /etc/haproxy/haproxy.cfg
defaults
        log global                        ##开启全局日志,对所有池子生效
        log 127.0.0.1:514 local3          ##将3级别日志信息发送到本机514端口的rsyslog程序
 :wq!

创建Haproxy日志存储的文件

[root@node1 haproxy]# mkdir -p /var/log/haproxy/
[root@node1 haproxy]# touch /var/log/haproxy/haproxy.log

配置Rsyslog配置,开启514端口的侦听

[root@node1 haproxy]# vim /etc/rsyslog.conf
$ModLoad imudp           #默认是注释,将注释去掉
$UDPServerRun 514        #默认是注释,将注释去掉
local3.* /var/log/haproxy/haproxy.log       ##添加一行该信息,该信息的local3要和ha里面的相对应
                                            ##后面的便是你希望haproxy的日志存储到哪里,指定文件

检查rsyslog是否开启监听

[root@node1 haproxy]# netstat -tunlp|grep 514
udp        0      0 0.0.0.0:514                 0.0.0.0:*                               2615/rsyslogd       
udp        0      0 :::514                      :::*                                    2615/rsyslogd

重启两个服务服务

[root@node1 haproxy]# service rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]
[root@node1 haproxy]# service haproxy restart
Shutting down haproxy:                                     [  OK  ]
Starting haproxy:                                          [  OK  ]

测试日志是否开始记录

[root@node1 haproxy]# tail -f /var/log/haproxy/haproxy.log 
Sep 26 10:56:40 localhost haproxy[2561]: Stopping proxy stats in 0 ms.
Sep 26 10:56:40 localhost haproxy[2561]: Stopping frontend dev_posp in 0 ms.
Sep 26 10:56:40 localhost haproxy[2561]: Stopping backend dev_posp in 0 ms.
Sep 26 10:56:40 localhost haproxy[2561]: Proxy stats stopped (FE: 0 conns, BE: 0 conns).
Sep 26 10:56:40 localhost haproxy[2561]: Proxy dev_posp stopped (FE: 6 conns, BE: 0 conns).
Sep 26 10:56:40 localhost haproxy[2561]: Proxy dev_posp stopped (FE: 0 conns, BE: 6 conns).
Sep 26 10:56:40 localhost haproxy[2635]: Proxy stats started.
Sep 26 10:56:40 localhost haproxy[2635]: Proxy dev_posp started.
Sep 26 10:56:40 localhost haproxy[2635]: Proxy dev_posp started.
Sep 26 10:57:01 localhost haproxy[2636]: Connect from 192.168.222.1:57057 to 192.168.222.130:80 (dev_posp/TCP)
Sep 26 10:57:01 localhost haproxy[2636]: Connect from 192.168.222.1:57058 to 192.168.222.130:80 (dev_posp/TCP)


技巧根据不同的需求来打印不同的日志级别

日志级别有local0~local7

       #define KERN_EMERG    "<0>"  /* system is unusable               */
       #define KERN_ALERT    "<1>"  /* action must be taken immediately */
       #define KERN_CRIT     "<2>"  /* critical conditions              */
       #define KERN_ERR      "<3>"  /* error conditions                 */
       #define KERN_WARNING  "<4>"  /* warning conditions               */
       #define KERN_NOTICE   "<5>"  /* normal but significant condition */
       #define KERN_INFO     "<6>"  /* informational                    */
       #define KERN_DEBUG    "<7>"  /* debug-level messages             */