fix:新增打印黑名单,以及定时器日志打印

This commit is contained in:
zhouz
2024-07-30 14:58:23 +08:00
parent 024d00bd37
commit 6ccd09a395
3 changed files with 19 additions and 38 deletions

View File

@@ -1,5 +1,6 @@
package org.nl.modules.logging.aspect; package org.nl.modules.logging.aspect;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
@@ -62,7 +63,11 @@ public class WhiteListLogAspect {
result = joinPoint.proceed(args); result = joinPoint.proceed(args);
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
long time = endTime - startTime; long time = endTime - startTime;
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//黑名单接口不打印日志
if (!shouldLog(signature.getName())){
addLog(joinPoint, JSONUtil.toJsonStr(result), time); addLog(joinPoint, JSONUtil.toJsonStr(result), time);
}
} catch (Exception e) { } catch (Exception e) {
log.error("doAround日志记录异常异常信息为:", e); log.error("doAround日志记录异常异常信息为:", e);
throw e; throw e;
@@ -75,39 +80,15 @@ public class WhiteListLogAspect {
*/ */
public void addLog(ProceedingJoinPoint joinPoint, String outParams, long time) { public void addLog(ProceedingJoinPoint joinPoint, String outParams, long time) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); HttpServletRequest request = null;
HttpServletResponse response = attributes.getResponse(); if (ObjectUtil.isNotEmpty(attributes)) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); request = attributes.getRequest();
Method method = signature.getMethod(); }
// 方法路径 // 方法路径
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
String[] split = joinPoint.getTarget().getClass().getName().split("\\.");
String className = split[split.length - 1];
StringUtils.getIp(request);
log.info("\n\r=======================================\n\r" +
"请求地址:{} \n\r" +
"请求方式:{} \n\r" +
"请求类方法:{} \n\r" +
"方法名::{} \n\r" +
"调用IP:{} \n\r" +
"接口名称:{} \n\r" +
"请求方法参数:{} \n\r" +
"返回报文:{} \n\r" +
"处理耗时:{} ms \n\r" +
"=======================================\n\r",
request.getRequestURI(),
request.getMethod(),
joinPoint.getSignature(),
className,
StringUtils.getIp(request),
signature.getName(),
JSONUtil.toJsonStr(filterArgs(joinPoint.getArgs())),
outParams,
time
);
SysInterfaceLog log = new SysInterfaceLog("INFO", time); SysInterfaceLog log = new SysInterfaceLog("INFO", time);
String borwser = ObjectUtil.isNotEmpty(attributes) ? StringUtils.getBrowser(request) : "定时器";
interfaceLogService.save(getUsername(), outParams, StringUtils.getBrowser(request), StringUtils.getIp(request), joinPoint, log); String ip = ObjectUtil.isNotEmpty(attributes) ? StringUtils.getIp(request) : "定时器";
interfaceLogService.save(getUsername(), outParams, borwser, ip, joinPoint, log);
} }
public String getUsername() { public String getUsername() {
@@ -138,5 +119,9 @@ public class WhiteListLogAspect {
&& !(object instanceof HttpServletResponse) && !(object instanceof HttpServletResponse)
).collect(Collectors.toList()); ).collect(Collectors.toList());
} }
private boolean shouldLog(String requestURI) {
return loggingProperties.getIncludePaths().stream().anyMatch(pattern -> pathMatcher.match(pattern, requestURI));
}
} }

View File

@@ -39,8 +39,6 @@ public class SysInterfaceLogServiceImpl extends ServiceImpl<SysInterfaceLogMappe
@Override @Override
public void save(String username, String outParam, String browser, String ip, ProceedingJoinPoint joinPoint, SysInterfaceLog logDto) { public void save(String username, String outParam, String browser, String ip, ProceedingJoinPoint joinPoint, SysInterfaceLog logDto) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String[] split = joinPoint.getTarget().getClass().getName().split("\\."); String[] split = joinPoint.getTarget().getClass().getName().split("\\.");
String className = split[split.length - 1]; String className = split[split.length - 1];

View File

@@ -92,11 +92,9 @@ logging:
file: file:
path: d:\log\lms path: d:\log\lms
config: classpath:logback-spring.xml config: classpath:logback-spring.xml
#接口日志黑名单
include-paths: include-paths:
- /api/mes/** - getHotPointStatus
- /api/lms/**
- /api/sap/**
- /api/crm/**
# sa-token白名单配置 # sa-token白名单配置
security: security:
# 排除路径 # 排除路径