Merge branch 'master' of https://e.coding.net/g-iswv8783/education/ExamLocalhost
This commit is contained in:
70
src/main/java/com/example/exam/exam/config/WebLogAspect.java
Normal file
70
src/main/java/com/example/exam/exam/config/WebLogAspect.java
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package com.example.exam.exam.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
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.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class WebLogAspect {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
private ThreadLocal<Long> startTime = new ThreadLocal<>();
|
||||||
|
|
||||||
|
@Pointcut("execution(* com.example..controller..*(..))") // ⚠ 修改为你自己的 Controller 包路径
|
||||||
|
public void webLog() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before("webLog()")
|
||||||
|
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||||
|
startTime.set(System.currentTimeMillis());
|
||||||
|
|
||||||
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
|
if (attributes == null) return;
|
||||||
|
HttpServletRequest request = attributes.getRequest();
|
||||||
|
|
||||||
|
// 获取方法参数
|
||||||
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
|
Method method = signature.getMethod();
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
Annotation[][] annotations = method.getParameterAnnotations();
|
||||||
|
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
for (int i = 0; i < annotations.length; i++) {
|
||||||
|
for (Annotation annotation : annotations[i]) {
|
||||||
|
if (annotation instanceof RequestParam requestParam) {
|
||||||
|
params.put(requestParam.value(), args[i]);
|
||||||
|
} else if (annotation instanceof RequestBody) {
|
||||||
|
params.put("body", args[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("请求开始:{} {}", request.getMethod(), request.getRequestURI());
|
||||||
|
logger.info("调用方法:{}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
|
||||||
|
logger.info("请求参数:{}", objectMapper.writeValueAsString(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterReturning(pointcut = "webLog()", returning = "ret")
|
||||||
|
public void doAfterReturning(Object ret) throws Throwable {
|
||||||
|
long elapsed = System.currentTimeMillis() - startTime.get();
|
||||||
|
logger.info("请求结束,耗时:{}ms", elapsed);
|
||||||
|
startTime.remove();
|
||||||
|
}
|
||||||
|
}
|
27
src/main/resources/logback.xml
Normal file
27
src/main/resources/logback.xml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<property name="LOG_PATH" value="logs"/>
|
||||||
|
<property name="APP_NAME" value="log"/>
|
||||||
|
|
||||||
|
<!-- 控制台输出 -->
|
||||||
|
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<!-- 写入固定日志文件 -->
|
||||||
|
<appender name="file" class="ch.qos.logback.core.FileAppender">
|
||||||
|
<file>${LOG_PATH}/${APP_NAME}.log</file>
|
||||||
|
<append>true</append> <!-- 追加写入 -->
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<!-- 根日志级别 -->
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="console"/>
|
||||||
|
<appender-ref ref="file"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Reference in New Issue
Block a user