Tlog SpringBoot3.x版本无法正常打印TraceId等数据
问题:Springboot3.0版本使用Tlog(1.5.1版本)开源框架时无法打印指定参数
原因:在Java EE 8及更高版本中,javax.servlet.*包已经替换成了jakarta.servlet.*,但是tlog官方只更新到了1.5.1版本所以还没支持到
解决方法:重写tlog中TLogServletFilter,TLogWebCommon两个关键类将javax.servlet包的东西替换成jakarta.servlet包的就可以了
话不多说,直接上代码
TLogServletFilter重写
import com.my.TLogWebCommon;
import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.context.TLogContext;
import java.io.IOException;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* 支持servlet
* @author Bryan.Zhang
* @since 1.3.5
*/
public class TLogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
//里面可以加入自定义的一些参数
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse){
try{
TLogWebCommon.loadInstance().preHandle((HttpServletRequest)request);
//把traceId放入response的header,为了方便有些人有这样的需求,从前端拿整条链路的traceId
((HttpServletResponse)response).addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId());
chain.doFilter(request, response);
return;
}finally {
TLogWebCommon.loadInstance().afterCompletion();
}
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
TLogWebCommon类重写
import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.core.rpc.TLogLabelBean;
import com.yomahub.tlog.core.rpc.TLogRPCHandler;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TLog web这块的逻辑封装类
*/
public class TLogWebCommon extends TLogRPCHandler {
private final static Logger log = LoggerFactory.getLogger(TLogWebCommon.class);
private static volatile TLogWebCommon tLogWebCommon;
public static TLogWebCommon loadInstance() {
if (tLogWebCommon == null) {
synchronized (TLogWebCommon.class) {
if (tLogWebCommon == null) {
tLogWebCommon = new TLogWebCommon();
}
}
}
return tLogWebCommon;
}
public void preHandle(HttpServletRequest request) {
String traceId = request.getHeader(TLogConstants.TLOG_TRACE_KEY);
String spanId = request.getHeader(TLogConstants.TLOG_SPANID_KEY);
String preIvkApp = request.getHeader(TLogConstants.PRE_IVK_APP_KEY);
String preIvkHost = request.getHeader(TLogConstants.PRE_IVK_APP_HOST);
String preIp = request.getHeader(TLogConstants.PRE_IP_KEY);
TLogLabelBean labelBean = new TLogLabelBean(preIvkApp, preIvkHost, preIp, traceId, spanId);
processProviderSide(labelBean);
}
public void afterCompletion() {
cleanThreadLocal();
}
}
注册自己写的过滤器
@Configuration
@ComponentScan(value = "com.yomahub.tlog")
public class LogConfig {
@Bean
public FilterRegistrationBean<TLogFilter> loggingFilter() {
FilterRegistrationBean<TLogFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TLogFilter());
registrationBean.addUrlPatterns("/*"); // 拦截所有请求路径
return registrationBean;
}
}
涉及到的依赖包
<!-- 默认有无需引入,本文因为将代码抽取到了公共模块所以引入了该包 -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>tlog-all-spring-boot-starter</artifactId>
<version>1.5.1</version>
</dependency>