当前位置:   article > 正文

springboot项目Api访问日志监控_springboot kafka监控api

springboot kafka监控api

通过aop实现日志记录,核心类如下:

  1. package com.it.elk.aop;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Arrays;
  4. import java.util.Date;
  5. import java.util.TimeZone;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.annotation.AfterReturning;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.it.elk.kafka.KafkaSender;
  18. import lombok.extern.slf4j.Slf4j;
  19. /**
  20. *
  21. * @ClassName: AopLogAspect
  22. * @description: ELK拦截日志信息
  23. * @date 2019年4月26日 下午2:30:12
  24. * @version V1.0
  25. */
  26. @Slf4j
  27. @Aspect
  28. @Component
  29. public class AopLogAspect {
  30. @Autowired
  31. private KafkaSender<JSONObject> kafkaSender;
  32. // 申明一个切点 里面是 execution表达式
  33. @Pointcut("execution(* com.it.*.api.service.impl.*.*(..))")
  34. private void serviceAspect() {
  35. }
  36. // 请求method前打印内容
  37. @Before(value = "serviceAspect()")
  38. public void methodBefore(JoinPoint joinPoint) {
  39. ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
  40. .getRequestAttributes();
  41. HttpServletRequest request = requestAttributes.getRequest();
  42. JSONObject jsonObject = new JSONObject();
  43. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  44. // 请求时间
  45. jsonObject.put("request_time", df.format(new Date()));
  46. // 请求URL
  47. jsonObject.put("request_url", request.getRequestURL().toString());
  48. //请求ip
  49. jsonObject.put("request_ip", getIpAddr(request));
  50. // 请求的方法
  51. jsonObject.put("request_method", request.getMethod());
  52. // 请求类方法
  53. jsonObject.put("signature", joinPoint.getSignature());
  54. // 请求参数
  55. jsonObject.put("request_args", Arrays.toString(joinPoint.getArgs()));
  56. // 加上ip和端口号
  57. JSONObject requestJsonObject = new JSONObject();
  58. requestJsonObject.put("request", jsonObject);
  59. kafkaSender.send(requestJsonObject);
  60. }
  61. // 在方法执行完结后打印返回内容
  62. @AfterReturning(returning = "obj", pointcut = "serviceAspect()")
  63. public void methodAfterReturing(JoinPoint joinPoint,Object obj) {
  64. ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  65. HttpServletRequest request = requestAttributes.getRequest();
  66. log.info("===============after请求内容===============");
  67. log.info("after请求地址:" + request.getRequestURL().toString());
  68. log.info("after请求方式:" + request.getMethod());
  69. log.info("after请求类方法:" + joinPoint.getSignature());
  70. log.info("after请求类方法参数:" + Arrays.toString(joinPoint.getArgs()));
  71. JSONObject respjsonObject = new JSONObject();
  72. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  73. df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
  74. //请求ip
  75. respjsonObject.put("request_ip", getIpAddr(request));
  76. // 请求URL
  77. respjsonObject.put("request_url", request.getRequestURL().toString());
  78. // 请求方式
  79. respjsonObject.put("request_method", request.getMethod());
  80. // 请求类方法
  81. respjsonObject.put("signature", joinPoint.getSignature());
  82. // 请求参数
  83. respjsonObject.put("request_args", Arrays.toString(joinPoint.getArgs()));
  84. log.info("--------------after返回内容----------------");
  85. // 响应时间
  86. respjsonObject.put("response_time", df.format(new Date()));
  87. // 响应内容
  88. respjsonObject.put("response_content", JSONObject.toJSONString(obj));
  89. JSONObject result = new JSONObject();
  90. result.put("response_log", respjsonObject);
  91. log.info("after>>>>>>>>>>"+result);
  92. kafkaSender.send(result);
  93. }
  94. // 多个项目 每个项目对应不同的主题 不同的主题对应不同的Logstash
  95. /**
  96. * 获取Ip地址
  97. *
  98. * @param request
  99. * @return
  100. */
  101. public String getIpAddr(HttpServletRequest request) {
  102. String ip = request.getHeader("X-Forwarded-For");
  103. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  104. ip = request.getHeader("Proxy-Client-IP");
  105. }
  106. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  107. ip = request.getHeader("WL-Proxy-Client-IP");
  108. }
  109. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  110. ip = request.getHeader("HTTP_CLIENT_IP");
  111. }
  112. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  113. ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  114. }
  115. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  116. ip = request.getRemoteAddr();
  117. }
  118. return ip;
  119. }
  120. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/102065
推荐阅读
相关标签
  

闽ICP备14008679号