以编程方式设置logback.xml路径 [英] Setting logback.xml path programmatically

查看:1105
本文介绍了以编程方式设置logback.xml路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

指定默认配置文件作为系统属性的位置

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

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.

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

但是我怎么能在代码中做到这一点?

but how can I do it in code?

推荐答案

你可以使用:

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

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

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

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.

这篇关于以编程方式设置logback.xml路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆