为什么我在不同请求之间的WebFilter中得到相同的logPrefix?

时间:2019-10-22 08:50:42

标签: spring spring-boot spring-webflux

我正在将Spring-boot 2.1.5.RELEASE与spring-framework一起使用。

对于记录请求和响应,我创建了实现LogFilter的{​​{1}}并为所有请求(如以下代码)写入日志。

WebFilter

我希望@Slf4j @Component public class LogFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { long startTime = System.currentTimeMillis(); String logPrefix = exchange.getLogPrefix(); String path = exchange.getRequest().getURI().getPath(); String remoteAddr = "(" + exchange.getRequest().getRemoteAddress().getAddress().getHostAddress() + ")"; String method = exchange.getRequest().getMethodValue(); log.info("{}HTTP {} {} {}", logPrefix, method, path, remoteAddr); return chain.filter(exchange); } } 中的logPrefix是唯一的字符串  对于每个请求。但实际上看起来像是在请求中重复使用或生成了相同的字符串。因此,日志文件如下所示。 (看来logPrefix是每个线程而不是每个请求的唯一字符串)

ServerWebExchange

这种情况正常吗?

如果是这样,如何为每个请求生成唯一的16:39:03.103 [http-epoll-8] INFO [ LogFilter:35 ] - [ca47e591] HTTP GET /monitor/l7check 16:39:08.123 [http-epoll-8] INFO [ LogFilter:35 ] - [ca47e591] HTTP GET /monitor/l7check 16:39:08.480 [http-epoll-1] INFO [ LogFilter:35 ] - [e3546756] HTTP POST /monitor/l7check 16:39:13.127 [http-epoll-8] INFO [ LogFilter:35 ] - [ca47e591] HTTP GET /monitor/l7check 16:39:18.126 [http-epoll-8] INFO [ LogFilter:35 ] - [ca47e591] HTTP GET /monitor/l7check 16:39:23.123 [http-epoll-2] INFO [ LogFilter:35 ] - [5a56750e] HTTP GET /monitor/l7check 16:39:28.122 [http-epoll-8] INFO [ LogFilter:35 ] - [ca47e591] HTTP GET /monitor/l7check

0 个答案:

没有答案