Struts 2 异常拦截器的 Log4j 记录器
我应该在 log4j.xml 中列出哪个记录器来捕获未处理的 struts 2 异常?
我在 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>
在我的 log4j.xml 文件中,声明了以下记录器:
<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>
但是,当 Struts 引发异常时,log4j 不会正确记录该异常。我知道我的 log4j.xml 正在被正确解析,因为我可以使用名为“error.unhandled”的记录器手动创建一个 java 类,并将错误级别消息直接写入其中。一位同事还建议我尝试捕获 log4j.logger.error.unhandled 记录器,但这也不起作用。
Struts 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要做的就是为默认拦截器堆栈中的异常拦截器提供参数。在所发布的示例中,您正在重新定义默认堆栈。
解决方案取自官方wiki: https://cwiki.apache.org/WW/exception- Handling.html
要启用对 Struts 2 框架处理的异常进行日志记录,您必须在 struts.xml 中指定一些参数值。
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.
您在拦截器定义中使用日志级别 WARN。将其更改为 ERROR,您将看到日志消息,或者在 log4j.xml 文件中将记录器级别降低为 WARN、INFO 或 DEBUG。
换句话说,拦截器发出 WARN 级别的消息,控制台附加程序接收该消息,但选择不打印它,因为它被配置为仅打印 ERROR 级别的消息。
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.
log4.xml
中的记录器将其阈值设置为ERROR
,因此低于该级别的任何记录都不会被记录。您的 struts 配置设置为在WARN
级别记录,该级别低于ERROR
。您可能应该将
log4j.xml
logger
中的阈值降低为WARN
,以便获得任何信息。Your logger in
log4.xml
has its threshold set toERROR
, so anything logged below that level will not be recorded. Your struts config is set to log atWARN
level, which is belowERROR
.You should probably reduce the threshold in the
log4j.xml
logger
toWARN
, in order to get anything.