Spring云网关,记录请求/响应

ep6jt1vc  于 2023-01-02  发布在  Spring
关注(0)|答案(1)|浏览(160)

我的全局筛选器仅记录成功的请求(200)。例如,代码500不通过ServerHttpRequestDecorator和ServerHttpResponseDecorator。

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
    ServerHttpRequestDecorator requestMutated = new ServerHttpRequestDecorator(exchange.getRequest()) {
        @Override
        public Flux<DataBuffer> getBody() {
            Logger requestLogger = new Logger(getDelegate());
            if(LOGGABLE_CONTENT_TYPES.contains(String.valueOf(getHeaders().getContentType()).toLowerCase())) {
                return super.getBody().map(ds -> {
                    requestLogger.appendBody(ds.asByteBuffer());
                    return ds;
                }).doFinally(s -> requestLogger.log());
            } else {
                requestLogger.log();
                return super.getBody();
            }
        }
    };

    ServerHttpResponseDecorator responseMutated = new ServerHttpResponseDecorator(exchange.getResponse()) {
        @Override
        public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
            Logger responseLogger = new Logger(getDelegate());
            if(LOGGABLE_CONTENT_TYPES.contains(String.valueOf(getHeaders().getContentType()).toLowerCase())) {
                return join(body).flatMap(db -> {
                    responseLogger.appendBody(db.asByteBuffer());
                    responseLogger.log();
                    return getDelegate().writeWith(Mono.just(db));
                });
            } else {
                responseLogger.log();
                return getDelegate().writeWith(body);
            }
        }
    };
    return chain.filter(exchange.mutate().request(requestMutated).response(responseMutated).build());

ServerResponse.status(HttpStatus.valueOf(statusCode)) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(errorPropertiesMap)) =〉不记录请求和响应;
我做错什么了?谢谢你的帮助。

yzuktlbb

yzuktlbb1#

尝试在application.yml中启用这些属性:

logging:
  level:
    org.springframework.web.HttpLogging: TRACE
    reactor.netty.http.server: DEBUG
    reactor.netty.http.client: DEBUG

相关问题