Java 通过 jvm 参数的 Logback 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19518843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:58:55  来源:igfitidea点击:

Logback configuration via jvm argument

javaloggingweb-applicationslogback

提问by Sai prateek

How can we load logback.xml via jvm argument if this is not present in project classpath?

如果项目类路径中不存在,我们如何通过 jvm 参数加载 logback.xml?

I'm using

我正在使用

 -Dlogback.configuration=file:C:\logbacs\logback.xml 

but this is not working.

但这不起作用。

采纳答案by Sai prateek

I found the solution

我找到了解决方案

-Dlogging.config="C:\logbacs\logback.xml"

is working fine for me.

对我来说工作正常。

Updated because the previous answer was obsolete

更新,因为以前的答案已过时

回答by Fre_d

The original answer to this question doesn't appear to be working anymore as it produces this warning instead:

这个问题的原始答案似乎不再起作用,因为它会产生此警告:

o.s.b.l.logback.LogbackLoggingSystem     : Ignoring 'logback.configurationFile' system property. Please use 'logging.config' instead.

Therefore, you would want to use something like this:

因此,你会想要使用这样的东西:

-Dlogging.config="C:\logbacs\logback.xml"

回答by Rudi

If you're not using SpringBoot the above won't work.

如果您不使用 SpringBoot,则上述方法将不起作用。

I'm using the ch.qos.logback.logback-classic library, and in this case the solution is

我正在使用 ch.qos.logback.logback-classic 库,在这种情况下,解决方案是

-Dlogback.configurationFile=file:/app/logback.xml

I've found this property in ch.qos.logback.classic.util.ContextInitializer:

我在 ch.qos.logback.classic.util.ContextInitializer 中找到了这个属性:

final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile"

回答by Idan Shviro

For me it works only with this property:

对我来说,它只适用于这个属性:

-Dconfig.dir= "YOUR PATH TO CONFIG FILE"