Spring boot does not load logback-spring.xml
Asked Answered
M

8

34

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 ?

Mcgaha answered 29/5, 2018 at 4:53 Comment(8)
is it working if you have the file in the resource folder?Kalif
@Kalif yes it works when it is under resources folder.Mcgaha
Did you try renaming logback-spring.xml to logback.xml ?Megohm
Don't put the file next to JAR. Place the logback-spring.xml in root of your project. Do a 'bootJar' build, and execute the JAR. Does it work ?Zielsdorf
@Mcgaha - If it works fine when the config is under resources folder that validates your logback config is fine. Could you copy and paste the entire java command line how you startup this spring boot app? can you share your base.xml?Noddle
What you are saying is it works when you use "logging.config=logback-spring.xml", so what your are trying to do works right ? what else did u want ?And it works exactly as written in Spring doc so all is good to me : "By default Spring Boot picks up the native configuration from its default location for the system (e.g. classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property." docs.spring.io/spring-boot/docs/1.5.x/reference/html/…Kelt
Maybe you were expecting logback.xml is looked for just like application.properties is, but it does not : docs.spring.io/spring-boot/docs/current/reference/html/…Kelt
In my case I add the org.codehaus.janino:janino dependency follow the link docs.spring.io/spring-native/docs/current-SNAPSHOT/reference/… It worked for me!Undo
C
26

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.

Chastitychasuble answered 4/6, 2018 at 13:6 Comment(2)
origin source ?Celebrated
This is when you want to setup logback.xml in a native logback way : logback.qos.ch/manual/configuration.html#configFileProperty but if you want to do it the spring boot way, you can just set a logging.config with a relative path (logging.config=relative/path/logback.xml) in application.properties or in "java -jar"Kelt
P
11

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

Psychomotor answered 6/6, 2018 at 7:44 Comment(0)
M
3

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>
Mullet answered 4/6, 2018 at 11:2 Comment(1)
Work for me. ThanksCourtneycourtrai
C
3

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)

Chafe answered 27/10, 2019 at 22:31 Comment(0)
M
2

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.

Megohm answered 1/6, 2018 at 7:26 Comment(1)
same problem: I found out that the logback-spring.xml is not in the classpath, but I have no idea whySubmerse
C
1

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.

Contributor answered 4/6, 2018 at 14:56 Comment(1)
could u plz share app.properties & pom.xml ? Or share github link with smpleChouinard
H
0

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>
Holy answered 12/3, 2024 at 4:58 Comment(0)
E
0

Add spring.profiles.active =dev it works

Electrophilic answered 15/7, 2024 at 11:31 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Croom

© 2022 - 2025 — McMap. All rights reserved.