带有Servlet 3.0的Spring Web应用程序中的Log4j2 [英] Log4j2 in a spring web app with servlet 3.0
问题描述
我需要更改log4j2配置文件的默认位置.我在这里关注了文档 https://logging.apache.org/log4j/2.x/manual /webapp.html
I need to change the default location of log4j2 configuration file. I followed the documentation here https://logging.apache.org/log4j/2.x/manual/webapp.html
但是log4j2唯一可以看到的文件是classpath中的log4j2.xml.否则,我会收到找不到log4j2配置文件"
But the only file log4j2 can see is log4j2.xml in the classpath. otherwise I get "no log4j2 configuration file found"
我尝试过:
-1设置上下文参数
-2将系统属性Log4jContextSelector设置为"org.apache.logging.log4j.core.selector.JndiContextSelector".并使用JNDI选择器
-2 setting system property Log4jContextSelector to "org.apache.logging.log4j.core.selector.JndiContextSelector". and using the JNDI selector
如此处所述 https://logging.apache.org/log4j/2.x /manual/webapp.html#ContextParams
-3查找:Web,env,sys,ctx和bundle.前4个失败的仅捆绑软件有效,但您只能在类路径中查找.
-3 lookups: web, env, sys, ctx and bundle. the first 4 failed only bundle worked but you can only lookup inside the classpath.
-4将isLog4jAutoInitializationDisabled设置为true,在这种情况下,我不确定如何配置过滤器.如果我将它们包含在web.xml中,则该应用将不会部署.
-4 set isLog4jAutoInitializationDisabled to true, and I am not sure how to configure the filter in this case. If I include them in the web.xml the app will not deploy.
项目中的jar
./WEB-INF/lib/log4j-jcl-2.4.1.jar
./WEB-INF/lib/log4j-core-2.4.1.jar
./WEB-INF/lib/log4j-slf4j-impl-2.4.1.jar
./WEB-INF/lib/log4j-api-2.4.1.jar
推荐答案
在使用.propeties文件的情况下,我使用下面显示的代码
In my situation with .propeties file I use code shown below
@Plugin(name = "LogsConfigurationFactory", category = ConfigurationFactory.CATEGORY)
public class CustomLogsConfigurationFactory extends PropertiesConfigurationFactory {
@Override
public Configuration getConfiguration(String name, URI configLocation) {
File propFile = new File("/path_to/log4j2.properties");
return super.getConfiguration(name, propFile.toURI());
}
@Override
protected String[] getSupportedTypes() {
return new String[] {".properties", "*"};
}
}
我认为您可以在 XmlConfigurationFactory 上更改CustomLogsConfigurationFactory,并在getSupportedTypes方法中更改返回类型.希望对您有帮助.
I think you can change CustomLogsConfigurationFactory on XmlConfigurationFactory, and change return typse in getSupportedTypes method. I hope this will help you.
这篇关于带有Servlet 3.0的Spring Web应用程序中的Log4j2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!