我通过使用过滤器做到了。现在大多数 linux 系统都使用 rsyslog。配置文件位于/etc/rsyslog.conf
和/etc/rsyslog.d
。
每当我运行命令logger -t SRI some message
时,我希望“一些消息”只显示在/var/log/sri.log
.
为此,我添加了/etc/rsyslog.d/00-sri.conf
具有以下内容的文件。
# Filter all messages whose tag starts with SRI
# Note that 'isequal, "SRI:"' or 'isequal "SRI"' will not work.
#
:syslogtag, startswith, "SRI" /var/log/sri.log
# The stop command prevents this message from getting processed any further.
# Thus the message will not show up in /var/log/messages.
#
& stop
然后重启 rsyslogd 服务:
systemctl restart rsyslog.service
这是一个显示结果的 shell 会话:
[root@rpm-server html]# logger -t SRI Hello World!
[root@rpm-server html]# cat /var/log/sri.log
Jun 5 10:33:01 rpm-server SRI[11785]: Hello World!
[root@rpm-server html]#
[root@rpm-server html]# # see that nothing shows up in /var/log/messages
[root@rpm-server html]# tail -10 /var/log/messages | grep SRI
[root@rpm-server html]#