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?
What you want to do is to provide parameters for exception interceptor in default interceptor stack. In example posted in question you are redefining default stack.
Solution is taken from official wiki: https://cwiki.apache.org/WW/exception-handling.html
To enable logging of the exceptions being handled by the Struts 2 framework you must specify some parameter values in struts.xml.
<interceptors>
<interceptor-stack name="appDefaultStack">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appDefaultStack" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With