mirror of https://github.com/halo-dev/halo
Enable request and response logging
parent
9139285a37
commit
c30f107a96
|
@ -80,7 +80,6 @@ public class HaloConfiguration {
|
||||||
*
|
*
|
||||||
* @return Log filter registration bean
|
* @return Log filter registration bean
|
||||||
*/
|
*/
|
||||||
@Bean
|
|
||||||
public FilterRegistrationBean<LogFilter> logFilter() {
|
public FilterRegistrationBean<LogFilter> logFilter() {
|
||||||
FilterRegistrationBean<LogFilter> logFilter = new FilterRegistrationBean<>();
|
FilterRegistrationBean<LogFilter> logFilter = new FilterRegistrationBean<>();
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||||
import run.halo.app.config.properties.HaloProperties;
|
import run.halo.app.config.properties.HaloProperties;
|
||||||
import run.halo.app.controller.support.PageJacksonSerializer;
|
import run.halo.app.core.PageJacksonSerializer;
|
||||||
import run.halo.app.factory.StringToEnumConverterFactory;
|
import run.halo.app.factory.StringToEnumConverterFactory;
|
||||||
import run.halo.app.model.support.HaloConst;
|
import run.halo.app.model.support.HaloConst;
|
||||||
import run.halo.app.security.resolver.AuthenticationArgumentResolver;
|
import run.halo.app.security.resolver.AuthenticationArgumentResolver;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package run.halo.app.controller.base;
|
package run.halo.app.core;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
|
@ -1,4 +1,4 @@
|
||||||
package run.halo.app.controller.base;
|
package run.halo.app.core;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
|
@ -0,0 +1,88 @@
|
||||||
|
package run.halo.app.core;
|
||||||
|
|
||||||
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import run.halo.app.utils.JsonUtils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class ControllerLogAop {
|
||||||
|
|
||||||
|
@Pointcut("execution(* *..*.*.controller..*.*(..))")
|
||||||
|
public void controller() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Around("controller()")
|
||||||
|
public Object controller(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
String className = joinPoint.getTarget().getClass().getSimpleName();
|
||||||
|
String methodName = joinPoint.getSignature().getName();
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
|
||||||
|
// Get request attribute
|
||||||
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
|
HttpServletRequest request = Objects.requireNonNull(requestAttributes).getRequest();
|
||||||
|
|
||||||
|
printRequestLog(request, className, methodName, args);
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
Object returnObj = joinPoint.proceed();
|
||||||
|
printResponseLog(request, className, methodName, returnObj, System.currentTimeMillis() - start);
|
||||||
|
return returnObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void printRequestLog(HttpServletRequest request, String clazzName, String methodName, Object[] args) throws JsonProcessingException {
|
||||||
|
log.debug("Request URL: [{}], URI: [{}], Request Method: [{}], IP: [{}]",
|
||||||
|
request.getRequestURL(),
|
||||||
|
request.getRequestURI(),
|
||||||
|
request.getMethod(),
|
||||||
|
ServletUtil.getClientIP(request));
|
||||||
|
|
||||||
|
if (args == null || !log.isDebugEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hasServletArg = false;
|
||||||
|
for (Object arg : args) {
|
||||||
|
if (arg instanceof HttpServletRequest ||
|
||||||
|
arg instanceof HttpServletResponse ||
|
||||||
|
arg instanceof MultipartFile) {
|
||||||
|
hasServletArg = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasServletArg) {
|
||||||
|
String requestBody = JsonUtils.objectToJson(args);
|
||||||
|
log.debug("{}.{} Parameters: [{}]", clazzName, methodName, requestBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printResponseLog(HttpServletRequest request, String className, String methodName, Object returnObj, long usage) throws JsonProcessingException {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
String returningData = null;
|
||||||
|
if (returnObj != null) {
|
||||||
|
if (returnObj.getClass().isAssignableFrom(byte[].class)) {
|
||||||
|
returningData = "Binary data";
|
||||||
|
} else {
|
||||||
|
returningData = JsonUtils.objectToJson(returnObj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.debug("{}.{} Response: [{}], usage: [{}]ms", className, methodName, returningData, usage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package run.halo.app.controller.support;
|
package run.halo.app.core;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
Loading…
Reference in New Issue