当在客户端和服务端的ChannelInitializer继承类中添加.addLast(“logging”, new LoggingHandler(LogLevel.INFO))这行代码时
Netty就会以给定的日志级别打印出LoggingHandler中的日志。
可以对入站\出站事件进行日志记录,从而方便我们进行问题排查。
public class NettyClientChannelInitializer extends ChannelInitializer<SocketChannel> {
//给pipeline设置处理器
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline p = channel.pipeline();
p.addLast("logging",new LoggingHandler(LogLevel.INFO)); //Netty自带的日志记录handler,这个handler使用Netty的日志框架打印日志,可以打印Netty的运行日志
p.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); 向pipeline加入解码器
p.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); 向pipeline加入编码器
//找到管道,添加handler
p.addLast(new NettyClientHandler2());
}
}
假如现在添加这行代码访问http://127.0.0.1:8007/Action?name=1234510
19:10:52.089 [nioEventLoopGroup-2-6] INFO io.netty.handler.logging.LoggingHandler - [id: 0x4a9db561, L:/127.0.0.1:8007 - R:/127.0.0.1:53151] REGISTERED
19:10:52.089 [nioEventLoopGroup-2-6] INFO io.netty.handler.logging.LoggingHandler - [id: 0x4a9db561, L:/127.0.0.1:8007 - R:/127.0.0.1:53151] ACTIVE
19:10:52.090 [nioEventLoopGroup-2-6] DEBUG com.bihang.seaya.server.handler.SeayaHandler - io.netty.handler.codec.http.DefaultHttpRequest
19:10:52.090 [nioEventLoopGroup-2-6] DEBUG com.bihang.seaya.server.handler.SeayaHandler - uri/Action?name=1234510
19:10:52.090 [nioEventLoopGroup-2-6] INFO io.netty.handler.logging.LoggingHandler - [id: 0x4a9db561, L:/127.0.0.1:8007 - R:/127.0.0.1:53151] CLOSE
19:10:52.090 [nioEventLoopGroup-2-6] INFO io.netty.handler.logging.LoggingHandler - [id: 0x4a9db561, L:/127.0.0.1:8007 ! R:/127.0.0.1:53151] INACTIVE
19:10:52.090 [nioEventLoopGroup-2-6] INFO io.netty.handler.logging.LoggingHandler - [id: 0x4a9db561, L:/127.0.0.1:8007 ! R:/127.0.0.1:53151] UNREGISTERED
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {
//给pipeline设置处理器
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline p = channel.pipeline();
p.addLast("logging",new LoggingHandler(LogLevel.INFO)); //Netty自带的日志记录handler,这个handler使用Netty的日志框架打印日志,可以打印Netty的运行日志
p.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); 向pipeline加入解码器
p.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); 向pipeline加入编码器
//找到管道,添加handler
p.addLast(new NettyClientHandler2());
}
}
如果没有这行代码的打印信息
19:15:02.292 [nioEventLoopGroup-2-2] DEBUG com.bihang.seaya.server.handler.SeayaHandler - io.netty.handler.codec.http.DefaultHttpRequest
19:15:02.292 [nioEventLoopGroup-2-2] DEBUG com.bihang.seaya.server.handler.SeayaHandler - uri/Action?name=1234510