如何在java类中使用logback为特定的类和方法实现不同的记录器模式?

时间:2017-09-20 13:22:06

标签: java logging aspectj logback-classic

基本的aop代码

@Aspect
public class LoggingAspect {
private   Logger logger  ;

@Before("execution(*  *(..)) && !execution(* com.*model*.*(..))")
public void logBefore(JoinPoint joinPoint) {
    logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());
    logger.info("{} :" + joinPoint.getSignature().getName(),"info for user log" );
    //logger.info("hijacked target class : " + joinPoint.getTarget().getClass().getSimpleName() );
}

@After("execution(*  *(..)) && !execution(* com.*model*.*(..)) ")
public void logAfter(JoinPoint joinPoint) {

    System.out.println("logAfter() is running!");
    System.out.println("hijacked : " + joinPoint.getSignature().getName());
    System.out.println("******");

}
}

目前,我默认为某些方法执行aop日志记录,如方法启动和方法结束.so使用aop logger打印apo类和方法而不是打印方法拥有的类和方法。我必须覆盖aop中的类名来打印该方法的类名,所以我需要将方法名称作为本机方法名称

目前我正在

  

2017-09-20 18:32:06 INFO [main] c.m.customer.bo.impl.CustomerBoImpl - logBefore:info for user log():addCustomer

我需要的是

  

2017-09-20 18:32:06 INFO [main] c.m.customer.bo.impl.CustomerBoImpl - addCustomer                   :info for user log():addCustomer

1 个答案:

答案 0 :(得分:0)

最后,我通过此链接找到了一个简单的ProceedingJoinPoint解决方案 Log4j and AOP, how to get actual class name