log4jpropertyconfigurator

java.lang.ClassNotFoundException: org.apache.log4j.PropertyConfigurator when upgrading log4j from 1.2.16 to 2.20.0


I have to upgrade log4j from version 1.2.16 to 2.20.0 in codebase of a legacy system. During the upgrade, upgrading spring security from version 4.1.1 to 5.2.15 is also required. Updated dependencies are shown below.

<dependencyManagement>
    <dependencies>
    <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-bom</artifactId>
            <version>2.20.0</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>5.2.15.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>5.2.15.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-cas</artifactId>
    <version>5.2.15.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.reactivestreams</groupId>
    <artifactId>reactive-streams</artifactId>
    <version>1.0.4</version>
</dependency>

When I start the application, there are NoClassDefFoundError and ClassNotFoundException.

14:46:10,354 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC000001: Failed to start service jboss.deployment.unit."cas-ear-2.1.1-SNAPSHOT.ear".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."cas-ear-2.1.1-SNAPSHOT.ear".POST_MODULE: WFLYSRV0153: Failed to process phase POST_MODULE of deployment "cas-ear-2.1.1-SNAPSHOT.ear"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:189)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1739)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1701)
    at org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1559)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/PropertyConfigurator
    at org.jboss.as.logging.deployments.LoggingConfigDeploymentProcessor.configure(LoggingConfigDeploymentProcessor.java:239)
    at org.jboss.as.logging.deployments.LoggingConfigDeploymentProcessor.processDeployment(LoggingConfigDeploymentProcessor.java:113)
    at org.jboss.as.logging.deployments.LoggingConfigDeploymentProcessor.processDeployment(LoggingConfigDeploymentProcessor.java:141)
    at org.jboss.as.logging.deployments.AbstractLoggingDeploymentProcessor.deploy(AbstractLoggingDeploymentProcessor.java:68)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:182)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.PropertyConfigurator from [Module "org.jboss.as.logging" version 16.0.1.Final from local module loader @604ed9f0 (finder: local module finder @6a4f787b (roots: C:\wildfly-24\cas\wildfly-24.0.1.Final\modules,C:\wildfly-24\cas\wildfly-24.0.1.Final\modules\system\layers\base))]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:255)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    ... 13 more

From the stacktrace, org.apache.log4j.PropertyConfigurator can't be found so there is ClassNotFoundException. Since PropertyConfigurator is from log4j 1.2.16 but not log4j 2.20.0, the existence of ClassNotFoundException implies there are some places still importing it from log4j 1.2.16. However, I've updated all relevant imports from old version to new version and searched whole codebase and find no codes still using the old version of log4j.

I've stuck for a few days and done researches on the Internet and still can't figure out the reason. Would someone please help?


Solution

  • As you can see the exception does not come from your application, but from the LoggingConfigDeploymentProcessor class of your application server.

    To find the reason of the error, look at the Per-deployment Logging section of your Wildfly Admin Guide: if use-deployment-logging-config configuration property is set to true (default), the container will look for files like log4j.properties in your application.

    To solve the problem, remove your log4j.properties file from either the WEB-INF or WEB-INF/classes (classpath) folder of your WAR file.