LogAspect.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. String after = o==null?null:new JSONObject(o).toString();
  37. tracer.getCurrentSpan().tag(GatewayContanrts.ZipkinElasticKey.gateway_out_params,after);
  38. } catch (Exception ex) {
  39. ex.printStackTrace();
  40. }
  41. return o;
  42. }
  43. }