how to redirect systemd logs in to the file

how to redirect systemd logs in to the file

Redirect systemd logs in file

Step 1:

Open service file and update following tags, in this tutorial we use tomcat service.

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=tomcat

vi  /lib/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINE_BASE=/opt/tomcat
Environment='CATALINE_OPTS=-Xms128M -Xmx765M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.haedless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=tomcat


[Install]
WantedBy=multi-user.target

Step 2:

Create file in rsyslog.d directory in any name

vi /etc/rsyslog.d/tomcat.conf

if $programname == 'tomcat' then /var/log/tomcat/file.log
& stop

Step 3:

Restart rsyslog and tomcat

systemctl  daemon-reloadsystemctl restart rsyslog tomcat

Step 4: 

Check log file
cat  /var/log/tomcat/file.log

Thanks,

Please share and like if this article is helpful 
 
Previous
Next Post »