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 ?
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.
Spring Boot provides a number of logback configurations that be included from your own configuration.
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.
Place the logback-spring. xml in root of your project.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With