Netty中的LoggingHandler

当在客户端和服务端的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
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值