0

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

Ashok Kumar N
  • 573
  • 6
  • 23

1 Answers1

0

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

Ashok Kumar N
  • 573
  • 6
  • 23
  • i found the answer for the class but not for the method – Ashok Kumar N Oct 19 '17 at 15:49
  • 1
    you will need to define separate appender and then access through logger name/category. Get different loggers by supplying different names when making a call to Logger.getLogger – Acewin Oct 19 '17 at 16:07
  • @Acewin, through the above answer I can get the dynamic class name in the logger. is there any other way to override the method name in logger like what MDC is doing. or we can override the %M through MDC – Ashok Kumar N Oct 19 '17 at 16:10
  • I am not sure what you mean. You may need to elaborate your question. If you need the log display patter to be different then you can change that in your log4j.xml. Your log pattern will look something like %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n.. Change this the way u want the log to be displayed. – Acewin Oct 19 '17 at 16:27
  • @Acewin I am using logback [https://stackoverflow.com/questions/46834273/override-method-name-in-the-logback?noredirect=1#comment80616691_46834273] here i am getting class name as the actual one instead of aop class like i need to get the actual method name instead of aop method name in logger or is there any way to override the %M – Ashok Kumar N Oct 20 '17 at 07:16