简体   繁体   中英

Load logback.xml from Manifest Class-path

I'm trying to load logback.xml file from outside of my executable myapp.jar file.

The myapp.jar has a META-INF/MANIFEST.MF file roughly like this:

Manifest-Version: 1.0
Class-Path: logger-config lib/lib/jcl-over-slf4j-1.7.18.jar lib/slf4j-api-1.7.18.jar 
Main-Class: com.mycompany.MyAppMain

and resides in

my-app/
  lib/
    jcl-over-slf4j-1.7.18.jar
    slf4j-api-1.7.18.jar 
  logger-config/
    logback.xml
  myapp.jar

(there is a lot of other jar dependencies, cut those out).

Now, if I run the application using java -jar myapp.jar , it starts OK, but it does not pick up the logback.xml file .

From their docs:

If no such file is found, it checks for the file logback.xml in the classpath..

( http://logback.qos.ch/manual/configuration.html )

I know I can override this using logback.configurationFile , but is there a way to just put the XML file to classpath so that logback loads it automatically? It seems to work only if I package the logback.xml to myapp.jar (placing it in src/main/resources dir), but then the config file will be propagated to other JARs that uses myapp.jar as a dependency.

Maybe you just cut it out, but your classpath does not specify the logback-classic dependency. You will have to make sure logback appears before slf4j in your class-path definition. Also, note the trailing slash for the logger-config path - logback will not find your logback.xml without it (see here for details).

Something like this should work:

Class-Path: lib/logback-classic-1.1.6.jar lib/logback-core-1.1.6.jar lib/jcl-over-slf4j-1.7.18.jar lib/slf4j-api-1.7.18.jar logger-config/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM