忧郁者 发表于 2019-1-1 14:43:32

【Haproxy】开启日志记录

  至于为什么开启日志记录,即使我不说,你们这群老司机也懂。
  开启日志分割,主要涉及到两个知识点:
  ①HAproxy关于日志记录的配置
  ②Rsyslog的配置

  

  环境:
  CentOS6.5
  haproxy-1.6.11
  

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

  配置HAproxy的日志记录选项
# vim /etc/haproxy/haproxy.cfg
defaults
      log global                        ##开启全局日志,对所有池子生效
      log 127.0.0.1:514 local3          ##将3级别日志信息发送到本机514端口的rsyslog程序
:wq!  创建Haproxy日志存储的文件
# mkdir -p /var/log/haproxy/
# touch /var/log/haproxy/haproxy.log  配置Rsyslog配置,开启514端口的侦听
# vim /etc/rsyslog.conf
$ModLoad imudp         #默认是注释,将注释去掉
$UDPServerRun 514      #默认是注释,将注释去掉
local3.* /var/log/haproxy/haproxy.log       ##添加一行该信息,该信息的local3要和ha里面的相对应
                                          ##后面的便是你希望haproxy的日志存储到哪里,指定文件  检查rsyslog是否开启监听
# netstat -tunlp|grep 514
udp      0      0 0.0.0.0:514               0.0.0.0:*                               2615/rsyslogd      
udp      0      0 :::514                      :::*                                    2615/rsyslogd  重启两个服务服务
# service rsyslog restart
Shutting down system logger:                              
Starting system logger:                                    
# service haproxy restart
Shutting down haproxy:                                    
Starting haproxy:                                            测试日志是否开始记录
# tail -f /var/log/haproxy/haproxy.log
Sep 26 10:56:40 localhost haproxy: Stopping proxy stats in 0 ms.
Sep 26 10:56:40 localhost haproxy: Stopping frontend dev_posp in 0 ms.
Sep 26 10:56:40 localhost haproxy: Stopping backend dev_posp in 0 ms.
Sep 26 10:56:40 localhost haproxy: Proxy stats stopped (FE: 0 conns, BE: 0 conns).
Sep 26 10:56:40 localhost haproxy: Proxy dev_posp stopped (FE: 6 conns, BE: 0 conns).
Sep 26 10:56:40 localhost haproxy: Proxy dev_posp stopped (FE: 0 conns, BE: 6 conns).
Sep 26 10:56:40 localhost haproxy: Proxy stats started.
Sep 26 10:56:40 localhost haproxy: Proxy dev_posp started.
Sep 26 10:56:40 localhost haproxy: Proxy dev_posp started.
Sep 26 10:57:01 localhost haproxy: Connect from 192.168.222.1:57057 to 192.168.222.130:80 (dev_posp/TCP)
Sep 26 10:57:01 localhost haproxy: Connect from 192.168.222.1:57058 to 192.168.222.130:80 (dev_posp/TCP)  

  技巧根据不同的需求来打印不同的日志级别
  日志级别有local0~local7
       #define KERN_EMERG    ""/* system is unusable               */
       #define KERN_ALERT    ""/* action must be taken immediately */
       #define KERN_CRIT   ""/* critical conditions            */
       #define KERN_ERR      ""/* error conditions               */
       #define KERN_WARNING""/* warning conditions               */
       #define KERN_NOTICE   ""/* normal but significant condition */
       #define KERN_INFO   ""/* informational                  */
       #define KERN_DEBUG    ""/* debug-level messages             */  




页: [1]
查看完整版本: 【Haproxy】开启日志记录