Search code examples
javaspring-bootlogginglogbackslf4j

Double log files in spring boot application


I have a spring boot application, called service1, which uses the following hierarchy of property files to configure the logs:

bootstrap.yml has:

  • spring.application.name = service1

application.yml has:

  • logging.file: /app/logs/${spring.application.name}.log
  • logging.level.root: INFO
  • logging.level.com.myproject.api: TRACE

The log framework used is Slf4j, which is injected into every class by using Lombok's @Sfl4j annotation. spring boot claims to have default support for this, which it does, but what I see in /app/logs when the application starts are two logfiles:

  • /app/logs/myservice1.log : this file has the correct location and name, but the loglevel is not trace (only info)
  • /app/logs/myservice1-service.log: this file has the wrong name (where does the -service come from?) but it has the correct loglevel. I noticed that the name of this file matches the name of the directory in Git where this code resides, and it is also the name of the module of this piece of code in IntelliJ. I cannot explain how this name can be "picked up" by spring and somehow be used for a log filename when running the compiled code?!

I used the following declaration in my pom file to get spring boot, and I suspect perhaps some default logback stuff is also creating a logfile in addition to the Sfl4j I declared. Is there any way I can have only one logfile, with the name I specifiede and the correct loglevel? Perhaps I need to exclude a dependency?

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

Solution

  • add the folowing to properties file

    logging.config=logback.xml
    

    Then create a logback.xml file. Here you can place any config you like, for example:

    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration scan="true">
    
    <!-- Logging to system file -->
    <appender name="system-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    
        <!-- output filename -->
        <file>logs/app.log</file>
    
    
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>logs/app_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>500MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
        <pattern>%d{dd-MM-yyyy HH:mm:ss} - %c [%t] %-5p - %m%n</pattern>
        </encoder>
    </appender>
    
    
    <!-- overide levels for specific loggers-->
    <logger name="com.myproject.api" level="TRACE" />
    
    
    <root level="INFO">
        <appender-ref ref="system-file"/>
    </root>
    </configuration>