haproxy配置日志及自定义日志

2023-05-14,,

日志级别:

    emerg 0 系统不可用
    alert 1 必须马上采取行动的事件
    crit 2 关键的事件
    err 3 错误事件
    warning 4 警告事件
    notice 5 普通但重要的事件
    info 6 有用的信息
    debug 7 调试信息

配置记录日志:

    在默认情况haproxy是不记录日志的;

    通过配置haproxy.cfg使其记录日志

    haproxy.cfg:开启haproxy日志记录

        log 127.0.0.1 local2
    /etc/rsyslog.conf:利用rsyslog接收日志:

        去掉这两行的注释:
            $ModLoad imudp    
            $UDPServerRun 514
        新增这一行:
            local2.* /var/log/haproxy.log

    重启rsyslog,查看udp的514端口是否开启:

        #/etc/init.d/rsyslog restart

        #netstat -tunpl

配置haproxy记录更详细的日志:

    默认的日志可读性很差,我们需要显示更多的信息,但默认的都捕获不到,只能自定义需要显示的内容了

    capture request header Host len 64
    capture request header User-Agent len 128
    capture request header X-Forwarded-For len 100
    capture request header Referer len 200
    capture response header Server len 40
    capture response header Server-ID len 40
    log-format %ci:%cp\ %si:%sp\ %B\ %U\ %ST\ %r\ %b\ %f\ %bi\ %hrl\ %hsl\

    重启haproxy即可

捕获请求头参考官网:http://cbonte.github.io/haproxy-dconv/1.7/configuration.html#8.8

自定义日志格式参考官网:http://cbonte.github.io/haproxy-dconv/1.7/configuration.html#8.2.4

《haproxy配置日志及自定义日志.doc》

下载本文的Word格式文档,以方便收藏与打印。