Java 以编程方式设置 logback.xml 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21885787/
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-13 11:16:08  来源:igfitidea点击:

Setting logback.xml path programmatically

javalogback

提问by shabby

I know I can set the logback.xml path like this:

我知道我可以像这样设置 logback.xml 路径:

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.

您可以使用名为“logback.configurationFile”的系统属性指定默认配置文件的位置。此属性的值可以是 URL、类路径上的资源或应用程序外部文件的路径。

java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1

but how can I do it in code?

但我如何在代码中做到这一点?

采纳答案by assylias

You could use:

你可以使用:

System.setProperty("logback.configurationFile", "/path/to/config.xml");

But it would have to happen before logback is loaded, i.e. something like:

但它必须在加载 logback 之前发生,即:

class Main {
  static { System.setProperty("logback.configurationFile", "/path/to/config.xml");}
  private final Logger LOG = LoggerFactory.getLogger(Main.class);

  public void main (String[] args) { ... }
}

Note: I have not tested it but it should work.

注意:我没有测试过它,但它应该可以工作。

回答by Luca Basso Ricci

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator configurator = new JoranConfigurator();
InputStream configStream = FileUtils.openInputStream(logbackPropertiesUserFile);
configurator.setContext(loggerContext);
configurator.doConfigure(configStream); // loads logback file
configStream.close();

回答by Amin Abbaspour

modifying environment variables might not always be feasible. To do it properly see logback manual:

修改环境变量可能并不总是可行的。要正确执行此操作,请参阅 logback 手册:

http://logback.qos.ch/manual/configuration.html#joranDirectly

http://logback.qos.ch/manual/configuration.html#joranDirectly

回答by sschrass

I just want to share what I did in the end with a Jersey-Spring app:

我只想分享我最后用 Jersey-Spring 应用程序做了什么:

MyWebApplicationInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        servletContext.addListener(new LogbackConfigListener());
        try { LogbackConfigurer.initLogging(servletContext.getRealPath("/WEB-INF/logback.xml")); }
        catch (FileNotFoundException | JoranException e) { e.printStackTrace(); }
    }
}

I also have to add, that I had to move

我还必须补充一点,我必须搬家

<dependency>
    <groupId>org.logback-extensions</groupId>
    <artifactId>logback-ext-spring</artifactId>
    <version>0.1.4</version>
    <scope>compile</scope></dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.3</version>
    <scope>compile</scope></dependency>

from runtime (parent project in my case) to compile.

从运行时(在我的情况下为父项目)进行编译。

回答by Himanshu Soni

Include another logback xml to change path in logback-spring.xml

包含另一个 logback xml 以更改logback-spring.xml 中的路径

include resource = "/path/to/logback.xml"

包含资源 = "/path/to/logback.xml"

add data within includedtags in logback.xml

logback.xml 中包含的标签中添加数据