一个rsyslog配置的例子

2020-02-01  本文已影响0人  CodingCode

一个rsyslog配置的例子

  1. 定义模版

定义log的内容:

$template contentTemplate,"%syslogpriority%,%syslogfacility%,%timegenerated::fulltime%,%HOSTNAME%,%syslogtag%,%msg%\n"

定义log输出文件:

$template rawlogfile,"/var/logs/my.log"
$template fmtlogfile,"/var/logs/%programname:F,46:1%.log"

重定向log:

# redirect local6 and discard
local6.*  /var/logs/my.log;contentTemplate
& ~
local6.*  /var/logs/my.log;contentTemplate
local6.*  /var/logs/my2.log;contentTemplate
& ~

上述把facility为local6的日志同时输出到/var/logs/my.log和/var/logs/my2.log,而:

local6.*  /var/logs/my.log;contentTemplate
& ~
local6.*  /var/logs/my2.log;contentTemplate

就不会输出到/var/logs/my2.log,因为& ~表示终止,后面的不再处理了。

使用另外一种方式:

$template rawlogfile,"/var/logs/my.log"
$template fmtlogfile,"/var/logs/%programname:F,46:1%.log"

if $syslogfacility-text == 'local6' then {
   action (type="omfile" dynaFile="rawlogfile" template="contentTemplate" dirCreateMode="0755" fileCreateMode="0644")
   action (type="omfile" dynaFile="fmtlogfile" template="contentTemplate" dirCreateMode="0755" fileCreateMode="0644")
   stop
}
  1. 模版操作
$template fmtlogfile,"/var/logs/%programname:F,46:1%.log"

这个语法%programname:F,46:1%
-. programname是内置变量,几个常用变量:

msg           :日志内容
hostname      : 主机名
timegenerated : 时间戳 rsyslog收到的时间
syslogtag     : tag域,像前面我们用到的local6
programname   : 程序名,即谁输出的日志

-.F,46:1是把programname按照‘-’(ascii 46)分割成多个域,然后取第一个域的值

  1. 比较操作

字符串相等:

if $syslogfacility-text == 'local6' then {}

字符串包含子串:

if $msg contains 'error' then {}

字符串开始:

if $msg startswith 'DEVNAME' then {}

规则表达式匹配:

if re_match($programname, "-app[0-9]+.[0-9]+.") then {}

逻辑与和或:

if expr1 or expr2 then {}
if expr1 and expr2 then {}
if expr1 and ( expr2 or expr3) then {}

逻辑非:

if not (expr1) then {}

另外这个链接文章整理的不错写的可以参考:
https://www.cnblogs.com/bugutian/p/6395085.html

上一篇 下一篇

猜你喜欢

热点阅读