首页>代码>SpringBoot医药管理系统>/springboot-drugs/src/main/java/com/simon/springbootdrugs/aspect/LogAspect.java
package com.simon.springbootdrugs.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author Simon
 */
@Aspect
@Component
public class LogAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.simon.springbootdrugs.controller.*.*(..))")
    public void log() {

    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        String url = request.getRequestURI();
        String ip = request.getRemoteAddr();

        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        RequestLog requestLog = new RequestLog(url, ip, classMethod, args);
        logger.info("Request : {}", requestLog);
    }

    @After("log()")
    public void doAfter() {
    }

    @AfterReturning(returning = "result", pointcut = "log()")
    public void doAfterRuturn(Object result) {
        logger.info("Result : {}", result);
    }

    private class RequestLog {
        private String url;
        private String ip;
        private String classMethod;
        private Object[] args;

        public RequestLog(String url, String ip, String classMethod, Object[] args) {
            this.url = url;
            this.ip = ip;
            this.classMethod = classMethod;
            this.args = args;
        }

        @Override
        public String toString() {
            return "{" +
                    "url='" + url + '\'' +
                    ", ip='" + ip + '\'' +
                    ", classMethod='" + classMethod + '\'' +
                    ", args=" + Arrays.toString(args) +
                    '}';
        }
    }
}
最近下载更多
Yj123456k  LV2 5月10日
王愉悦  LV4 2024年12月3日
zolscy  LV24 2024年11月26日
xinxin23383  LV7 2024年9月13日
微信网友_7134912998903808  LV15 2024年8月29日
sweetlove  LV20 2024年7月30日
樱花味小然子  LV5 2024年7月3日
破剑茶寮  LV4 2024年6月26日
TY0165  LV20 2024年6月16日
2775645521  LV7 2024年6月16日
最近浏览更多
haitianjiaoyu0  LV8 5月27日
Yj123456k  LV2 5月10日
gann2015  LV25 4月11日
zhao44413 2月21日
暂无贡献等级
zhangtian1997  LV10 2月5日
wangjinhao 1月20日
暂无贡献等级
geekcjj  LV18 2024年12月23日
苯科仔 2024年12月16日
暂无贡献等级
lovedy  LV2 2024年12月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友