|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.fluss.rpc.netty; |
| 18 | + |
| 19 | +import com.alibaba.fluss.shaded.netty4.io.netty.buffer.ByteBuf; |
| 20 | +import com.alibaba.fluss.shaded.netty4.io.netty.buffer.ByteBufHolder; |
| 21 | +import com.alibaba.fluss.shaded.netty4.io.netty.channel.ChannelHandlerContext; |
| 22 | +import com.alibaba.fluss.shaded.netty4.io.netty.handler.logging.LogLevel; |
| 23 | +import com.alibaba.fluss.shaded.netty4.io.netty.handler.logging.LoggingHandler; |
| 24 | + |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | + |
| 31 | +/* This file is based on source code of Apache Spark Project (https://spark.apache.org/), licensed by the Apache |
| 32 | + * Software Foundation (ASF) under the Apache License, Version 2.0. See the NOTICE file distributed with this work for |
| 33 | + * additional information regarding copyright ownership. */ |
| 34 | + |
| 35 | +/** A Netty logger that constructs a log handler depending on the log level. */ |
| 36 | +public class NettyLogger { |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(NettyLogger.class); |
| 38 | + |
| 39 | + /** A Netty LoggingHandler which does not dump the message contents. */ |
| 40 | + private static class NoContentLoggingHandler extends LoggingHandler { |
| 41 | + |
| 42 | + NoContentLoggingHandler(Class<?> clazz, LogLevel level) { |
| 43 | + super(clazz, level); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected String format(ChannelHandlerContext ctx, String eventName, Object arg) { |
| 48 | + if (arg instanceof ByteBuf) { |
| 49 | + return format(ctx, eventName) + " " + ((ByteBuf) arg).readableBytes() + "B"; |
| 50 | + } else if (arg instanceof ByteBufHolder) { |
| 51 | + return format(ctx, eventName) |
| 52 | + + " " |
| 53 | + + ((ByteBufHolder) arg).content().readableBytes() |
| 54 | + + "B"; |
| 55 | + } else if (arg instanceof InputStream) { |
| 56 | + int available = -1; |
| 57 | + try { |
| 58 | + available = ((InputStream) arg).available(); |
| 59 | + } catch (IOException ex) { |
| 60 | + // Swallow, but return -1 to indicate an error happened |
| 61 | + } |
| 62 | + return format(ctx, eventName, arg) + " " + available + "B"; |
| 63 | + } else { |
| 64 | + return super.format(ctx, eventName, arg); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private final LoggingHandler loggingHandler; |
| 70 | + |
| 71 | + public NettyLogger() { |
| 72 | + if (logger.isTraceEnabled()) { |
| 73 | + loggingHandler = new LoggingHandler(NettyLogger.class, LogLevel.TRACE); |
| 74 | + } else if (logger.isDebugEnabled()) { |
| 75 | + loggingHandler = new NoContentLoggingHandler(NettyLogger.class, LogLevel.DEBUG); |
| 76 | + } else { |
| 77 | + loggingHandler = null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public LoggingHandler getLoggingHandler() { |
| 82 | + return loggingHandler; |
| 83 | + } |
| 84 | +} |
0 commit comments