views:

907

answers:

2

Which logger do I list in my log4j.xml to trap unhandled struts 2 exceptions?

I have the following code declared in my struts.xml:

<package name="default" extends="struts-default">
   <interceptor-stack name="defaultStack">
      <interceptor-ref name="timer"/> 
  <interceptor-ref name="logger"/> 
      <interceptor-ref name="exception">
            <param name="logEnabled">true</param>
            <param name="logCategory">error.unhandled</param>
            <param name="logLevel">WARN</param>
      </interceptor-ref>
   </interceptor-stack>
</package>

In my log4j.xml file, I have the following logger declared:

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%p [%c] - %C{1}.%M(%L) | %m%n"/>
    </layout>
</appender>
<logger name="error.unhandled">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE" />
</logger>

However, when Struts throws an exception, it is not properly logged by log4j. I know that my log4j.xml is being parsed correctly since I can manually create a java class with a logger called "error.unhandled" and write ERROR level messages directly to it. A colleague also suggested that I should try trapping the log4j.logger.error.unhandled logger but that did not work either.

Which logger does Struts 2 use for the exception interceptor?

A: 

You are using log level WARN in your interceptor definition. Change that to ERROR and you'll see the log messages, or alternatively reduce the logger level to WARN, INFO or DEBUG in the log4j.xml file.

In other words, the interceptor sends out a message at WARN level, the console appender receives that message but chooses not to print it because it is configured to print only messages at ERROR level.

Yoni
I lowered the error level all the way down to DEBUG, but it didn't help. Any other ideas?
David
struts uses commons logging, so perhaps your problem lies elsewhere. Are commons-logging and log4j both configured properly?
Yoni
Where would I configure commons-logging at? I've only configured log4j.xml
David
commons-logging doesn't have configuration files, it is designed to "pass through" the logging requests to another logging library, such as log4j. If I recall correctly, you need to have commons-logging-api.jar and commons-logging.jar in your webapp lib library, but that's a completely different topic ... good luck :)
Yoni
@Yoni JCL has the commons-logging.properties file but its not needed in most circumstances.
Tim R
A: 

Your logger in log4.xml has its threshold set to ERROR, so anything logged below that level will not be recorded. Your struts config is set to log at WARN level, which is below ERROR.

You should probably reduce the threshold in the log4j.xml logger to WARN, in order to get anything.

skaffman
I lowered the error level all the way down to DEBUG, but it didn't help. Any other ideas?
David