rsyslog: send logs to different file using custom template

caa*_*os0 2 rsyslog syslog systemd

So, I'm using systemd to send the logs to rsyslog, and I want to write the logs to /var/log/app.log.

Since it is a Java app with its own log format, I don't want to have timestamps or anything on it, just the message itself.

What I have so far is this:

[Unit]
Description=My App
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myapp
ExecStart=/usr/bin/java -jar /opt/app.jar
Restart=on-failure

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

And then I have thet /etc/rsyslog.d/myapp.conf file:

template(name="clean" type="string" string="%msg:2:$:drop-last-lf%\n")

if $programname == 'myapp' then action(type="omfile" file="/var/log/app.log" template="clean")
& stop
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,如果程序名称是myapp,它应该使用clean模板并将日志保存到/var/log/app.log然后停止。

这是有效的,除了它还将日志发送到/var/log/syslog.

如何防止将这些日志也保存到 syslog 中?

caa*_*os0 5

问题是调用了默认配置文件50-defaults.conf,因此首先对其进行了处理...

这意味着日志将/var/log/syslog根据默认配置发送到,然后它会通过我的配置文件,这会停止,但这并不重要,因为它之后没有任何内容。

重命名它以10-myapp.conf解决问题。