Take logback.xml to outside of the jar
I am using logback with slf4j in my Maven Java project. Currently logback config file (logback.xml) is in src -> main -> resources folder. And it is working fine.
My issue is, I need to give my client the ability to configure logging as he prefers. For that logback.xml should be outside the jar when I build it. But as xml is inside src folder it is inside the jar and no one can change it after build.
How to achieve this?
4 Answers 4
Specifying the location of the default configuration file as a system property You may specify the location of the default configuration file with a system property named «logback.configurationFile». The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml -jar myapp.jar
This question and answer relate to logback configuration in general. This solution does not work for spring.
Logback config file location can be specified in application.properties or application.yml.
logging: config: logback-spring.xml
This allows you to place jar and log-back.xml at the same folder.
Please note that logback-spring.xml file in your project folder should not be included in your jar. This can be achieved setting on build.gradle or pom.xml.
This is what I was looking for! Thank you! By the way, if you’re using an application.properties file, then it’s logging.config=logback-spring.xml
The logback.xml file needs to be on the classpath, but it doesn’t need to be inside any specific jar. The details of how you want to do this depend on the exact deployment mechanism that’s being used: How does whatever’s starting this application set the classpath? Whatever that mechanism is, you should be able to configure it to include wherever you’re putting the logback.xml file, and then just don’t include in in the src/main/resources to be embedded in the jar file.
Depending on the complexity of what you’re going for, you may find the maven-assembly-plugin useful for creating your distribution of dependencies.
Using Scala SBT (1.2.1) on Windows:
@cd %~dp0 @set JAVA_OPTS=-Dlogback.configurationFile=logback.xml @sbt clean run
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.19.43539
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.