Nabil Sham
Nabil Sham

Reputation: 2355

Log4j2 in a spring web app with servlet 3.0

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

Upvotes: 1

Views: 887

Answers (1)

Tony Ra
Tony Ra

Reputation: 128

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.

Upvotes: 1

Related Questions