如何在Netty 4.0.0.Beta1中记录HTTP请求/响应? [英] How do I log HTTP requests/responses in Netty 4.0.0.Beta1?

查看:64
本文介绍了如何在Netty 4.0.0.Beta1中记录HTTP请求/响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Netty 4.0.0.Beta1,对我来说,将传入/传出HTTP流量记录到基于Netty的服务器的最佳方法是什么?我的管道当前为:

Using Netty 4.0.0.Beta1, what would be the best way for me to log the incoming/outgoing HTTP traffic to a netty-based server? My pipeline is currently:

p.addLast("decoder", new HttpRequestDecoder());
p.addLast("aggregator", new HttpObjectAggregator(1048576));

p.addLast("encoder", new HttpResponseEncoder());
p.addLast("handler", new MyBusinessLogicHandler());

我尝试编写一个实现ChannelInboundMessageHandler<FullHttpRequest>的处理程序,然后在inboundBufferUpdated(ChannelHandlerContext ctx)方法中进行日志记录,该方法对于传入的请求似乎可以正常工作,但这是推荐的方法吗?

I tried writing a handler that implements ChannelInboundMessageHandler<FullHttpRequest>, and then does the logging in the inboundBufferUpdated(ChannelHandlerContext ctx) method, which seems to work fine for incoming requests, but is that the recommended way?

当我尝试实现ChannelOutboundMessageHandler<FullHttpResponse>时,未能成功看到flush(ChannelHandlerContext ctx, ChannelPromise promise)方法中的实际FullHttpResponse对象.

When I tried to implement ChannelOutboundMessageHandler<FullHttpResponse>, I wasn't successful in seeing the actual FullHttpResponse objects inside the flush(ChannelHandlerContext ctx, ChannelPromise promise) method.

建议?谢谢!

推荐答案

注意:自测试版发布以来,Netty API发生了很大变化.现在,您只需在管道中添加LoggingHandler. MessageLoggingHandlerByteLoggingHandler不见了.

Note: The Netty API has changed a lot since its beta days. Now you can just add a LoggingHandler in the pipeline. MessageLoggingHandler and ByteLoggingHandler are gone.

您可以将MessageLoggingHandler放在管道中的编解码器处理程序之后(即MyBusinessLogicHandler之前).默认情况下,它将在DEBUG级别记录所有消息和事件,因此您可能需要进行调整.如果您对低级流量转储比较感兴趣,请使用ByteLoggingHandler并将其放在编解码器处理程序之前.

You can put MessageLoggingHandler after the codec handlers in your pipeline (i.e before MyBusinessLogicHandler). By default, it logs all messages and events at DEBUG level, so you might want to adjust that. If you are rather interested in low-level traffic dump, please use ByteLoggingHandler and place it before the codec handlers.

这篇关于如何在Netty 4.0.0.Beta1中记录HTTP请求/响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆