views:

3195

answers:

4

I'm trying to get log4j application (webapp) logging working in a Tomcat 6 app. I have log4j-1.2.15.jar in my WEB-INF directory, log4j.dtd and log4j.xml in WEB-INF/classes.

My log4j.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="massAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="maxFileSize" value="100KB" />
    <param name="maxBackupIndex" value="2" />
    <param name="File" value="${catalina.home}/logs/mass.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}: %m%n " />
    </layout>
</appender>

<category name="com.company.mass">
    <priority value="DEBUG"/>
    <appender-ref ref="massAppender"/>
</category>

<root>
     <appender-ref ref="massAppender" />
</root>

</log4j:configuration>

My servlet is in the package:

package com.company.mass;

where the logger is declared as:

private static Logger logger = Logger.getLogger(Handler.class);

and at the top of my doGet(...) method there is:

logger.error("foo");

When I deploy the app in Tomcat and go to the servlet, it works correctly. I even get a mass.log file, but nothing gets put in it. It doesn't show up in any other logs either, and there are no obvious errors. Any idea what's going on?

A: 

Not sure if you need a priority in your root logger. Try this config

<category name="com.company.mass">
    <priority value="DEBUG"/> 
    <!-- no need to specify appender again here -->
</category>

<root>
    <priority value="INFO"/> 
     <appender-ref ref="massAppender" />
</root>
Mike Pone
Unfortunately, those changes did not fix it.
+3  A: 

Are you sure that log4j is actually using your log4j.xml for it's configuration, and not another file on the classpath?

Enable the system property -Dlog4j.debug to have log4j print out information about exactly which configuration file it is using.

matt b
It looks like log4j is reading the proper config values.
well here is a dumb question, are you absolutely positive that the logging lines are even getting hit when the code is executed? Also btw, adding the appender twice (to root and to "com.company.mass") will result in log messages sent to the appender twice
matt b
Stupid question but a good one. I was hitting the default index.jsp that Maven produces which coincidentally produced the exact same output (Hello World) that my servelt produced. Going to a page with logging made logging work.
Glad to hear it's working!
matt b
A: 

Here is what executing Tomcat with -Dlog4j.debug gives:

log4j: Using URL [file:/C:/dev/mass/trunk/mass/target/mass/WEB-INF/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.company.mass] additivity to [true].
log4j: Level value for com.company.mass is  [ERROR].
log4j: com.company.mass level set to ERROR
log4j: Level value for root is  [INFO].
log4j: root level set to INFO
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [maxFileSize] to [100KB].
log4j: Setting property [maxBackupIndex] to [2].
log4j: Setting property [file] to [C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.18/logs/mass.log].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}: %m%n ].
log4j: setFile called: C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.18/logs/mass.log, true
log4j: setFile ended
log4j: Adding appender named [massAppender] to category [root].
A: 

Try adding the line :

<param name="Threshold" value="ALL" />

to your massAppender config

and this line

<priority value ="debug"/>

inside your root definition

Gandalf
That doesn't make a difference.