28

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 ?

Tamerlane
  • 2,031
  • 3
  • 21
  • 34
  • 1
    is it working if you have the file in the resource folder? – Patrick May 29 '18 at 06:12
  • @Patrick yes it works when it is under resources folder. – Tamerlane May 29 '18 at 12:34
  • 1
    Did you try renaming logback-spring.xml to logback.xml ? – Shubham Kadlag Jun 01 '18 at 05:48
  • 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 ? – soufrk Jun 04 '18 at 10:22
  • @Tamerlane - 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? – runwuf Jun 04 '18 at 16:42
  • 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." https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/howto-logging.html – Tristan Aug 09 '19 at 14:48
  • Maybe you were expecting logback.xml is looked for just like application.properties is, but it does not : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files – Tristan Aug 09 '19 at 14:52
  • In my case I add the org.codehaus.janino:janino dependency follow the link https://docs.spring.io/spring-native/docs/current-SNAPSHOT/reference/htmlsingle/#aot-build-setup-configuration It worked for me! – phancuongviet Mar 01 '23 at 12:35

6 Answers6

23

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.

JohanB
  • 2,068
  • 1
  • 15
  • 15
  • origin source ? – zhaoyou Apr 24 '19 at 08:05
  • 2
    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" – Tristan Aug 09 '19 at 14:42
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

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
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)

alove0286
  • 51
  • 4
2

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>
this_is_om_vm
  • 608
  • 5
  • 23
1

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.

Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
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.

Giang Phan
  • 534
  • 7
  • 15