首页>代码>springboot+mybatis职教务管理系统>/niceschoolapi/src/main/java/com/nsapi/niceschoolapi/common/aspect/LogAspect.java
package com.nsapi.niceschoolapi.common.aspect;

import com.alibaba.fastjson.JSONObject;
import com.nsapi.niceschoolapi.common.annotation.SysLog;
import com.nsapi.niceschoolapi.common.config.MySysUser;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    private ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Pointcut("@annotation(com.nsapi.niceschoolapi.common.annotation.SysLog)")
    public void webLog(){}

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
        startTime.set(System.currentTimeMillis());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpSession session = (HttpSession) attributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
        logger.info("classMethod:======>" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        logger.info("method:======>" + request.getMethod());
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            Object o = args[i];
            if(o instanceof ServletRequest || (o instanceof ServletResponse) || o instanceof MultipartFile){
                args[i] = o.toString();
            }
        }

        String str = JSONObject.toJSONString(args);
        str = str.length() > 2000 ? str.substring(2000) : str;
        logger.info("params:======>" + str);

        if(session != null){
            logger.info("session id :======>" + session.getId());
        }

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog mylog = method.getAnnotation(com.nsapi.niceschoolapi.common.annotation.SysLog.class);
        if(mylog != null){
            //注解上的描述
            logger.info("mylog:======>" + mylog.value());
        }

        if(MySysUser.ShiroUser() != null) {
            String username = StringUtils.isNotBlank(MySysUser.nickName()) ? MySysUser.nickName() : MySysUser.loginName();
            logger.info("user:======>" + username);
        }
    }

    @Around("webLog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            Object obj = proceedingJoinPoint.proceed();
            return obj;
        } catch (Exception e) {
            logger.error("exception message :======>" + e.getMessage());
            throw e;
        }
    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) {
        if(MySysUser.ShiroUser() != null) {
            String username = StringUtils.isNotBlank(MySysUser.nickName()) ? MySysUser.nickName() : MySysUser.loginName();
            logger.info("user:======>" + username);
        }
        String retString = JSONObject.toJSONString(ret);
        retString = retString.length() > 2000 ? retString.substring(2000) : retString;
        logger.info("ret:======>" + retString);
        logger.info("useTime:======>" + (System.currentTimeMillis() - startTime.get()) + "");
    }


}
最近下载更多
itlaolang  LV6 4月28日
梁小贤  LV1 4月25日
zolscy  LV12 3月24日
米继宝是憨憨  LV4 2月1日
admin_z  LV22 1月9日
xutao0523  LV2 2023年10月27日
weilaizhisheng  LV21 2023年9月28日
五折也挺好的  LV13 2023年9月18日
760423530  LV2 2023年9月12日
syczzxj  LV9 2023年8月1日
最近浏览更多
1941549176 4月29日
暂无贡献等级
slf123 4月29日
暂无贡献等级
itlaolang  LV6 4月28日
梁小贤  LV1 4月23日
ll时空幻境  LV1 4月22日
bai fei liu  LV1 4月19日
不包邮 4月15日
暂无贡献等级
wppppp  LV3 4月5日
zolscy  LV12 3月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友