提交 7ac95a63 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:日志打印

上级 3c661aa2
...@@ -34,7 +34,7 @@ public class TestEventLoop_01_server { ...@@ -34,7 +34,7 @@ public class TestEventLoop_01_server {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg; ByteBuf buf = (ByteBuf) msg;
log.info(buf.toString(Charset.defaultCharset())); log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
ctx.fireChannelRead(msg); ctx.fireChannelRead(msg);
} }
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() { }).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
...@@ -42,7 +42,7 @@ public class TestEventLoop_01_server { ...@@ -42,7 +42,7 @@ public class TestEventLoop_01_server {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg; ByteBuf buf = (ByteBuf) msg;
log.info(buf.toString(Charset.defaultCharset())); log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
} }
}); });
} }
......
...@@ -7,13 +7,15 @@ import io.netty.channel.ChannelInitializer; ...@@ -7,13 +7,15 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Scanner; import java.util.Scanner;
/** /**
* ChannelFuture * ChannelFuture和LoggingHandler使用
* *
* @author : qinyingjie * @author : qinyingjie
* @version : 2.2.0 * @version : 2.2.0
...@@ -28,10 +30,11 @@ public class TestEventLoop_05_ChannelFuture { ...@@ -28,10 +30,11 @@ public class TestEventLoop_05_ChannelFuture {
.group(new NioEventLoopGroup()) .group(new NioEventLoopGroup())
//3.选择客户端channel实现 //3.选择客户端channel实现
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
// 4.添加处理器 // 4.添加处理器4
.handler(new ChannelInitializer<NioSocketChannel>() { .handler(new ChannelInitializer<NioSocketChannel>() {
@Override //在连接建立后被调用 @Override //在连接建立后被调用
protected void initChannel(NioSocketChannel ch) throws Exception { protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new StringEncoder());
} }
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册