Tamerlane
Tamerlane

Reputation: 2103

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 ?

Upvotes: 35

Views: 64646

Answers (8)

Sai naik
Sai naik

Reputation: 1

Add spring.profiles.active =dev it works

Upvotes: 0

Parth Solanki
Parth Solanki

Reputation: 11

if not found logback-spring.xml file , please add below line in application.properties file : logging.config=classpath:logback-spring.xml

and this is working for me [logback-spring.xml file]:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <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>

Upvotes: 0

alove0286
alove0286

Reputation: 61

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)

Upvotes: 4

Sangam Belose
Sangam Belose

Reputation: 4506

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

Upvotes: 12

Giang Phan
Giang Phan

Reputation: 544

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.

Upvotes: 2

JohanB
JohanB

Reputation: 2148

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.

Upvotes: 27

this_is_om_vm
this_is_om_vm

Reputation: 636

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>

Upvotes: 5

Shubham Kadlag
Shubham Kadlag

Reputation: 2308

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.

Upvotes: 3

Related Questions