LogAspect.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.yihu.jw.aspect;
  2. import com.yihu.jw.restmodel.gateway.GatewayContanrts;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Pointcut;
  7. import org.json.JSONObject;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.cloud.sleuth.Tracer;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.context.request.RequestContextHolder;
  12. import org.springframework.web.context.request.ServletRequestAttributes;
  13. import javax.servlet.http.HttpServletRequest;
  14. /**
  15. * Created by chenweida on 2017/6/19.
  16. */
  17. @Aspect
  18. @Component
  19. public class LogAspect {
  20. @Autowired
  21. private Tracer tracer;
  22. //Controller层切点
  23. @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
  24. public void controllerAspect() {
  25. }
  26. @Around("controllerAspect()")
  27. public Object checkToken(ProceedingJoinPoint point) throws Throwable {
  28. ;
  29. Object o = null;
  30. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  31. try {
  32. //访问前日志
  33. tracer.getCurrentSpan().tag(GatewayContanrts.ZipkinElasticKey.gateway_input_params,new JSONObject(request.getParameterMap()).toString());
  34. o = point.proceed();
  35. //访问后日志
  36. tracer.getCurrentSpan().tag(GatewayContanrts.ZipkinElasticKey.gateway_out_params,new JSONObject(o).toString());
  37. } catch (Exception ex) {
  38. ex.printStackTrace();
  39. }
  40. return o;
  41. }
  42. }