Search code examples
spring-bootlogback

Why does the Spring application save logs in two places?


When the application is started (spring-boot), the application saves the logs from the console in two places. How to change it?

This is a multi-module application, and this is the configuration of one of the modules.

pom.xml:

...
<properties>
    <logs.path>${basedir}/logs</logs.path>
</properties>
...

application.yml:

...
logging.path: @logs.path@
...

logback.xml:

<configuration>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <charset>utf-8</charset>
        <pattern>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %magenta([%thread]) %logger{36}.%M: %msg%n
            </pattern>
        </pattern>
    </encoder>
</appender>

<appender name="CONSOLE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH}/console.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/archived/console/console.%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>90</maxHistory>
    </rollingPolicy>
    <encoder>
        <charset>utf-8</charset>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36}.%M: %msg%n</pattern>
    </encoder>
</appender>

<root level="info">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="CONSOLE_FILE"/>
</root>

</configuration>

When I run the application logs are saved in the following locations:

modules/LOG_PATH_IS_UNDEFINED/console.log

and

modules/singleModule/logs/console.log

How to fix it.


Solution

  • I added the tag, where I specified the application.yml file. It helped:

    <configuration>
    
    <property resource="application.yml" />
    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>utf-8</charset>
            <pattern>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %magenta([%thread]) %logger{36}.%M: %msg%n
                </pattern>
            </pattern>
        </encoder>
    </appender>
    
    <appender name="CONSOLE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logging.path}/console.log</file>
    ...
    

    The application.yml and logback.xml files are located in the resources folder.