rsyslog配置
rsyslog的配置文件为/etc/rsyslog.conf,但真正的配置放在/etc/rsyslog.d/目录下
# Default rules for rsyslog.
#
# For more information see rsyslog.conf(5) and /etc/rsyslog.conf
#
# First some standard log files. Log by facility.
#
auth,authpriv.* /var/log/auth.log
*.*;user.none;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
#daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
#lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log #设备类型.紧急程度
修改了rsyslog的配置文件后,需要重启服务来生效,命令为service rsyslog restart
代码示例
PHP代码示例:
<?php
openlog('USER-LOG', LOG_PID, LOG_USER);
syslog(LOG_ERR, 'ERROR!!!');
closelog();
运行结果:
$ cat /var/log/user.log
Mar 17 02:45:34 homestead USER-LOG[6540]: ERROR!!!
函数解释
rsyslog会根据openlog()的设备类型,和syslog()的紧急程度,来确定该日志信息被写入哪个文件
bool openlog ( string $ident , int $option , int $facility )
- $ident:日志的标识,将被添加到日志信息中(USER-LOG)
- $option:示例中的值为LOG_PID,因此pid进程号将被添加到每条日志信息中([6540])

- $facility:该参数指定记录信息的设备类型

bool syslog ( int $priority , string $message )
- 产生一条日志信息,并被rsyslog系统分发
- $priority:紧急程度

网友评论