Log4j2 in a spring web app with servlet 3.0

三世轮回 提交于 2020-01-16 01:37:17

问题


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

But the only file log4j2 can see is log4j2.xml in the classpath. otherwise I get "no log4j2 configuration file found"

I tried:

-1 setting context parameters

-2 setting system property Log4jContextSelector to "org.apache.logging.log4j.core.selector.JndiContextSelector". and using the JNDI selector

as described here https://logging.apache.org/log4j/2.x/manual/webapp.html#ContextParams

-3 lookups: web, env, sys, ctx and bundle. the first 4 failed only bundle worked but you can only lookup inside the classpath.

-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 in the project

./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

回答1:


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", "*"};
    }
}

I think you can change CustomLogsConfigurationFactory on XmlConfigurationFactory, and change return typse in getSupportedTypes method. I hope this will help you.



来源:https://stackoverflow.com/questions/33187689/log4j2-in-a-spring-web-app-with-servlet-3-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!