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,309
- 8
- 38
- 57
-
Great question, well written. – thonnor Jun 21 '23 at 09:00
4 Answers
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
From offcial docs

- 25,039
- 43
- 140
- 225

- 404
- 3
- 11
-
1if you want to do it through code https://stackoverflow.com/q/21885787/40570 – shabby Jan 14 '21 at 10:06
-
This question and answer relate to logback configuration in general. This solution does not work for spring. – Sridhar Jammalamadaka Oct 16 '21 at 04:27
Logback config file location can be specified in application.properties or application.yml.
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.
build.gradle
bootJar {
archiveName 'your-project.jar'
exclude("*.xml")
}

- 540
- 7
- 7
-
-
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` – Kira Resari Dec 15 '22 at 08:09
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:
Batch file:
@cd %~dp0
@set JAVA_OPTS=-Dlogback.configurationFile=logback.xml
@sbt clean run
worked for me (strange ...)

- 85
- 6