We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
package com.elite.common.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.ContentCachingResponseWrapper; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 记录完整请求响应日志 * */ @Slf4j @Configuration public class WebConfig { @Bean public OncePerRequestFilter contentCachingRequestFilter() { return new OncePerRequestFilter() { @Override protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException { ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request); ContentCachingResponseWrapper wrappedResponse = new ContentCachingResponseWrapper(response); filterChain.doFilter(wrappedRequest, wrappedResponse); log.info("http request:{}", new String(wrappedRequest.getContentAsByteArray())); log.info("http response:{}", new String(wrappedResponse.getContentAsByteArray())); wrappedResponse.copyBodyToResponse(); } }; } }
如上,我这里打印了http请求和响应日志,但是并没有traceId
The text was updated successfully, but these errors were encountered:
你如果用到了 tlog-web-spring-boot-starter 这个引入方式, TLog默认使用的是TLogWebInterceptor拦截器操作TLogContext上下文信息,因为过滤器前置是先于拦截器前置执行的,所以过滤器链调用前置操作的log输出打印不出来,对于过滤器链调用后置操作的log是因为过滤器的后置是晚于拦截器的后置执行的,已经被TLogWebInterceptor后置操作清除了,所以也打印不出来,这个是实现机制导致的,有三种解决办法:
@SpringBootApplication(exclude = {TLogWebAutoConfiguration.class})
package com.example.demo; import com.yomahub.tlog.web.filter.TLogServletFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; @Configuration public class LogFilterConfig { @Bean public FilterRegistrationBean<Filter> tLogServletFilter() { FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>(); registration.setFilter(new TLogServletFilter()); registration.addUrlPatterns("/*"); return registration; } }
package com.example.demo; import com.yomahub.tlog.constant.TLogConstants; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author Oct */ @Slf4j @Component public class NormalFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.debug("normal-filter - before"); filterChain.doFilter(servletRequest, servletResponse); String traceId = null; if (servletResponse instanceof HttpServletResponse) { traceId = ((HttpServletResponse) servletResponse).getHeader(TLogConstants.TLOG_TRACE_KEY); } log.debug("[{}] normal-filter - after", traceId); } }
Sorry, something went wrong.
No branches or pull requests
如上,我这里打印了http请求和响应日志,但是并没有traceId
The text was updated successfully, but these errors were encountered: