add [重磅更新] 增加分布式日志框架 TLog
parent
aaf9f57459
commit
cbb0c98f98
@ -0,0 +1,85 @@
|
||||
package com.ruoyi.framework.Interceptor;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.ruoyi.common.utils.JsonUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.yomahub.tlog.context.TLogContext;
|
||||
import com.yomahub.tlog.web.interceptor.AbsTLogWebHandlerMethodInterceptor;
|
||||
import com.yomahub.tlog.web.wrapper.RequestWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 重写Tlog web的调用时间统计拦截器
|
||||
*
|
||||
* @author Lion Li
|
||||
* @since 3.3.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class PlusWebInvokeTimeInterceptor extends AbsTLogWebHandlerMethodInterceptor {
|
||||
|
||||
private final TransmittableThreadLocal<StopWatch> invokeTimeTL = new TransmittableThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public boolean preHandleByHandlerMethod(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
if (TLogContext.enableInvokeTimePrint()) {
|
||||
String url = request.getMethod() + " " + request.getRequestURI();
|
||||
|
||||
// 打印请求参数
|
||||
if (isJsonRequest(request)) {
|
||||
String jsonParam = new RequestWrapper(request).getBodyString();
|
||||
log.info("[PLUS]开始请求 => URL[{}],参数类型[json],参数:[{}]", url, jsonParam);
|
||||
} else {
|
||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||
if (MapUtil.isNotEmpty(parameterMap)) {
|
||||
String parameters = JsonUtils.toJsonString(parameterMap);
|
||||
log.info("[PLUS]开始请求 => URL[{}],参数类型[param],参数:[{}]", url, parameters);
|
||||
} else {
|
||||
log.info("[PLUS]开始请求 => URL[{}],无参数", url);
|
||||
}
|
||||
}
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
invokeTimeTL.set(stopWatch);
|
||||
stopWatch.start();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandleByHandlerMethod(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletionByHandlerMethod(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
if (TLogContext.enableInvokeTimePrint()) {
|
||||
StopWatch stopWatch = invokeTimeTL.get();
|
||||
stopWatch.stop();
|
||||
log.info("[PLUS]结束请求 => URL[{}],耗时:[{}]毫秒", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime());
|
||||
invokeTimeTL.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断本次请求的数据类型是否为json
|
||||
*
|
||||
* @param request request
|
||||
* @return boolean
|
||||
*/
|
||||
private boolean isJsonRequest(HttpServletRequest request) {
|
||||
String contentType = request.getContentType();
|
||||
if (contentType != null) {
|
||||
return StringUtils.startsWithIgnoreCase(contentType, MediaType.APPLICATION_JSON_VALUE);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.ruoyi.framework.config;
|
||||
|
||||
import com.yomahub.tlog.core.aop.AspectLogAop;
|
||||
import com.yomahub.tlog.feign.filter.TLogFeignFilter;
|
||||
import com.yomahub.tlog.spring.TLogPropertyInit;
|
||||
import com.yomahub.tlog.spring.TLogSpringAware;
|
||||
import com.yomahub.tlog.springboot.property.TLogProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 整合 TLog 框架配置
|
||||
*
|
||||
* @author Lion Li
|
||||
* @since 3.3.0
|
||||
*/
|
||||
@Order(-999)
|
||||
@Configuration
|
||||
@Import(TLogProperty.class)
|
||||
public class TLogConfig {
|
||||
|
||||
@Bean
|
||||
public TLogPropertyInit tLogPropertyInit(TLogProperty tLogProperty) {
|
||||
TLogPropertyInit tLogPropertyInit = new TLogPropertyInit();
|
||||
tLogPropertyInit.setPattern(tLogProperty.getPattern());
|
||||
tLogPropertyInit.setEnableInvokeTimePrint(tLogProperty.enableInvokeTimePrint());
|
||||
tLogPropertyInit.setIdGenerator(tLogProperty.getIdGenerator());
|
||||
tLogPropertyInit.setMdcEnable(tLogProperty.getMdcEnable());
|
||||
return tLogPropertyInit;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TLogSpringAware tLogSpringAware(){
|
||||
return new TLogSpringAware();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AspectLogAop aspectLogAop() {
|
||||
return new AspectLogAop();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TLogFeignFilter tLogFeignFilter() {
|
||||
return new TLogFeignFilter();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue