Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take logback.xml to outside of the jar

Tags:

java

logback

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?

like image 676
Lasitha Yapa Avatar asked Jun 20 '18 11:06

Lasitha Yapa


People also ask

How do I use external Logback xml?

logback. xml is ment to be loaded from external resources as a standalone file, so sysadmin can change logging settings without recompiling the code. LoggerFactory automaticly picks up any logback. xml file from the classpath, so in order to use external file, add its containing directory to a runtime classpath.

Where should the Logback xml be?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

Where should I put Logback Spring xml?

By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback. xml for Logback), but you can set the location of the config file by using the "logging. config" property.

How do I put 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.


1 Answers

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")
}
like image 71
Water Avatar answered Sep 19 '22 17:09

Water