简体   繁体   中英

Log4j2 not logging when starting spring as linux service

I configured my spring application to write the logs into the file /logs/application.log, and store old logs into a folder with the respective date, eg. /logs/2021-05/20210503_1-application.log

############## log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%style{%d{yyyy-MM-dd'T'HH:mm:ss.SSS}} %highlight{%-5level}: %msg%n%throwable" />
        </Console>

        <RollingFile name="RollingFile"
                     fileName="./logs/application.log"
                     filePattern="./logs/$${date:yyyy-MM}/%d{yyyyMMdd}_%i-application.log">
        .....

When I'm starting the jar-File using the command java jar Application.jar the 'logs' folder gets created propperly. But when I'm starting the application as a service, the 'logs' folder does not get created nor any log file created (i tried already to search the system, there is nowhere a 'logs' folder).

My service configuration looks as follows (pi user should have access to his own home folder, also giving every user full access to 'logs' folder did not help: I'm running my spring application on linux as a service.

############ application.service
[Unit]
Description=Application webserver Daemon

[Service]
ExecStart=/bin/sh "java -jar /home/pi/application.jar"
User=pi

[Install]
WantedBy=multi-user.target

Also redirecting the logs to the folder did not work out (previously I used default logger from spring, redirecting the logs into a file, starting the application like ExecStart=/bin/sh "java -jar /home/pi/application.jar > application.log" , that worked just fine, no permission issues, etc.

Use WorkingDirectory in your application.service configuration:

[Service]
ExecStart=/bin/sh "java -jar /home/pi/application.jar"
WorkingDirectory=/home/pi <-- add this line
User=pi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM