How do I log HTTP requests/responses in Netty 4.0.

2019-08-07 06:22发布

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());

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?

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.

Suggestions? Thanks!

标签: netty
1条回答
对你真心纯属浪费
2楼-- · 2019-08-07 07:19

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.

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.

查看更多
登录 后发表回答