Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package com.ft.qto.rest.aspect;
  2.  
  3. import org.apache.log4j.Logger;
  4. import org.aspectj.lang.JoinPoint;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.AfterThrowing;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.reflect.CodeSignature;
  10. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  11. import org.springframework.stereotype.Component;
  12.  
  13. @Aspect
  14. @EnableAspectJAutoProxy(proxyTargetClass=true)
  15. @Component
  16. public class LoggingAspect {
  17.  
  18. private static final Logger LOG = Logger.getLogger("LoggingAspect.class");
  19.  
  20. @Around("within(com.ft.qto.rest.controller..*)")
  21. public Object getLogMessage(ProceedingJoinPoint joinPoint) throws Throwable{
  22. // LOG.setLevel(Level.DEBUG);
  23.  
  24.  
  25. String logClassName = this.getClass().getName(); // i have tried this but //how to remove it from logs
  26.  
  27.  
  28. String pckgName = "com.ft.qto.rest.controller.";
  29. String methodName = joinPoint.getSignature().getName();
  30. String className = joinPoint.getSignature().getDeclaringTypeName().replace(pckgName, "");
  31.  
  32. // log info for for all controllers
  33. LOG.info("Inside " + methodName + " Method of " + className + " :::::");
  34.  
  35. int count =joinPoint.getArgs().length;
  36. LOG.info("method name " + methodName + " Count " + count + " :::::");
  37. if(count>0) {
  38. CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
  39. for(int i=0;i<count;i++) {
  40. LOG.info("value of " + codeSignature.getParameterNames()[i] +" in "+ methodName + " ::::: " + joinPoint.getArgs()[i]);
  41. }
  42. }
  43.  
  44. return joinPoint.proceed();
  45.  
  46. }
  47. // logs for exception raise in code
  48. @AfterThrowing(pointcut = "within(com.ft.qto.rest.controller..*)", throwing = "ex")
  49. public void logAfterThrowingException(JoinPoint joinPoint, Exception ex )
  50. {
  51. String pckgName = "com.ft.qto.rest.controller.";
  52. String methodName = joinPoint.getSignature().getName();
  53. String className = joinPoint.getSignature().getDeclaringTypeName().replace(pckgName, "");
  54. LOG.error("ERROR Occurred In " + methodName + " Method of " + className + " :::::");
  55. LOG.error("ERROR ", ex);
  56. }
  57.  
  58. }
  59.  
  60. 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