开发者社区> 问答> 正文

在WebFlux中格式化Wiretap输出

我按照https://www.baeldung.com/spring-log-webclient-calls进行操作,最终得到以下代码来格式化Netty的HttpClient转储的日志

public class HttpLoggingHandler extends LoggingHandler {

    public HttpLoggingHandler(Class<?> clazz) {
        super(clazz);
    }

    @Override
    protected String format(ChannelHandlerContext ctx, String event, Object arg) {
        if (arg instanceof ByteBuf) {
            ByteBuf msg = (ByteBuf) arg;
            String output = msg.toString(StandardCharsets.UTF_8);
            return output;
        }

        return super.format(ctx, event, arg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        ctx.fireChannelActive();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelReadComplete();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelUnregistered();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelInactive();
    }

    @Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

}

我最终得到了以下日志输出。

2019-12-26 15:14:17.450 DEBUG 30176 --- [ctor-http-nio-7] reactor.netty.http.client.HttpClient     : [id: 0x3638d952] CONNECT: localhost/127.0.0.1:8082
2019-12-26 15:14:17.455 DEBUG 30176 --- [ctor-http-nio-7] r.netty.http.client.HttpClientConnect    : [id: 0x3638d952, L:/127.0.0.1:61250 - R:localhost/127.0.0.1:8082] Handler is being applied: {uri=http://localhost:8082/enhanceAndSendForProcessing/, method=POST}
2019-12-26 15:14:17.477 DEBUG 30176 --- [ctor-http-nio-7] reactor.netty.http.client.HttpClient     : POST /enhanceAndSendForProcessing/ HTTP/1.1
host: localhost:8082
Accept: application/json
User-Agent: I'm a teapot
Content-Type: application/json
cookie: cookieKey=cookieValue
cookie: cookieKey=teapot
cookie: cookieKey=amsterdam
cookie: secretToken=f9a68c54-1242-40da-ad08-cce575fad42c
content-length: 96

{"id":4,"productId":4,"customer":{"firstName":"John","lastName":"Doe","email":"john@gmail.com"}}
2019-12-26 15:14:17.495 DEBUG 30176 --- [ctor-http-nio-7] reactor.netty.http.client.HttpClient     : HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 1

4
2019-12-26 15:14:17.496 DEBUG 30176 --- [ctor-http-nio-7] r.n.http.client.HttpClientOperations     : [id: 0x3638d952, L:/127.0.0.1:61250 - R:localhost/127.0.0.1:8082] Received response (auto-read:false) : [Content-Type=application/json, Content-Length=1]
2019-12-26 15:14:17.506 DEBUG 30176 --- [ctor-http-nio-7] r.n.http.client.HttpClientOperations     : [id: 0x3638d952, L:/127.0.0.1:61250 - R:localhost/127.0.0.1:8082] Received last HTTP packet

这是对十六进制转储的巨大改进,但是有没有办法识别标题/ cookie和请求/响应主体?我看到请求日志的最后一行是请求正文

{"id":4,"productId":4,"customer":{"firstName":"John","lastName":"Doe","email":"john@gmail.com"}}

但有时请求主体会像这样单独转储:

2019-12-26 15:14:17.495 DEBUG 30176 --- [ctor-http-nio-7] reactor.netty.http.client.HttpClient     : {"id":4,"productId":4,"customer"{"firstName":"John","lastName":"Doe","email":"john@gmail.com"}}

我尝试arg以的身份进行检查instanceof HttpContent/HttpRequest,但没有运气。

我可以通过使用cookieBytebuf输出中的键来获取标头(使用HTTPHeaders映射)和cookie ,但是我不确定要获取请求/响应主体的方法,因为它以不同的顺序以不同的顺序打印。有办法解决吗?

展开
收起
垚tutu 2019-12-26 18:21:13 1097 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载