简体   繁体   中英

Spring boot does not load logback-spring.xml

I have a sample Spring Boot application that uses Logback for logging. So I have logback-spring.xml next to the jar to configure the logging, however it does not work unless I specify it with logging.config , ex : logging.config=logback-spring.xml .

I have looked into Spring Boot ignoring logback-spring.xml where it suggests that it might be because there's already a spring.xml somewhere, but putting breakpoint on org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(LoggingInitializationContext, LogFile) shows that logFile is empty.

Am I doing something wrong here ?

By default, Spring will not look for resources outside the jar file. If you want to use an external logback configuration file, you must pass it's location when starting the jar:

$ java -jar -Dlogback.configurationFile=/full_path/logback.xml app.jar

Please, do not include the logback.xml into the final Jar file, it will cause multiple logback.xml files in the classpath.

As per the description of the problem, you are using the externalized version of your log configuration. The file is kept outside the jar. So you have to mention the path as run-time argument as below:

-Dlogging.config=file:logback-spring.xml

Or in mention the same property in application.properties as below:

logging.config=file:logback-spring.xml

The reason it pickup the file from resources folder, because it is configured in spring that way. Spring pick up the logback file by below names from classpath.

logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy

Please check the relevant docs at spring-boot custom log configuration

Just define these lines in your logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

Dockerfile:

COPY /rootProjectName/src/main/resources/logback-spring.xml /deployments/  

application-dev.properties:

logging.config=classpath:logback-spring.xml

I'm running a docker container and must copy over the resource folder into my deployments folder in my Docker File... but once copied over
this is the logging.config value that works for me (adding the classpath word)

There can be two reasons for such behaviour:

Reason 1: The logback-spring.xml is somehow not in the classpath. You can verify this at runtime by printing System.getProperty("java.class.path") and checking if the folder containing logback-spring.xml is present in the output.

Reason 2 : If Reason 1 is not the issue, then there is already a file named logback.xml or logback-spring.xml in the classpath and this may be causing conflict. Now here you have to find that file. You can try renaming logback-spring.xml to logback.xml and check the behaviour.

I don't know why it does not working for you. I have created a logback-spring.xml file in the resource folder and it worked fine.

Following is content of log file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <property name="LOGDIR" value="logs"></property>
    <property name="APP_NAME" value="spring-boot-sample"></property>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d ${APP_NAME} %-5level [%thread] %logger: %msg%n</Pattern>
        </layout>
    </appender>

    <appender name="ROLLINGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGDIR}/${APP_NAME}-log.%d{MM-dd-yyyy}.log</fileNamePattern>
            <maxHistory>90</maxHistory>
        </rollingPolicy>
        <encoder>
            <charset>utf-8</charset>
            <Pattern>%d ${APP_NAME} %-5level [%thread] %logger: %msg%n</Pattern>
        </encoder>
    </appender>

    <springProfile name="local">
        <root level="debug">
            <appender-ref ref="CONSOLE"/>
        </root>
        <logger name="co.jp.oha" additivity="false" level="debug">
            <appender-ref ref="ROLLINGFILE"/>
            <appender-ref ref="STDOUT"/>
        </logger>
    </springProfile>
</configuration>

You can try with them. I hope that it will help you.

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