[英]Load logback.xml from Manifest Class-path
I'm trying to load logback.xml
file from outside of my executable myapp.jar
file. 我试图从可执行文件myapp.jar
文件外部加载logback.xml
文件。
The myapp.jar
has a META-INF/MANIFEST.MF
file roughly like this: myapp.jar
具有一个META-INF/MANIFEST.MF
文件,大致如下所示:
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). (还有很多其他jar依赖项,请切掉那些依赖项)。
Now, if I run the application using java -jar myapp.jar
, it starts OK, but it does not pick up the logback.xml file . 现在,如果我使用java -jar myapp.jar
运行该应用程序,它将启动OK,但不会拾取logback.xml文件 。
From their docs: 从他们的文档:
If no such file is found, it checks for the file logback.xml in the classpath.. 如果找不到这样的文件,它将在类路径中检查文件logback.xml。
( http://logback.qos.ch/manual/configuration.html ) ( 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? 我知道我可以使用logback.configurationFile
重写此方法,但是有没有一种方法可以将XML文件仅放在类路径中,以便logback自动加载它? 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. 仅当我将logback.xml
到myapp.jar
(将其放在src/main/resources
dir中)时,它才似乎起作用,但是配置文件将传播到使用myapp.jar
作为依赖项的其他JAR。
Maybe you just cut it out, but your classpath does not specify the logback-classic
dependency. 也许您只是删除了它,但您的类路径未指定logback-classic
依赖项。 You will have to make sure logback
appears before slf4j
in your class-path definition. 你必须确保logback
之前出现slf4j
在类路径定义。 Also, note the trailing slash for the logger-config
path - logback will not find your logback.xml
without it (see here for details). 另外,请注意logger-config
路径的末尾斜杠- logback.xml
没有它,logback将找不到您的logback.xml
(有关详细信息,请参见此处 )。
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.