如何记录Spring运行时异常
我在从 Spring 服务记录 RuntimeExceptions 时遇到问题。
我的配置是:
jars (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。 Tomcat 7.0.11 作为 Web 服务器。
我的目标是将 IllegalStateException 像任何其他正常异常一样记录到我的日志附加程序中。我寻找一种注册全局 UncaughtExceptionHandler 的方法,但找不到任何方法,所以我猜我的运行时异常没有被记录。相反,它们被输出到标准输出。
我应该如何解决这个问题? 任何帮助表示赞赏。
欧根.
I'm having trouble logging RuntimeExceptions from my Spring service.
My configuration is:
jars (Maven):
- I have excluded commons-logging
- I have log4j and sl4j on the classpath
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
and in a service method:
throw new IllegalStateException();
I should mention that I'm using Spring MVC. The Spring version is 3.0.5. Tomcat 7.0.11 as a web server.
My goal is to have the IllegalStateException logged as any other normal exception would - to my log appender. I looked for a way to register a global UncaughtExceptionHandler but couldn't find any, so I guess my runtime exceptions aren't being logged. Instead they're being outputted to standard out.
How should I fix this?
Any help is appreciated.
Eugen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 HandlerExceptionResolver。
请参阅 处理异常。
Use a HandlerExceptionResolver.
See the Spring MVC Reference on Handling Exceptions.