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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty是一个基于Java的异步事件驱动的网络应用框架,它提供了高性能、可扩展的网络编程能力。Netty的核心是NIO(非阻塞I/O)模型,它允许在单个线程处理多个并发连接,从而提高了网络应用的吞吐量和性能。 Netty提供了许多开箱即用的组件,其包括HTTP服务器。通过使用Netty的HTTP服务器组件,您可以轻松地构建和部署自己的HTTP服务器。 下面是一个简单的示例,演示如何使用Netty构建一个简单的HTTP服务器: ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; public class HttpServerExample { public static void main(String[] args) throws Exception { // 创建两个EventLoopGroup,一个用于接收连接,一个用于处理连接的I/O操作 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { // 创建ServerBootstrap实例,用于引导和绑定服务器 ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // 添加HTTP编解码器 ch.pipeline().addLast(new HttpServerCodec()); // 添加自定义的HTTP请求处理器 ch.pipeline().addLast(new HttpServerHandler()); } }); // 绑定端口并启动服务器 ChannelFuture future = serverBootstrap.bind(8080).sync(); System.out.println("Netty HTTP Server started on port 8080."); // 等待服务器关闭 future.channel().closeFuture().sync(); } finally { // 关闭EventLoopGroup bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } ``` 在上面的示例,我们创建了一个`ServerBootstrap`实例,并配置了两个`EventLoopGroup`,一个用于接收连接,一个用于处理连接的I/O操作。然后,我们指定了服务器的通道类型为`NioServerSocketChannel`,并添加了一个`LoggingHandler`用于打印日志。接下来,我们创建了一个`ChannelInitializer`,并在其添加了一个`HttpServerCodec`用于处理HTTP编解码,以及一个自定义的`HttpServerHandler`用于处理HTTP请求。最后,我们绑定了服务器的端口并启动服务器。 请注意,上述示例的`HttpServerHandler`是一个自定义的处理器,您可以根据自己的需求来实现它。它负责处理接收到的HTTP请求,并返回相应的HTTP响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值