简体   繁体   English

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

[英]Setting logback.xml path programmatically

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". 您可以使用名为“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. 此属性的值可以是URL,类路径上的资源或应用程序外部文件的路径。

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

but how can I do it in code? 但我怎么能在代码中做到这一点?

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();

You could use: 你可以使用:

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

But it would have to happen before logback is loaded, ie 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. 注意:我没有测试过,但应该可以使用。

modifying environment variables might not always be feasible. 修改环境变量可能并不总是可行的。 To do it properly see logback manual: 要正确地看到回滚手册:

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

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. 从运行时(在我的案例中为父项目)进行编译。

Include another logback xml to change path in logback-spring.xml 包含另一个logback xml以更改logback-spring.xml中的路径

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

add data within included tags in logback.xml logback.xml中添加包含标记内的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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