netty loggingHandler日志
相关类与接口
LoggingHandler:默认日志级别为debug
@Sharable
public class LoggingHandler extends ChannelDuplexHandler {
private static final LogLevel DEFAULT_LEVEL;
protected final InternalLogger logger;
protected final InternalLogLevel internalLevel;
private final LogLevel level;
private final ByteBufFormat byteBufFormat;
public LoggingHandler() {
public LoggingHandler(ByteBufFormat format) {
public LoggingHandler(LogLevel level) {
public LoggingHandler(LogLevel level, ByteBufFormat byteBufFormat) {
public LoggingHandler(Class<?> clazz) {
public LoggingHandler(Class<?> clazz, LogLevel level) {
public LoggingHandler(Class<?> clazz, LogLevel level, ByteBufFormat byteBufFormat) {
public LoggingHandler(String name) {
public LoggingHandler(String name, LogLevel level) {
public LoggingHandler(String name, LogLevel level, ByteBufFormat byteBufFormat) {
public LogLevel level() {
public ByteBufFormat byteBufFormat() {
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
if (this.logger.isEnabled(this.internalLevel)) {
this.logger.log(this.internalLevel, this.format(ctx, "REGISTERED"));
} //每个方法都调用该方法进行相应的日志级别输出
ctx.fireChannelRegistered();
}
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
public void flush(ChannelHandlerContext ctx) throws Exception {
protected String format(ChannelHandlerContext ctx, String eventName) {
protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
protected String format(ChannelHandlerContext ctx, String eventName, Object firstArg, Object secondArg) {
private String formatByteBuf(ChannelHandlerContext ctx, String eventName, ByteBuf msg) {
private String formatByteBufHolder(ChannelHandlerContext ctx, String eventName, ByteBufHolder msg) {
private static String formatSimple(ChannelHandlerContext ctx, String eventName, Object msg) {
static {
DEFAULT_LEVEL = LogLevel.DEBUG;
} //默认日志级别为debug
}
使用示例
CustomHttpHandler
public class CustomHttpHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
switch (fullHttpRequest.method().name()){
case "GET": processGetRequest(fullHttpRequest); break;
case "POST": {
if (fullHttpRequest.headers().get("Content-Type").contains("x-www-form-urlencoded")){
processPostFormRequest(fullHttpRequest);
}else if (fullHttpRequest.headers().get("Content-Type").contains("application/json")){
processPostJsonRequest(fullHttpRequest);
}
}
}
ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
buf.writeCharSequence("success", StandardCharsets.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
response.headers().set("Content-Type","application/json;charset=UTF-8");
response.headers().set("Content-Length",response.content().readableBytes());
channelHandlerContext.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
private void processGetRequest(FullHttpRequest request){
System.out.println("处理get请求");
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
Map<String, List<String>> params = decoder.parameters();
params.forEach((key, value) -> System.out.println(key + " ==> "+ value));
}
private void processPostFormRequest(FullHttpRequest request){
System.out.println("处理post form请求");
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request);
List<InterfaceHttpData> httpDataList = decoder.getBodyHttpDatas();
httpDataList.forEach(item -> {
if (item.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute){
Attribute attribute = (Attribute) item;
try {
System.out.println(attribute.getName() + " ==> " + attribute.getValue());
}catch (Exception e){
e.printStackTrace();
}
}
});
}
private void processPostJsonRequest(FullHttpRequest request){
System.out.println("处理post json请求");
ByteBuf content = request.content();
byte[] bytes = new byte[content.readableBytes()];
content.readBytes(bytes);
JSONObject jsonObject = JSONObject.parseObject(new String(bytes));
jsonObject.getInnerMap().forEach((key,value) -> System.out.println(key + " ==> " + value));
}
}
NettyServer
public class NettyServer {
public static void startServer(int port){
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline channelPipeline = socketChannel.pipeline();
//channelPipeline.addLast(new LoggingHandler());
channelPipeline.addLast(new HttpRequestDecoder());
channelPipeline.addLast(new HttpResponseEncoder());
channelPipeline.addLast(new HttpObjectAggregator(65535));
channelPipeline.addLast(new CustomHttpHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
startServer(8000);
}
}
启动应用,客户端post调用
不添加loggingHandler,控制台输出:
17:13:29.186 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
17:13:29.187 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@25584ae4
17:13:29.226 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
17:13:29.228 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
17:13:29.228 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.chunkSize: 32
17:13:29.228 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.blocking: false
处理post json请求
name ==> 瓜田李下
age ==> 20
loggingHandler没有日志输出
添加loggingHandler,控制台输出:
17:12:36.584 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
17:12:36.584 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
17:12:36.585 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@12cc906a
17:12:36.628 [nioEventLoopGroup-3-1] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x4b781faf, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51965] REGISTERED
17:12:36.628 [nioEventLoopGroup-3-1] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x4b781faf, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51965] ACTIVE
17:12:36.630 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51966] REGISTERED
17:12:36.630 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51966] ACTIVE
17:12:36.633 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
17:12:36.633 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
17:12:36.633 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.chunkSize: 32
17:12:36.633 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.blocking: false
17:12:36.641 [nioEventLoopGroup-3-1] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x4b781faf, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51965] READ COMPLETE
17:12:36.642 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51966] READ: 304B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 50 4f 53 54 20 2f 68 65 6c 6c 6f 20 48 54 54 50 |POST /hello HTTP|
|00000010| 2f 31 2e 31 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 |/1.1..Content-Ty|
|00000020| 70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f |pe: application/|
|00000030| 6a 73 6f 6e 0d 0a 55 73 65 72 2d 41 67 65 6e 74 |json..User-Agent|
|00000040| 3a 20 50 6f 73 74 6d 61 6e 52 75 6e 74 69 6d 65 |: PostmanRuntime|
|00000050| 2f 37 2e 32 39 2e 30 0d 0a 41 63 63 65 70 74 3a |/7.29.0..Accept:|
|00000060| 20 2a 2f 2a 0d 0a 50 6f 73 74 6d 61 6e 2d 54 6f | */*..Postman-To|
|00000070| 6b 65 6e 3a 20 63 65 36 64 36 66 30 34 2d 35 32 |ken: ce6d6f04-52|
|00000080| 62 62 2d 34 36 37 38 2d 62 38 38 31 2d 33 32 34 |bb-4678-b881-324|
|00000090| 38 37 37 65 66 37 61 63 39 0d 0a 48 6f 73 74 3a |877ef7ac9..Host:|
|000000a0| 20 6c 6f 63 61 6c 68 6f 73 74 3a 38 30 30 30 0d | localhost:8000.|
|000000b0| 0a 41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 |.Accept-Encoding|
|000000c0| 3a 20 67 7a 69 70 2c 20 64 65 66 6c 61 74 65 2c |: gzip, deflate,|
|000000d0| 20 62 72 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a | br..Connection:|
|000000e0| 20 6b 65 65 70 2d 61 6c 69 76 65 0d 0a 43 6f 6e | keep-alive..Con|
|000000f0| 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 34 35 0d |tent-Length: 45.|
|00000100| 0a 0d 0a 7b 0a 20 20 20 20 22 6e 61 6d 65 22 3a |...{. "name":|
|00000110| 20 22 e7 93 9c e7 94 b0 e6 9d 8e e4 b8 8b 22 2c | "............",|
|00000120| 0a 20 20 20 20 22 61 67 65 22 3a 20 32 30 0a 7d |. "age": 20.}|
+--------+-------------------------------------------------+----------------+
17:12:36.642 [nioEventLoopGroup-3-1] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x4b781faf, L:/0:0:0:0:0:0:0:1:8000 ! R:/0:0:0:0:0:0:0:1:51965] INACTIVE
17:12:36.644 [nioEventLoopGroup-3-1] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x4b781faf, L:/0:0:0:0:0:0:0:1:8000 ! R:/0:0:0:0:0:0:0:1:51965] UNREGISTERED
处理post json请求
name ==> 瓜田李下
age ==> 20
17:12:36.745 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51966] WRITE: 91B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d |HTTP/1.1 200 OK.|
|00000010| 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 |.Content-Type: a|
|00000020| 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 3b |pplication/json;|
|00000030| 63 68 61 72 73 65 74 3d 55 54 46 2d 38 0d 0a 43 |charset=UTF-8..C|
|00000040| 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 37 |ontent-Length: 7|
|00000050| 0d 0a 0d 0a 73 75 63 63 65 73 73 |....success |
+--------+-------------------------------------------------+----------------+
17:12:36.746 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51966] FLUSH
17:12:36.748 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 - R:/0:0:0:0:0:0:0:1:51966] CLOSE
17:12:36.748 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 ! R:/0:0:0:0:0:0:0:1:51966] READ COMPLETE
17:12:36.748 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 ! R:/0:0:0:0:0:0:0:1:51966] INACTIVE
17:12:36.748 [nioEventLoopGroup-3-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x21e2c6d6, L:/0:0:0:0:0:0:0:1:8000 ! R:/0:0:0:0:0:0:0:1:51966] UNREGISTERED