2011-03-26 50 views
1

我無法從我的Spring服務記錄RuntimeExceptions。如何登錄Spring RuntimeException

我的配置是:

罐子(Maven的):

  • 我已經排除共享記錄
  • 我的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。作爲Web服務器的Tomcat 7.0.11。

我的目標是有IllegalStateException異常記錄爲任何其他正常的例外是 - 我的日誌附加目的地。我尋找一種方法來註冊一個全局的UncaughtExceptionHandler但找不到任何,所以我想我的運行時異常沒有被記錄。相反,他們被輸出到標準輸出。

我該如何解決這個問題? 任何幫助表示讚賞。

尤金。

回答

0

使用HandlerExceptionResolver

參見Handling Exceptions的Spring MVC的參考。

+0

我知道春天異常解析器,並會考慮他們。但我的假設是,記錄異常是一個正交的問題,應該看看我如何設置任何異常解析器的角色。我從你的回答中瞭解到我的假設是錯誤的?感謝您的反饋。 – Eugen 2011-03-28 14:47:21

+1

有些東西需要捕捉異常並記錄下來,否則它會一直持續到Tomcat。 – sourcedelica 2011-03-28 14:59:30