如果没有,如何告诉 rsyslog 创建日志文件?

fdu*_*uff 14 logs centos rsyslog

rsyslog 的默认行为是将跟踪附加到现有日志文件。

现在,我已经看到(CentOs,Scientific Linux),当 rsyslog 已经运行时,您删除日志文件(例如,专门用于记录应用程序中的日志跟踪的文件),然后运行您的应用程序,rsyslog不会创建日志文件并且不会记录任何痕迹。

是否有一个配置选项可以告诉 rsyslog 在附加跟踪之前创建一个日志文件?

注意:执行 aservice rsyslog restart将强制创建一个空的日志文件。

rsyslog.conf(未添加任何内容)

# rsyslog v5 configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog   # provides kernel logging support (previously done by rklogd)
#$ModLoad immark  # provides --MARK-- message capability

$SystemLogRateLimitInterval 1
$SystemLogRateLimitBurst 50000

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf


#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;local1.none    /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 *

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log

# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###
Run Code Online (Sandbox Code Playgroud)

cas*_*cas 13

从 rsyslog 的 POV 来看,删除的日志文件仍然存在。这是因为 rsyslog 没有写入文件名,而是写入它为日志文件打开的文件句柄。

在没有进程打开文件句柄之前,Unix 系统实际上不会删除文件。这意味着在关闭所有打开的文件句柄之前,不会释放被删除文件使用的磁盘空间。这也意味着任何具有已删除文件的打开文件句柄的进程都可以继续读取和/或写入文件。

向 rsyslog 发送 HUP 信号(例如,通过pkill -HUP rsyslog/etc/init.d/rsyslog rotate)告诉它关闭所有打开的文件,重新加载其配置文件,并重新打开所有日志文件以进行写入(必要时创建它们)。

重新启动 rsyslogd 也有效。

请注意,这是一个功能,而不是错误,具有一些有用的含义 - 例如,这就是为什么 rsyslog 即使在旋转(即重命名/mv-ed)后仍继续写入同一个日志文件的原因,直到 rsyslog 收到 HUP 信号。这意味着日志处理脚本和实用程序不必非常小心时间——它们只需轮换所有日志,向 rsyslog 发送 HUP,一切都会继续工作,而不会丢失日志数据。

顺便说一句,rsyslog 不会发生这种情况的唯一方法是在每次写入(或至少调用sync())时关闭并重新打开每个日志文件。性能会很糟糕。


slm*_*slm 3

$文件创建模式

这个选项是否没有达到您想要的效果,$FileCreateMode

摘抄

$FileCreateMode 0600

This sample lets rsyslog create files with read and write access only for the 
users it runs under.

The following sample is deemed to be a complete rsyslog.conf:

$umask 0000 # make sure nothing interferes with the following definitions
*.* /var/log/file-with-0644-default
$FileCreateMode 0600
*.* /var/log/file-with-0600
$FileCreateMode 0644

*.* /var/log/file-with-0644
Run Code Online (Sandbox Code Playgroud)

文件输出模块

根据 rsyslog 文档,文件输出模块的 File 参数可用于执行此操作。

摘录omfile 模块

文件

如果文件已存在,则会将新数据附加到其中。现有数据不会被截断。如果该文件尚不存在,则会创建该文件。只要 rsyslogd 处于活动状态,文件就会保持打开状态。这与外部日志文件轮换冲突。为了在轮换后关闭文件,请在文件轮换后向 rsyslogd 发送 HUP 信号。

向系统日志发送 HUP 信号

我认为最终您需要“触发” rsyslog 来执行此操作。我认为这不会自动达到您想要的效果。因此,您可以给它一个 HUP 信号,以在删除日志文件后触发日志文件的重新创建。

$ sudo pkill -HUP rsyslog
Run Code Online (Sandbox Code Playgroud)

这样做会在我的日志文件中创建以下消息/var/log/messages

Sep 26 15:16:17 grinchy rsyslogd: [origin software="rsyslogd" swVersion="4.6.3" x-pid="1245" x-info="http://www.rsyslog.com"] rsyslogd was HUPed, type 'lightweight'.
Sep 26 15:16:44 grinchy rsyslogd: [origin software="rsyslogd" swVersion="4.6.3" x-pid="1245" x-info="http://www.rsyslog.com"] rsyslogd was HUPed, type 'lightweight'.
Run Code Online (Sandbox Code Playgroud)