首页>代码>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日
暂无贡献等级
最近浏览更多
暂无贡献等级
熊猫烧香 LV1
8月29日
ttg2025 LV3
8月22日
Solowen
7月18日
暂无贡献等级
AaronDjc LV3
7月9日
空中飞尘 LV13
7月8日
sgm123456 LV14
5月26日
JiangYing009 LV8
5月12日
phoneant
4月15日
暂无贡献等级
tongguan00 LV2
4月1日

