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

7

15

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?

Nissensohn answered 20/10, 2008 at 9:49 Comment(0)
E
10

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.

Exacting answered 20/10, 2008 at 15:11 Comment(3)
I think commons-logging (since 1.1, there where bugs resulting in memory leaks before) is ok. But maybe it is the best solution just to use log4j directly - leaving the libraries using commons-logging unlogged.Nissensohn
This all worked in Tomcat 5.5 - sometimes things getting worseNissensohn
Nope, commons-logging is still garbage (2010), use slf4j. articles.qos.ch/thinkAgain.htmlFission
A
5

Be especially careful that you have not placed log4j.jar in the Tomcat commons/lib directory. If the root classloader loads the log4j libraries, you'll run into conflicts and initialization problems when your webapps also try to use log4j.

If you need to use log4j for common Tomcat logging, you'll need to be careful that your webapps do not attempt to load log4j as well. If you have multiple webapps on the server, then you'll need discipline that each webapp's log initialization does not stomp on the initialization of other webapps. Each webapp will need to use unique Logger IDs, which can be accomplished with unique package names.

Using a common log4j in Tomcat with multiple webapps causes serious conflicts when you have shared libraries that all want to do logging, such as Hibernate or Spring. The next webapp that attempts to initialize log4j may close the logger of the previous one. It can be a mess.

Acrostic answered 22/10, 2008 at 18:25 Comment(0)
A
3

I had similar problem and found a fix now. Start tomcat with additional parameter:

-Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl

Annotate answered 1/4, 2011 at 0:23 Comment(1)
Worked for me !! Thank you ! Been stuck on this for 1 week :PHamil
D
1

You need to compile the extra component for full commons-logging. By default Tomcat 6 uses a hardcoded implementation of commons-logging that always delegates to java.util.logging.

Building instructions here http://tomcat.apache.org/tomcat-6.0-doc/building.html

Then replace the tomcat-juli.jar in the /bin directory of Tomcat and place the tomcat-juli-adapters.jar in the /lib directory along with log4j and config.

Dieterich answered 20/10, 2008 at 13:35 Comment(3)
I do not wont the tomcat itself to log with log4j, just my webapp. Or is there a tomcat classloader bug not allowing to provide my own commons logging?Nissensohn
If Tomcat has the Commons Logging classes in it's lib path, then that will be loaded before any JARs in WEB-INF/lib are loaded.Jot
The problem is the classloader design in tomcat 6 (Common should not have System as parent).Nissensohn
B
0

if you are using log4j +common logging, you can avoid most of above configurations. common logging LogFactory have a discovery feature similar to JAXP, in following precedence, searching for Log implementations, 1. configuration attribute org.apache.commons.logging.Log inside file commons-logging.properties 2. system property org.apache.commons.logging.Log 3. If the Log4J available at class path, use the corresponding wrapper class (Log4JLogger). 4. Jdk14Logger 5. SimpleLog

just make sure, both common-logging.jar and common-logging-api.jar and log4j.jar at classpath.

Beet answered 28/9, 2010 at 14:27 Comment(0)
C
0

To troubleshoot commons logging the system property org.apache.commons.logging.diagnostics.dest allows to dump output diagnostic information to a destination file (I'm not aware from which version).

Chime answered 28/12, 2023 at 10:23 Comment(1)
The problem is not the configuration of commons-logging itselfNissensohn
H
-2

May be i am wrong. Please try the following:

A) Add appender to my.package as: OR B) Reduce the log leve of root to INFO

Hornbeck answered 18/2, 2010 at 12:40 Comment(1)
Its not as easy, I know to config log4j.Nissensohn

© 2022 - 2024 — McMap. All rights reserved.