如何记录Spring RuntimeException

时间:2011-03-26 18:23:55

标签: java spring logging handler runtimeexception

我无法从Spring服务中记录RuntimeExceptions。

我的配置是:

jars(Maven):

  • 我已排除了公共记录
  • 我在classpath上有log4j和sl4j

web.xml:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

log4j.properties:

log4j.rootLogger=INFO, infrastructure

log4j.appender.infrastructure=org.apache.log4j.FileAppender
log4j.appender.infrastructure.File=/opt/logs/infrastructure.log
log4j.appender.infrastructure.layout=org.apache.log4j.PatternLayout
log4j.appender.infrastructure.layout.ConversionPattern=%d{HH:mm:ss} (%t) %-5p [%c] - %m%n

并在服务方法中:

throw new IllegalStateException();

我应该提到我正在使用Spring MVC。 Spring版本是3.0.5。 Tomcat 7.0.11作为Web服务器。

我的目标是将IllegalStateException记录为任何其他正常异常 - 我的日志追加器。我找了一种方法来注册一个全局UncaughtExceptionHandler但找不到任何东西,所以我想我的运行时异常没有被记录。相反,它们被输出到标准输出。

我该如何解决这个问题? 任何帮助表示赞赏。

尤金。

1 个答案:

答案 0 :(得分:0)

使用HandlerExceptionResolver

请参阅Handling Exceptions上的Spring MVC参考。