2团
Published on 2025-04-09 / 1 Visits
0
0

Netty中LoggingHandler输出日志展示

1. 前言

Netty中提供了LoggingHandler,便于开发者快速定位网络事件。此处简单记录下一次PING-PONG的日志输出。

2. 服务端/客户端实现

2.1 服务端

public class TestServer {

    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .handler(new LoggingHandler(LogLevel.INFO)) // 添加 LoggingHandler
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(new LoggingHandler(LogLevel.INFO)); // 为每个连接添加 LoggingHandler
                            // 添加其他处理器
                        }
                    });

            ChannelFuture f = b.bind(8080).sync();
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

2.2 客户端

public class TestClient {
    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(new LoggingHandler(LogLevel.INFO)); // 添加 LoggingHandler
                            // 添加其他处理器
                        }
                    });

            Channel ch = b.connect("localhost", 8080).sync().channel();

            // 发送消息
            ByteBuf buf = ch.alloc().buffer();
            buf.writeBytes("Hello, Netty!".getBytes());
            ch.writeAndFlush(buf);

            // 等待关闭
            ch.closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}

3. 日志输出

3.1 服务端日志

2025-04-09 15:01:08 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x551600f3] REGISTERED
2025-04-09 15:01:08 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x551600f3] BIND: 0.0.0.0/0.0.0.0:8080
2025-04-09 15:01:08 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x551600f3, L:/0:0:0:0:0:0:0:0:8080] ACTIVE
2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x551600f3, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0x7132de4e, L:/127.0.0.1:8080 - R:/127.0.0.1:9025]
2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x551600f3, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
2025-04-09 15:01:26 INFO [nioEventLoopGroup-3-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x7132de4e, L:/127.0.0.1:8080 - R:/127.0.0.1:9025] REGISTERED
2025-04-09 15:01:26 INFO [nioEventLoopGroup-3-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x7132de4e, L:/127.0.0.1:8080 - R:/127.0.0.1:9025] ACTIVE
2025-04-09 15:01:26 INFO [nioEventLoopGroup-3-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x7132de4e, L:/127.0.0.1:8080 - R:/127.0.0.1:9025] READ: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 2c 20 4e 65 74 74 79 21          |Hello, Netty!   |
+--------+-------------------------------------------------+----------------+
2025-04-09 15:01:26 INFO [nioEventLoopGroup-3-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x7132de4e, L:/127.0.0.1:8080 - R:/127.0.0.1:9025] READ COMPLETE

3.2 客户端输出

2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x9150316d] REGISTERED
2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x9150316d] CONNECT: localhost/127.0.0.1:8080
2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x9150316d, L:/127.0.0.1:9025 - R:localhost/127.0.0.1:8080] ACTIVE
2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x9150316d, L:/127.0.0.1:9025 - R:localhost/127.0.0.1:8080] WRITE: 13B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 65 6c 6c 6f 2c 20 4e 65 74 74 79 21          |Hello, Netty!   |
+--------+-------------------------------------------------+----------------+
2025-04-09 15:01:26 INFO [nioEventLoopGroup-2-1] io.netty.handler.logging.LoggingHandler Line:150 - [id: 0x9150316d, L:/127.0.0.1:9025 - R:localhost/127.0.0.1:8080] FLUSH


Comment