Spring Boot uses /tmp/spring.log file during testing
Asked Answered
G

3

15

One of my Spring Boot applications makes problems during its Maven test phase.

Both during testing and "regular" application runtime, the Spring Boot application uses a logback configuration file very similar to src/main/resources/logback-spring.xml. This configuration file (transitively) includes the logback configuration files base.xml and file-appender.xml. These configuration files set a logback property LOG_FILE=/tmp/spring.log.

I guess it is best practice that file /tmp/server.log is owned by user and group ${MY_SPRING_BOOT_APPLICATION}.

Jenkins runs as user jenkins. jenkins does not have write permissions for /tmp/server.log. Therefore the JUnit tests fail when executed by Jenkins.

  • What is the best way to configure logging so that it works well during a Jenkins build-with-tests and so that it sets up daily rolling logging when leveraging Spring Boot's SysV init.d service functionality (which puts logs into /var/log/)?
  • Will file /tmp/spring.log be modified (and therefore be broken) concurrently if there are two or more Spring Boot applications running at the same time?
Gallegos answered 28/6, 2016 at 16:5 Comment(0)
G
16

In my Spring Boot application, I have added <property name="LOG_TEMP" value="./logs"/> to src/test/resources/logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
    <property name="LOG_TEMP" value="./logs"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <logger name="com.example" level="INFO"/>


    <root level="WARN">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>

This way, during Maven testing, a separate logging file will be created in the current (testing) working directory.

Props to welcor for helping out.

Gallegos answered 20/9, 2016 at 18:10 Comment(1)
Make sure you put the property override before the <include > property, as demonstrated here.Lir
B
2

We were experiencing an issue where running multiple Spring Boot modules on the same server was causing multiple processes to try to write to /tmp/spring.log. We solved this by updating logback-spring.xml to include ${PID} in the log name:

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>

to

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring-${PID}.log}"/>

Related Spring docs: https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/howto-logging.html#howto-configure-logback-for-logging

Brewster answered 2/3, 2021 at 17:28 Comment(0)
I
0

If the permissions are insufficient, you can modify them using the chmod command:

 chmod +r /tmp/spring.log
Incredulity answered 21/2 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.