rsyslog
https://www.rsyslog.com/ https://www.rsyslog.com/doc/configuration/modules/omrabbitmq.html https://github.com/rsyslog/rsyslog-docker/tree/master/dev_env/ubuntu/base/22.04
测试
const net = require('net');
function writeLog(port,host,logInfo)
{
var client = new net.Socket();
return new Promise((resolve,reject)=>{
client.connect(port, host, () => {
client.end(logInfo);
client.destroy();
resolve(true);
});
client.once('error', (err) => {
console.log(err);
reject(err);
});
});
}
// 默认是在/var/log/message文件中
// tail -f /var/log/message
writeLog(514,'127.0.0.1','hello!rsyslog!')
// 日志内容:hello!rsyslog!
// <142>怎么来的
// 因为http://local1.info对应的Facility是17,Severity是6
// 而PRI计算规则是Facility乘8+6即是17乘8+6=142
// TIME:Jul 26 21:02:30 就是时间
// IP或主机名 127.0.0.1 就是ip,你也可以填写你的主机名如 zsComputer
// myApp[0]: 这里myApp就是我们的应用名称(后面可以根据这个做日志拆分),0代表的是该应用的错误编码,如果是错误则大于0
writeLog(514,'127.0.0.1','<142>Jul 26 21:02:30 127.0.0.1 myApp[0]: hello!local1.info!')
// 日志内容:Jul 26 21:02:30 127.0.0.1 hello!local1.info!
变量
# 系统变量
msg,rawmsg,hostname,source,fromhost,fromhost-ip,syslogtag,programname,pri-text,iut,syslogfacility,syslogfacility-text,syslogseverity,syslogseverity-text,syslogpriority,syslogpriority-text,timegenerated,timereported,timestamp,protocol-version,structured-data,app-name,procid,msgid,inputname
# 系统常量
$bom,$now,$year,$month,$day,$hour,$hhour,$qhour,$minute,$myhostname
$template myFormat,"/var/log/%programname%/%$year%%$month%%$day%.log"
*.* -?myFormat #本机测试使用
#-?myFormat: 这是日志消息的处理动作。
#-: 这个符号表示异步写入日志文件。异步写入可以提高性能,因为 rsyslog 不会等待日志写入完成。
#?: 这个符号表示使用之前定义的模板来生成日志文件的路径和名称。
规则类型
local1.info /var/log/local1.info.log #别忘了增加配置要重启服务哦
这里的Facility就是local1,Severity就是info
Facility和Severity有以下几种Facility:有0-23种设备
0 kernel messages
1 user-level messages
2 mail system
3 system daemons
4 security/authorization messages
5 messages generated internally by syslogd
6 line printer subsystem
7 network news subsystem
8 UUCP subsystem
9 clock daemon
10 security/authorization messages
11 FTP daemon
12 NTP subsystem
13 log audit
14 log alert
15 clock daemon
16-23 local0 - local7
Severity:日志等级
0 Emergency表示系统无法使用
1 Alert必须立即采取措施
2 Critical致命级别
3 Error错误级别
4 Warning警告级别
5 Notice 通知级别
6 Informational正常级别
7 Debug调试消息
none : 不记录任何东西
语法规则
*.info;mail.none;authpriv.none;cron.none /var/log/messages
# 这条规则,由4个选择器由分号( ; )隔开,他们之间是并集的关系,每个具体含义如下。
# *.info的表示匹配所有任何设备,但消息级别是info的消息;
# mail.none这个选择器的消息等级是none,意味着不会匹配mail类型的任何等级的消息。
# 同理:authpriv.none选择器,标识不会匹配authpriv类型的任何等级的消息。
# 同理:cron.none选择器,标识不会匹配cron类型的任何等级的消息。
# 那么,这条规则的意思就是:任何设备类型且仅当消息等级为info的消息会被写入action字段指向的/var/log/messages文件。
# .priority:表示大于等于priority级别的信息
# .=priority:表示等于priority级别的信息
# .!priority:表示在priority之外的等级的信息
news.* ~
# 使用波浪号(~)丢弃选定的消息。以上规则丢弃所有news设备产生的消息
kern.!emerge,kern.!crit,kern.!err /var/log/iptables.log
# 在优先级前加上感叹号(!)的优先级事件会被过滤掉,其他优先级的日志记录会被写入/var/log/iptables.log
案例
template(name="RoutingKeyTemplate" type="string" string="%syslogfacility-text%.%syslogseverity-text%")
template(name="rkTpl" type="string" string="%syslogtag%.%syslogfacility-text%.%syslogpriority-text%")
module(load="omrabbitmq")
# routing_key_template="RoutingKeyTemplate"
action(type="omrabbitmq"
host="localhost"
virtual_host="/"
user="guest"
password="guest"
routing_key="syslog.all"
body_template="rkTpl"
exchange="syslog")
if $programname startswith 'docker' then {
action(type="omfile" file="/var/log/errors.log")
}
# 判断
if $syslogseverity-text == 'error' then {
#asyncWriting参数 是一个二进制类型的参数,可以设置为on或off。如果设置为on,文件将以异步模式通过单独的线程进行写入。在这种情况下,会使#用双缓冲区,以便在一个缓冲区正在写入时,另一个缓冲区可以被填充。
#为了使flushInterval生效,asyncWriting必须设置为on,否则flushInterval将被忽略。
# flushInterval是一个整数类型的参数,单位为秒。它定义了未写入的数据被刷新的时间间隔。例如,如果设置为1(默认值),则表示每秒钟会检查是否有未写入的数据,如果有,就会将其刷新到输出文件中。
# 设置了非零值后,这个参数允许对输出文件设置大小限制。一旦文件大小达到这个限制,就会尝试对文件进行轮转(rotation)。例如,如果将rotation.sizeLimit设置为 10MB(假设单位是字节,10 * 1024 * 1024),当输出文件达到 10MB 大小时,系统会尝试进行文件轮转操作。
# 需要注意的是,这个大小限制不是精确的。允许有一些额外的字节,目的是防止消息被分割在两个文件中。而且,一批完整的消息不会在中间被截断。所以,在实际应用中,输出文件的大小可能会比配置的大小多出几 KiB。
# 同时,文档提醒不要配置过低的大小限制,特别是对于写入操作频繁的文件。因为调用文件轮转脚本会对性能有较大影响,可能会对整个rsyslog系统的性能产生负面影响。
action(type="omfile" flushInterval="5" asyncWriting="on" rotation.sizeLimit="10M" file="/var/log/tests.log")
# 不在匹配其他规则
stop
}
if not ($syslogseverity-text == 'error') then {
$template myTemplate,"/var/log/%fromhost-ip%/%$year%-%$month%-%$day%-%syslogtag%.log"
action(type="omfile" dynaFile="myTemplate")
}