首页>代码>springboot教务管理系统+微信小程序云开发>/新教务管理系统/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()) + "");
    }


}
最近下载更多
tongguan00  LV2 4月1日
zolscy  LV24 2024年11月27日
admin_z  LV22 2024年1月9日
wbbhappy  LV13 2024年1月9日
lkt123456  LV8 2024年1月6日
晴曛  LV6 2023年12月29日
微信网友_6709209201250304  LV3 2023年10月26日
kennyban  LV1 2023年8月31日
syczzxj  LV10 2023年8月1日
molu123456 2023年6月2日
暂无贡献等级
最近浏览更多
sgm123456  LV14 5月26日
JiangYing009  LV8 5月12日
phoneant 4月15日
暂无贡献等级
tongguan00  LV2 4月1日
星星没有答案 3月27日
暂无贡献等级
ma406805131  LV19 2月21日
quyan5632  LV2 2月10日
wyx065747  LV67 1月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友