Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 238
Tamerlane Avatar asked May 29 '18 04:05

Tamerlane


People also ask

How do I use Logback xml in spring boot?

To configure Logback for a Spring Boot project via XML, create the logback. xml or logback-spring. xml file under the src/main/resources folder. The configuration in XML file will override the logging properties in the application.

Does spring boot support Logback?

Spring Boot provides a number of logback configurations that be included from your own configuration.

How do you specify Logback xml in application properties?

xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.

Where should I put Logback-spring xml?

Place the logback-spring. xml in root of your project.


3 Answers

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.

like image 56
JohanB Avatar answered Oct 10 '22 15:10

JohanB


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

like image 30
Sangam Belose Avatar answered Oct 10 '22 16:10

Sangam Belose


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)

like image 24
alove0286 Avatar answered Oct 10 '22 14:10

alove0286