示例 2:将程序消息重定向到特定日志文件而不是消息

我们有如下所示的消息,我们希望将其重定向到特定文件,在本例中为 /var/log/custom_app.log:

June 4 22:20:21 onitroad appname: [804617.902850] this is a test message to discard

将如下所示的规则添加到 /etc/rsyslog.conf 文件中:

:programname, isequal, "appname" /var/log/custom_app.log
:programname, isequal, "appname" ~

更新 rsyslog.conf 文件后重新启动 rsyslog 服务。

# service rsyslog restart        # CentOS/RHEL 6
# systemctl restart rsyslog      # CentOS/RHEL 7
查看更多教程 https://on  itroad.com

示例 1:丢弃(抑制)特定消息

我们有如下所示的要丢弃或者禁止的消息:

June 4 22:20:21 onitroad app: [804617.902850] this is a test message to discard

将如下所示的规则添加到 /etc/rsyslog.conf 文件中:

# vi /etc/rsyslog.conf
:msg, contains, "test message to discard"    ~

更新 rsyslog.conf 文件后重新启动 rsyslog 服务。

# service rsyslog restart        # CentOS/RHEL 6
# systemctl restart rsyslog      # CentOS/RHEL 7
/etc/rsyslog.conf - 设置过滤器以丢弃或者重定向消息

本文说明了创建基于属性的过滤器以丢弃(抑制)特定消息或者将程序消息重定向到特定日志文件的步骤。

语法

编写基于属性的过滤器的 systax 如下所示:

:[Available Properties], [compare-operations], [customized expression] [path/log file]

从 rsyslog.conf 的手册页

丢弃操作 (~)

如果执行丢弃操作,则立即丢弃接收到的消息。
丢弃只是单个波浪号字符,没有其他参数。

例子:

*.*   ~      # discards everything.

重启 rsyslog

更新rsyslog.conf后需要重启rsyslog服务

# service rsyslog restart        # CentOS/RHEL 6
# systemctl restart rsyslog      # CentOS/RHEL 7
日期:2020-09-17 00:14:58 来源:oir作者:oir