利用syslog-ng推送和接收程序日志
个人认为,在Linux环境下,syslog-ng的配置文件比rsyslog更加简单易懂,而且对于跨平台的日志传输也具备更好的兼容性。
安装程序:
yum install -y syslog-ng
在启动syslog-ng之前,记得先停掉rsyslog并关闭开机启动
systemctl stop rsyslog.service systemctl disable rsyslog.service
发送日志
编写配置文件,推送日志
/etc/syslog-ng/syslog-ng.conf
# 定义第一个文件的目的地 destination d_remote_syslog1 { syslog("42.193.48.240" transport("tcp") port(514)); }; # 定义第一个文件的源 source s_file1 { file("/home/steam/squad_server/SquadGame/Saved/Logs/SquadGame.log" follow-freq(1) flags(no-parse)); }; # 为第一个文件添加标记 rewrite r_tag1 { set("Soi_BCTC#0", value("PROGRAM")); }; # 定义日志路径 log { source(s_file1); rewrite(r_tag1); destination(d_remote_syslog1); }; #第二份日志,不同标记发往不同目标 ,注意修改标记 s_file2; d_remote_syslog2; r_tag2; source s_file2 { file("/home/steam/squad_server1/SquadGame/Saved/Logs/SquadGame.log" follow-freq(1) flags(no-parse)); }; destination d_remote_syslog2 { syslog("42.193.48.240" transport("tcp") port(514)); # 若发送到不同地址,更改 IP }; rewrite r_tag2 { set("Soi_BCTC#1", value("PROGRAM")); }; log { source(s_file2); rewrite(r_tag2); destination(d_remote_syslog2); };
如果发送地址相同,则应该直接使用destination(d_remote_syslog1);
destination d_remote_syslog1 { syslog("192.168.1.1" transport("tcp") port(514)); }; destination d_remote_syslog2 { syslog("192.168.1.2" transport("tcp") port(514)); }; #设置日志路径 source s_file1 { file("/home/steam/squad_server1/SquadGame/Saved/Logs/SquadGame.log" follow-freq(1) flags(no-parse)); }; source s_file2 { file("/home/steam/squad_server2/SquadGame/Saved/Logs/SquadGame.log" follow-freq(1) flags(no-parse)); }; source s_file3 { file("/home/steam/squad_server3/SquadGame/Saved/Logs/SquadGame.log" follow-freq(1) flags(no-parse)); }; source s_file4 { file("/home/steam/squad_server4/SquadGame/Saved/Logs/SquadGame.log" follow-freq(1) flags(no-parse)); }; #设置标签 rewrite r_tag1 { set("Log#1", value("PROGRAM")); }; rewrite r_tag2 { set("Log#2", value("PROGRAM")); }; rewrite r_tag3 { set("Log#3", value("PROGRAM")); }; rewrite r_tag4 { set("Log#4", value("PROGRAM")); }; #设置发送的目标 log { source(s_file1); rewrite(r_tag1); destination(d_remote_syslog1); }; log { source(s_file2); rewrite(r_tag2); destination(d_remote_syslog1); }; log { source(s_file3); rewrite(r_tag3); destination(d_remote_syslog2); }; log { source(s_file4); rewrite(r_tag4); destination(d_remote_syslog2); };
接收日志
接收服务器的配置
/etc/syslog-ng/syslog-ng.conf
#设置日志来源(网络来源) source s_network { network( transport("tcp") port(514) ); # 对于 UDP, 使用: transport("udp") }; #情况1,如果存在标签 filter f_bctc1 { match("qingya1" value("MESSAGE")); }; filter f_soi_bctc_0 { match("Soi_BCTC#0" value("MESSAGE")); }; #情况2,如果不存在标签,则通过IP匹配 filter f_host1 { match("192.168.1.1" value("HOST")); }; filter f_host2 { match("192.168.1.2" value("HOST")); }; #设置存储路径 destination d_soi_0 { file("/SquadRemoteLog/OriginalLog/Soi_0.log"); }; destination d_bctc1 { file("/SquadRemoteLog/OriginalLog/qingya1.log"); }; destination d_host1 { file("/SquadRemoteLog/OriginalLog/192.168.1.1.log"); }; destination d_host2 { file("/SquadRemoteLog/OriginalLog/192.168.1.2.log"); }; #关联过滤器和目标路径 log { source(s_network); filter(f_bctc1); destination(d_bctc1); flags(final); }; log { source(s_network); filter(f_soi_bctc_0); destination(d_soi_0); flags(final); }; log { source(s_network); filter(f_host1); destination(d_host1); }; log { source(s_network); filter(f_host2); destination(d_host2); }; #如果没有标签和IP destination d_all_logs { file("/SquadRemoteLog/OriginalLog/All.log" template("$DATE $HOST $MSG\n") create-dirs(yes)); }; log { source(s_network); destination(d_all_logs); };
启动syslog-ng
启动并设置开机启动
systemctl start syslog-ng systemctl enable syslog-ng systemctl status syslog-ng
处理日志
处理收到的日志(选择性在以下关键字中添加标记)
awk -F'- - - ' '{print $2}' /path/to/your/logfile
发表评论