代码之家  ›  专栏  ›  技术社区  ›  Ashok Kumar N

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

  •  0
  • Ashok Kumar N  · 技术社区  · 7 年前

    @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日志记录。因此,使用aop记录器打印apo类和方法,而不是打印方法拥有的类和方法。我必须重写aop中的类名以打印该方法的类名,因此我需要将方法名作为本机方法名

    目前我正在

    我需要的是

    2017-09-20 18:32:06信息[主]c.m.customer。薄。impl。CustomerBoImpl-添加客户 :info for user log():addCustomer

    1 回复  |  直到 6 年前
        1
  •  0
  •   Ashok Kumar N    7 年前

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