Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ft.qto.rest.aspect;
- import org.apache.log4j.Logger;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.reflect.CodeSignature;
- import org.springframework.context.annotation.EnableAspectJAutoProxy;
- import org.springframework.stereotype.Component;
- @Aspect
- @EnableAspectJAutoProxy(proxyTargetClass=true)
- @Component
- public class LoggingAspect {
- private static final Logger LOG = Logger.getLogger("LoggingAspect.class");
- @Around("within(com.ft.qto.rest.controller..*)")
- public Object getLogMessage(ProceedingJoinPoint joinPoint) throws Throwable{
- // LOG.setLevel(Level.DEBUG);
- String logClassName = this.getClass().getName(); // i have tried this but //how to remove it from logs
- String pckgName = "com.ft.qto.rest.controller.";
- String methodName = joinPoint.getSignature().getName();
- String className = joinPoint.getSignature().getDeclaringTypeName().replace(pckgName, "");
- // log info for for all controllers
- LOG.info("Inside " + methodName + " Method of " + className + " :::::");
- int count =joinPoint.getArgs().length;
- LOG.info("method name " + methodName + " Count " + count + " :::::");
- if(count>0) {
- CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
- for(int i=0;i<count;i++) {
- LOG.info("value of " + codeSignature.getParameterNames()[i] +" in "+ methodName + " ::::: " + joinPoint.getArgs()[i]);
- }
- }
- return joinPoint.proceed();
- }
- // logs for exception raise in code
- @AfterThrowing(pointcut = "within(com.ft.qto.rest.controller..*)", throwing = "ex")
- public void logAfterThrowingException(JoinPoint joinPoint, Exception ex )
- {
- String pckgName = "com.ft.qto.rest.controller.";
- String methodName = joinPoint.getSignature().getName();
- String className = joinPoint.getSignature().getDeclaringTypeName().replace(pckgName, "");
- LOG.error("ERROR Occurred In " + methodName + " Method of " + className + " :::::");
- LOG.error("ERROR ", ex);
- }
- }
- 2019-05-30 09:39:36,055 INFO [com.ft.qto.rest.aspect.LoggingAspect] getLogMessage(36) : Inside getAllProductReferential Method of ObjectApplicationControllerImpl :::::
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement