我的web应用程序正在使用log4j2.properties.目前,该文件位于我的web-app中的web-INF中.客



我们可以使用系统属性-Dlog4j2.configurationFile=<file_path>,然而,在同一台服务器上部署了多个web应用程序,因此,我只想明确设置我的web应用程序的文件路径。一种方法是使用以下代码。但是,LoggerContext是一个私有的API,可以在小版本中更改。有没有任何简单的方法(没有java代码(来设置特定于web应用程序的(或者说每个web应用程序都有外部log4j2.properties(外部log4k2.properties文件?

// import org.apache.logging.log4j.core.LoggerContext;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());

如果将log4j-web添加到应用程序中,则会出现特定于web应用程序的其他功能(请参阅文档(。

在您的特定情况下,您可以使用log4jConfigurationservlet上下文参数来指定特定应用程序的配置文件的位置。如何从外部指定servlet上下文参数取决于servlet容器。

例如,在Tomcat中,您可以在context.xml文件中执行此操作:

<Context>
<Parameter name="log4jConfiguration" value="/path/to/config/file" override="false"/>
...
</Context>

备注org.apache.logging.log4j.core.LoggerContext不是内部类,它只是Log4j Core(Log4j API的官方实现(中的一个类,而org.apache.logging.log4j.LoggerContext属于Log4j API。除非您将实现从Log4j Core切换到另一个,否则LoggerContext将始终是core.LoggerContext

相关内容

最新更新