Spring - slf4J:如何自动记录错误和异常? [英] Spring - slf4J : how to automatically log errors and exceptions?

查看:632
本文介绍了Spring - slf4J:如何自动记录错误和异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用slf4j和hibernate的Spring,我试图找出一种方法来自动记录异常和错误(即不在每个类中启动调试器的实例),以便它可以捕获任何错误或异常抛出并在日志中获得类和方法名称,



我读了一篇关于使用方面和使用方面的简短说明。拦截器为此,所以你可以提供一些详细的方式来实现这一点,



问候,



  @Aspect 
public class ExceptionAspect

解决方案

{

private static final Logger log = LoggerFactory.getLogger(ExceptionAspect.class);

public Object handle(ProceedingJoinPoint pjp)throws Throwable {
try {
return pjp.proceed();
} catch(Throwable t){
//所以t:log,wrap,return default,...
log.warn(invocation of+ pjp.getSignature() .toLongString()+failed,t);
//我讨厌记录和重新提升,但为了这个例子,我们来做这件事
throw t;
}
}
}

spring conf:

 <! - 记录程序包中任何名称为'svc'的对象的方法调用异常 - > 
< bean class =org.example.aspects.ExceptionAspectname =exceptionAspect/>
< aop:config>
< aop:aspect ref =exceptionAspect>
< / aop:aspect>
< / aop:config>

编辑:

如果您想记录器代表包装bean登录,你当然可以这样做:

  LoggerFactory.getLogger(pjp.getTarget())。的getClass())警告( 该死的!)。 

或者如果您优先考虑此方法的声明类而不是实际(可能代理/自动生成type):

  LoggerFactory.getLogger(pjp.getSignature()。getDeclaringType())。warn(damn!); 

老实说,我无法估计每次调用LoggerFactory.getLogger(..)时的性能影响。我认为它不应该太糟糕,因为无论如何,例外情况都是例外(即罕见)。


We are using Spring with slf4j and hibernate, I'm trying to figure out a way to log exceptions and errors automatically (i.e without initiating an instance of the debugger in each class), so that it may catch any error or exception thrown and also get class and method name in the log,

I read a very short note about using aspects & interceptors for this, so could you provide me with some detailed way to implement this,

Regards,

解决方案

an exception aspect could look like this:

@Aspect
public class ExceptionAspect {

  private static final Logger log = LoggerFactory.getLogger(ExceptionAspect.class);

  public Object handle(ProceedingJoinPoint pjp) throws Throwable {
     try {
       return pjp.proceed();
     } catch (Throwable t) {
       // so something with t: log, wrap, return default, ...
       log.warn("invocation of " + pjp.getSignature().toLongString() + " failed", t);
       // I hate logging and re-raising, but let's do it for the sake of this example
       throw t;
     }
  }
}

spring conf:

<!-- log exceptions for any method call to any object in a package called 'svc' -->
<bean class="org.example.aspects.ExceptionAspect" name="exceptionAspect" />
<aop:config>
  <aop:aspect ref="exceptionAspect">
    <aop:around method="handle" pointcut="execution(* org.example..svc..*.*(..))" />
  </aop:aspect>
</aop:config>

EDIT:

if you want the logger to log on behalf of the wrapped bean, you could of course do:

LoggerFactory.getLogger(pjp.getTarget().getClass()).warn("damn!");

or if you prefere the declaring class of this method rather than the actual (potentially proxied/auto-generated type):

LoggerFactory.getLogger(pjp.getSignature().getDeclaringType()).warn("damn!");

Honestly, I can't estimate the performance implications of calling LoggerFactory.getLogger(..) every time. I'd assume that it shouldn't be too bad, as exceptions are exceptional (i.e. rare) anyway.

这篇关于Spring - slf4J:如何自动记录错误和异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆