| package com.xxx.common.aop; |
| |
| import java.lang.annotation.*; |
| |
| @Target(ElementType.METHOD) |
| @Retention(RetentionPolicy.RUNTIME) |
| @Documented |
| public @interface LogAnnotation { |
| String module() default ""; |
| |
| String operator() default ""; |
| } |
| |
| package com.xxx.common.aop; |
| |
| import com.gexin.fastjson.JSON; |
| import com.xxx.model.utils.HttpContextUtils; |
| import com.xxx.model.utils.IpUtils; |
| import lombok.extern.slf4j.Slf4j; |
| import org.aspectj.lang.ProceedingJoinPoint; |
| import org.aspectj.lang.annotation.Around; |
| import org.aspectj.lang.annotation.Aspect; |
| import org.aspectj.lang.annotation.Pointcut; |
| import org.aspectj.lang.reflect.MethodSignature; |
| import org.springframework.stereotype.Component; |
| |
| import javax.servlet.http.HttpServletRequest; |
| import java.lang.reflect.Method; |
| |
| @Aspect |
| @Component |
| @Slf4j |
| public class LogAspect { |
| |
| |
| @Pointcut("@annotation(com.xxx.common.aop.LogAnnotation)") |
| public void pt() { |
| } |
| |
| @Around("pt()") |
| public Object log(ProceedingJoinPoint joinPoint) throws Throwable { |
| |
| long beginTime = System.currentTimeMillis(); |
| |
| Object returnValue = joinPoint.proceed(); |
| |
| long time = System.currentTimeMillis() - beginTime; |
| |
| |
| recordLog(joinPoint, time); |
| |
| return returnValue; |
| } |
| |
| private void recordLog(ProceedingJoinPoint joinPoint, long time) { |
| MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| Method method = signature.getMethod(); |
| LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class); |
| log.info("========log start========="); |
| log.info("module:{}", logAnnotation.module()); |
| log.info("operation:{}", logAnnotation.operator()); |
| |
| |
| String className = joinPoint.getTarget().getClass().getName(); |
| String methodName = signature.getName(); |
| log.info("request method:{}", className + "." + methodName + "()"); |
| |
| |
| Object[] args = joinPoint.getArgs(); |
| String params = JSON.toJSONString(args[0]); |
| log.info("params:{}", params); |
| |
| |
| HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); |
| log.info("ip:{}", IpUtils.getIpAddr(request)); |
| |
| log.info("execute time : {} ms", time); |
| log.info("==========log end=========="); |
| } |
| } |
| |
| |
| @LogAnnotation(module = "模块名称", operator="操作参数") |
| package com.xxx.model.utils; |
| |
| import org.springframework.web.context.request.RequestContextHolder; |
| import org.springframework.web.context.request.ServletRequestAttributes; |
| |
| import javax.servlet.http.HttpServletRequest; |
| |
| public class HttpContextUtils { |
| |
| public static HttpServletRequest getHttpServletRequest() { |
| return Objects.requireNonNull(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())).getRequest(); |
| } |
| } |
| |
| package com.xxx.model.utils; |
| |
| import lombok.extern.slf4j.Slf4j; |
| import org.apache.commons.lang3.StringUtils; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import javax.servlet.http.HttpServletRequest; |
| |
| @Slf4j |
| public class IpUtils { |
| private static final Logger logger = LoggerFactory.getLogger(IpUtils.class); |
| |
| |
| |
| |
| |
| |
| public static String getIpAddr(HttpServletRequest request) { |
| String ip = null; |
| try { |
| ip = request.getHeader("x-forwarded-for"); |
| if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
| ip = request.getHeader("Proxy-Client-IP"); |
| } |
| if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| ip = request.getHeader("WL-Proxy-Client-IP"); |
| } |
| if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
| ip = request.getHeader("HTTP_CLIENT_IP"); |
| } |
| if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
| ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
| } |
| if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
| ip = request.getRemoteAddr(); |
| } |
| } catch (Exception e) { |
| logger.error("IPUtils ERROR ", e); |
| } |
| |
| if (StringUtils.isNotEmpty(ip) && ip.length() > 15) { |
| if (ip.indexOf(",") > 0) { |
| ip = ip.substring(0, ip.indexOf(",")); |
| } |
| } |
| return ip; |
| } |
| } |
| |
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/17371495.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步