赞
踩
通过aop实现日志记录,核心类如下:
- package com.it.elk.aop;
-
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.TimeZone;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
-
- import com.alibaba.fastjson.JSONObject;
- import com.it.elk.kafka.KafkaSender;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- *
- * @ClassName: AopLogAspect
- * @description: ELK拦截日志信息
- * @date 2019年4月26日 下午2:30:12
- * @version V1.0
- */
- @Slf4j
- @Aspect
- @Component
- public class AopLogAspect {
- @Autowired
- private KafkaSender<JSONObject> kafkaSender;
-
- // 申明一个切点 里面是 execution表达式
- @Pointcut("execution(* com.it.*.api.service.impl.*.*(..))")
- private void serviceAspect() {
- }
-
- // 请求method前打印内容
- @Before(value = "serviceAspect()")
- public void methodBefore(JoinPoint joinPoint) {
- ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
- .getRequestAttributes();
- HttpServletRequest request = requestAttributes.getRequest();
-
- JSONObject jsonObject = new JSONObject();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
- // 请求时间
- jsonObject.put("request_time", df.format(new Date()));
- // 请求URL
- jsonObject.put("request_url", request.getRequestURL().toString());
- //请求ip
- jsonObject.put("request_ip", getIpAddr(request));
- // 请求的方法
- jsonObject.put("request_method", request.getMethod());
- // 请求类方法
- jsonObject.put("signature", joinPoint.getSignature());
- // 请求参数
- jsonObject.put("request_args", Arrays.toString(joinPoint.getArgs()));
-
- // 加上ip和端口号
- JSONObject requestJsonObject = new JSONObject();
- requestJsonObject.put("request", jsonObject);
-
- kafkaSender.send(requestJsonObject);
- }
-
- // 在方法执行完结后打印返回内容
- @AfterReturning(returning = "obj", pointcut = "serviceAspect()")
- public void methodAfterReturing(JoinPoint joinPoint,Object obj) {
- ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- HttpServletRequest request = requestAttributes.getRequest();
- log.info("===============after请求内容===============");
- log.info("after请求地址:" + request.getRequestURL().toString());
- log.info("after请求方式:" + request.getMethod());
- log.info("after请求类方法:" + joinPoint.getSignature());
- log.info("after请求类方法参数:" + Arrays.toString(joinPoint.getArgs()));
-
- JSONObject respjsonObject = new JSONObject();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
- df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
- //请求ip
- respjsonObject.put("request_ip", getIpAddr(request));
- // 请求URL
- respjsonObject.put("request_url", request.getRequestURL().toString());
- // 请求方式
- respjsonObject.put("request_method", request.getMethod());
- // 请求类方法
- respjsonObject.put("signature", joinPoint.getSignature());
- // 请求参数
- respjsonObject.put("request_args", Arrays.toString(joinPoint.getArgs()));
-
-
- log.info("--------------after返回内容----------------");
- // 响应时间
- respjsonObject.put("response_time", df.format(new Date()));
- // 响应内容
- respjsonObject.put("response_content", JSONObject.toJSONString(obj));
-
- JSONObject result = new JSONObject();
- result.put("response_log", respjsonObject);
- log.info("after>>>>>>>>>>"+result);
- kafkaSender.send(result);
-
- }
- // 多个项目 每个项目对应不同的主题 不同的主题对应不同的Logstash
-
- /**
- * 获取Ip地址
- *
- * @param request
- * @return
- */
- public String getIpAddr(HttpServletRequest request) {
- String ip = request.getHeader("X-Forwarded-For");
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_CLIENT_IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_FORWARDED_FOR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- return ip;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。