Starting a web application, but hits the following error messages :

...
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
...

1. Normal Case

1.1 Obviously, the Apache Commons logging is missing – commons-logging-xxx.jar. To fix it, get it from Maven central repository.

pom.xml

   <dependency>
	  <groupId>commons-logging</groupId>
	  <artifactId>commons-logging</artifactId>
	  <version>1.2</version>
   </dependency>

2. Spring Case

2.1 For Spring application, developers always excluded the commons-logging, but forget to include another logging framework. For example

pom.xml

   <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-webmvc</artifactId>
	  <version>${spring.version}</version>
	  <exclusions>
	      <exclusion>
		  <groupId>commons-logging</groupId>
		  <artifactId>commons-logging</artifactId>
	      </exclusion>
	  </exclusions>
   </dependency>

The above declaration will cause this classNotFoundException: org.apache.commons.logging.LogFactory also.

2.2 To fix it, declares another logging framework, often, this is SLF4j and redirect the Spring’s logging via a bridge.

pom.xml

   <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-webmvc</artifactId>
	  <version>${spring.version}</version>
	  <exclusions>
	      <exclusion>
		  <groupId>commons-logging</groupId>
		  <artifactId>commons-logging</artifactId>
	      </exclusion>
	  </exclusions>
   </dependency>

   <!-- bridge jck to slf4j -->
   <dependency>
	  <groupId>org.slf4j</groupId>
	  <artifactId>jcl-over-slf4j</artifactId>
	  <version>${jcl.slf4j.version}</version>
   </dependency>

   <dependency>
	  <groupId>ch.qos.logback</groupId>
	  <artifactId>logback-classic</artifactId>
	  <version>${logback.version}</version>
   </dependency>

References

  1. Apache Commons Logging
  2. Spring MVC + Logback SLF4j example