haproxy json 自定义格式日志 并用rsyslog 收集

haproxy的配置

日志投送:

global
    log    127.0.0.1     local1 info

默认端口是udp514

 

默认日志:

defaults
    log global
    log-format \"local_time\":\"%t\",\"log_type\":\"tcp_log\",\"frontend_name\":\"%f\",\"hostname\":\"%H\",\"clientip\":\"%ci\",\"backend_name\":\"%b\",\"server_name\":\"%s\",\"server_ip\":\"%si\"

这部分的日志字段不包含http的信息防止tcp mode报错

 

http日志配置:

haproxy的日志跟nginx日志有所同,有的header信息并不能直接配到日志里面

haproxy提供了一个capture方法可以捕捉一些信息,可以用capture捕获的信息作为log-format的字段

#声明一个槽并捕获数据

frontend public
    capture request header Host len 64
    capture request header User-Agent len 64
    capture request header X-Forwarded-For len 64
    log-format \"local_time\":\"%t\",\"log_type\":\"http_log\",\"frontend_name\":\"%f\",\"hostname\":\"%H\",\"clientip\":\"%ci\",\"backend_name\":\"%b\",\"server_name\":\"%s\",\"server_ip\":\"%si\",\"http_version\":\"%HV\",\"http_mode\":\"%HM\",\"url\":\"%HP\",\"http_code\":\"%ST\",\"http_host\":\"%[capture.req.hdr(0)]\",\"agent\":\"%[capture.req.hdr(1)]\",\"forwarded_for\":\"%[capture.req.hdr(2)]\"

 

注意capture request header 只能配置在frontend部分

参考文档:

https://www.haproxy.com/documentation/hapee/1-8r1/traffic-management/log-format-rules/

https://www.haproxy.com/documentation/hapee/1-8r1/traffic-management/traffic-capture/

 

rsyslog的配置

准备环境:

docke pull rsyslog/syslog_appliance_alpine  #下载官方的docker镜像版本是8.x版本
wget https://github.com/rsyslog/rsyslog-docker/blob/master/appliance/alpine/rsyslog.conf #下载配置文件
vim Dockerfile  #把配置文件放到原始镜像里面
FROM    rsyslog/syslog_appliance_alpine
COPY    ./rsyslog.conf  /etc/rsyslog.conf

 启动容器

vim build.sh
registry_tag="haproxy-rsyslog"
name="haproxy-rsyslog"
docker build -t=$registry_tag .
docker stop $name
docker rm $name
docker run --restart=always -d --privileged=true -u root --name $name -p 514:514/udp -v /var/log:/var/log $registry_tag

./build.sh

每次修改rsyslog.conf只需要执行一下./build.sh 就行了

 

日志配置:

修改刚才下载的rsyslog.conf

使用自定义模板

template(name="FileFormat" type="string"
         string= "{\"log_source\":\"%syslogtag%\",%msg%}\n"
        )

haproxy日志已经包含了 %TIMESTAMP% %HOSTNAME% 所以把这两个字段去掉了

配置日志接收

local1.*  action(type="omfile" file="/var/log/haproxy/haproxy_all.log" template="FileFormat")

 

日志关键字过滤:

方法1 基本过滤

:msg, contains, "debug"  #只保留包含有debug的日志,使用!contains就是丢弃包含有debug的日志
local1.*  action(type="omfile" file="/var/log/haproxy/haproxy.log" template="FileFormat")

方法2 扩展过滤

if $syslogfacility-text == 'local1' and not ($msg contains 'debug') then action(type="omfile" file="/var/log/haproxy/haproxy.log" template="FileFormat") #丢弃包含有debug的日志
local1.*  action(type="omfile" file="/var/log/haproxy/haproxy.log" template="FileFormat")
if $msg contains 'debug' then /var/log/haproxy/haproxy_debug.log                          #把包含debug的日志单独保留 

 

参考文档:

https://www.rsyslog.com/doc/v8-stable/configuration/filters.html

转载于:https://www.cnblogs.com/37yan/p/10158496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值