Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Commons Logging / Log4j setup in spring webapp with tomcat 6

I have a problem wih a logging setup in a apring webapp deployed under tomcat 6.

The webapp uses the commons-logging api, on runtime log4j should be used. The log file is created but remains empty - no log entries occur.

the setup is the following:

WEB-INF/web.xml:

 <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

WEB-INF/classes/commons-logging.properties:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

WEB-INF/log4j.xml:

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

  <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    ...
  </appender>
  <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${catalina.home}/logs/my.log"/>
    ...
  </appender>

  <logger name="my.package">
    <level value="INFO"/>
  </logger>

  <root>
    <level value="ERROR"/>
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="FILE"/>
  </root>
</log4j:configuration>

The file logs/my.log is created, but no logs appear. The are info logs on the tomcat console, but not with the layout pattern configured.

The commons-logging-1.1.1.jar and log4j-1.2.14.jar are included in WEB-INF/lib. Any idea what is wrong here?

like image 687
Arne Burmeister Avatar asked Oct 20 '08 09:10

Arne Burmeister


People also ask

Is Commons Logging affected by log4j?

Apache commons logging is a facade API that abstracts the underlying implementation. Using commons logging, one can decide between different implementations such as log4j, slf4j and java util Logging without touching the code. log4j is a logging framework that provides the actual implementation.

Does Apache Commons use log4j?

As Apache Commons Logging is based on JDK 1.4 logging APIs (JSR-47), only log4j and Logkit are supported out of the box. To use Log4J as your logging client, simply add log4j-1.2. jar to your classpath and add log4j. properties configuration file, and you are done.

What is Apache Commons Logging in spring framework?

Apache Commons Logging (previously known as Jakarta Commons Logging or JCL) is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools.


1 Answers

There are numerous documented instances on the web warning people about the use of commons-logging. So much so, that SLF4J is gaining a lot of popularity.

Considering that you are not interested in using Tomcat with Log4j, you should just use Log4j directly in your application. Particularly if there is no chance that you'll be switching logging frameworks in the future. It'll reduce the complexity of your application, and get rid of any class loader issues you are having with commons-logging.

This should be a relatively easy search and replace in your text, as commons-logging and log4j both use a similar call structure for their logging methods.

like image 150
Spencer Kormos Avatar answered Oct 08 '22 22:10

Spencer Kormos