Search code examples
javaloggingaspectjlogback-classic

How to implement different logger pattern for specific class and methods using logback in java class?


basic aop code

@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("******");

}
}

Currently, I am implementing the aop logging for some methods by default like methods starts and methods end .so using aop logger printing the apo class and method instead of printing method owned class and method. I have to override the class name inside aop to print that method's class name like so i need to get method name as native method name

currently i am getting

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

what i need is

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


Solution

  • at last, I found an easy solution with ProceedingJoinPoint from this link Log4j and AOP, how to get actual class name