Remove unnecessary logging handler - ByteLoggingHandler and MessageLoggingHandler / Extensible log message formatiing
This commit is contained in:
parent
63403884f7
commit
b6fdac7df3
@ -1,181 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.logging;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import io.netty.channel.MessageList;
|
||||
|
||||
public class ByteLoggingHandler
|
||||
extends LoggingHandler {
|
||||
|
||||
private static final String NEWLINE = String.format("%n");
|
||||
|
||||
private static final String[] BYTE2HEX = new String[256];
|
||||
private static final String[] HEXPADDING = new String[16];
|
||||
private static final String[] BYTEPADDING = new String[16];
|
||||
private static final char[] BYTE2CHAR = new char[256];
|
||||
|
||||
static {
|
||||
int i;
|
||||
|
||||
// Generate the lookup table for byte-to-hex-dump conversion
|
||||
for (i = 0; i < 10; i ++) {
|
||||
StringBuilder buf = new StringBuilder(3);
|
||||
buf.append(" 0");
|
||||
buf.append(i);
|
||||
BYTE2HEX[i] = buf.toString();
|
||||
}
|
||||
for (; i < 16; i ++) {
|
||||
StringBuilder buf = new StringBuilder(3);
|
||||
buf.append(" 0");
|
||||
buf.append((char) ('a' + i - 10));
|
||||
BYTE2HEX[i] = buf.toString();
|
||||
}
|
||||
for (; i < BYTE2HEX.length; i ++) {
|
||||
StringBuilder buf = new StringBuilder(3);
|
||||
buf.append(' ');
|
||||
buf.append(Integer.toHexString(i));
|
||||
BYTE2HEX[i] = buf.toString();
|
||||
}
|
||||
|
||||
// Generate the lookup table for hex dump paddings
|
||||
for (i = 0; i < HEXPADDING.length; i ++) {
|
||||
int padding = HEXPADDING.length - i;
|
||||
StringBuilder buf = new StringBuilder(padding * 3);
|
||||
for (int j = 0; j < padding; j ++) {
|
||||
buf.append(" ");
|
||||
}
|
||||
HEXPADDING[i] = buf.toString();
|
||||
}
|
||||
|
||||
// Generate the lookup table for byte dump paddings
|
||||
for (i = 0; i < BYTEPADDING.length; i ++) {
|
||||
int padding = BYTEPADDING.length - i;
|
||||
StringBuilder buf = new StringBuilder(padding);
|
||||
for (int j = 0; j < padding; j ++) {
|
||||
buf.append(' ');
|
||||
}
|
||||
BYTEPADDING[i] = buf.toString();
|
||||
}
|
||||
|
||||
// Generate the lookup table for byte-to-char conversion
|
||||
for (i = 0; i < BYTE2CHAR.length; i ++) {
|
||||
if (i <= 0x1f || i >= 0x7f) {
|
||||
BYTE2CHAR[i] = '.';
|
||||
} else {
|
||||
BYTE2CHAR[i] = (char) i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ByteLoggingHandler() { }
|
||||
|
||||
public ByteLoggingHandler(Class<?> clazz, LogLevel level) {
|
||||
super(clazz, level);
|
||||
}
|
||||
|
||||
public ByteLoggingHandler(Class<?> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
public ByteLoggingHandler(LogLevel level) {
|
||||
super(level);
|
||||
}
|
||||
|
||||
public ByteLoggingHandler(String name, LogLevel level) {
|
||||
super(name, level);
|
||||
}
|
||||
|
||||
public ByteLoggingHandler(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, MessageList<Object> msgs, ChannelPromise promise) throws Exception {
|
||||
log(ctx, "WRITE", msgs);
|
||||
ctx.write(msgs, promise);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageList<Object> msgs) throws Exception {
|
||||
log(ctx, "RECEIVED", msgs);
|
||||
ctx.fireMessageReceived(msgs);
|
||||
}
|
||||
|
||||
private void log(ChannelHandlerContext ctx, String message, MessageList<Object> msgs) {
|
||||
if (logger.isEnabled(internalLevel)) {
|
||||
for (int i = 0; i < msgs.size(); i++) {
|
||||
Object msg = msgs.get(i);
|
||||
if (msg instanceof ByteBuf) {
|
||||
logger.log(internalLevel, format(ctx, formatBuffer(message, (ByteBuf) msg)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String formatBuffer(String message, ByteBuf buf) {
|
||||
int length = buf.readableBytes();
|
||||
int rows = length / 16 + (length % 15 == 0? 0 : 1) + 4;
|
||||
StringBuilder dump = new StringBuilder(rows * 80 + message.length() + 16);
|
||||
|
||||
dump.append(message).append('(').append(length).append('B').append(')');
|
||||
dump.append(
|
||||
NEWLINE + " +-------------------------------------------------+" +
|
||||
NEWLINE + " | 0 1 2 3 4 5 6 7 8 9 a b c d e f |" +
|
||||
NEWLINE + "+--------+-------------------------------------------------+----------------+");
|
||||
|
||||
final int startIndex = buf.readerIndex();
|
||||
final int endIndex = buf.writerIndex();
|
||||
|
||||
int i;
|
||||
for (i = startIndex; i < endIndex; i ++) {
|
||||
int relIdx = i - startIndex;
|
||||
int relIdxMod16 = relIdx & 15;
|
||||
if (relIdxMod16 == 0) {
|
||||
dump.append(NEWLINE);
|
||||
dump.append(Long.toHexString(relIdx & 0xFFFFFFFFL | 0x100000000L));
|
||||
dump.setCharAt(dump.length() - 9, '|');
|
||||
dump.append('|');
|
||||
}
|
||||
dump.append(BYTE2HEX[buf.getUnsignedByte(i)]);
|
||||
if (relIdxMod16 == 15) {
|
||||
dump.append(" |");
|
||||
for (int j = i - 15; j <= i; j ++) {
|
||||
dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
|
||||
}
|
||||
dump.append('|');
|
||||
}
|
||||
}
|
||||
|
||||
if ((i - startIndex & 15) != 0) {
|
||||
int remainder = length & 15;
|
||||
dump.append(HEXPADDING[remainder]);
|
||||
dump.append(" |");
|
||||
for (int j = i - remainder; j < i; j ++) {
|
||||
dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
|
||||
}
|
||||
dump.append(BYTEPADDING[remainder]);
|
||||
dump.append('|');
|
||||
}
|
||||
|
||||
dump.append(
|
||||
NEWLINE + "+--------+-------------------------------------------------+----------------+");
|
||||
|
||||
return dump.toString();
|
||||
}
|
||||
}
|
@ -303,26 +303,40 @@ public class LoggingHandler extends ChannelDuplexHandler {
|
||||
ctx.write(msgs, promise);
|
||||
}
|
||||
|
||||
private void logMessages(ChannelHandlerContext ctx, String message, MessageList<Object> msgs) {
|
||||
private void logMessages(ChannelHandlerContext ctx, String eventName, MessageList<Object> msgs) {
|
||||
if (logger.isEnabled(internalLevel)) {
|
||||
int size = msgs.size();
|
||||
for (int i = 0; i < size; i ++) {
|
||||
Object msg = msgs.get(i);
|
||||
if (msg instanceof ByteBuf) {
|
||||
logger.log(internalLevel, format(ctx, formatBuffer(message, (ByteBuf) msg)));
|
||||
} else {
|
||||
// ignore
|
||||
if (size == 0) {
|
||||
logger.log(internalLevel, format(ctx, formatEmptyMessageList(eventName)));
|
||||
} else {
|
||||
for (int i = 0; i < size; i ++) {
|
||||
Object msg = msgs.get(i);
|
||||
logger.log(internalLevel, format(ctx, formatMessage(eventName, i + 1, size, msg)));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String formatBuffer(String message, ByteBuf buf) {
|
||||
protected String formatEmptyMessageList(String eventName) {
|
||||
return eventName + "(empty)";
|
||||
}
|
||||
|
||||
protected String formatMessage(String eventName, int seq, int size, Object msg) {
|
||||
if (msg instanceof ByteBuf) {
|
||||
return formatByteBuf(eventName, seq, size, (ByteBuf) msg);
|
||||
} else {
|
||||
return formatNonByteBuf(eventName, seq, size, msg);
|
||||
}
|
||||
}
|
||||
|
||||
protected String formatByteBuf(String eventName, int seq, int size, ByteBuf buf) {
|
||||
int length = buf.readableBytes();
|
||||
int rows = length / 16 + (length % 15 == 0? 0 : 1) + 4;
|
||||
StringBuilder dump = new StringBuilder(rows * 80 + message.length() + 16);
|
||||
StringBuilder dump = new StringBuilder(rows * 80 + eventName.length() + 16);
|
||||
|
||||
dump.append(message).append('(').append(length).append('B').append(')');
|
||||
dump.append(eventName).append('(').append(seq).append('/').append(size).append(", ");
|
||||
dump.append(length).append('B').append(')');
|
||||
dump.append(
|
||||
NEWLINE + " +-------------------------------------------------+" +
|
||||
NEWLINE + " | 0 1 2 3 4 5 6 7 8 9 a b c d e f |" +
|
||||
@ -368,24 +382,7 @@ public class LoggingHandler extends ChannelDuplexHandler {
|
||||
return dump.toString();
|
||||
}
|
||||
|
||||
protected String formatBuffer(String message, Object[] msgs, int index, int length) {
|
||||
return message + '(' + length + "): " + contentToString(msgs, index, length);
|
||||
}
|
||||
|
||||
private static String contentToString(Object[] msgs, int index, int length) {
|
||||
if (length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('[');
|
||||
for (int i = index; i < length; i++) {
|
||||
Object msg = msgs[i];
|
||||
sb.append(msg);
|
||||
|
||||
if (i + 1 < length) {
|
||||
sb.append(", ");
|
||||
}
|
||||
}
|
||||
return sb.append(']').toString();
|
||||
protected String formatNonByteBuf(String eventName, int seq, int size, Object msg) {
|
||||
return eventName + '(' + seq + '/' + size + "): " + msg;
|
||||
}
|
||||
}
|
||||
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.logging;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import io.netty.channel.MessageList;
|
||||
|
||||
public class MessageLoggingHandler extends LoggingHandler {
|
||||
|
||||
public MessageLoggingHandler() { }
|
||||
|
||||
public MessageLoggingHandler(Class<?> clazz, LogLevel level) {
|
||||
super(clazz, level);
|
||||
}
|
||||
|
||||
public MessageLoggingHandler(Class<?> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
public MessageLoggingHandler(LogLevel level) {
|
||||
super(level);
|
||||
}
|
||||
|
||||
public MessageLoggingHandler(String name, LogLevel level) {
|
||||
super(name, level);
|
||||
}
|
||||
|
||||
public MessageLoggingHandler(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, MessageList<Object> msgs, ChannelPromise promise) throws Exception {
|
||||
log("WRITE", msgs);
|
||||
ctx.write(msgs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageList<Object> msgs) throws Exception {
|
||||
log("RECEIVED", msgs);
|
||||
ctx.fireMessageReceived(msgs);
|
||||
}
|
||||
|
||||
private void log(String message, MessageList<Object> msgs) {
|
||||
if (logger.isEnabled(internalLevel)) {
|
||||
logger.log(internalLevel, formatBuffer(message, msgs));
|
||||
}
|
||||
}
|
||||
|
||||
protected String formatBuffer(String message, MessageList<Object> msgs) {
|
||||
return message + '(' + msgs.size() + "): " + contentToString(msgs);
|
||||
}
|
||||
|
||||
private static String contentToString(MessageList<Object> msgs) {
|
||||
if (msgs.isEmpty()) {
|
||||
return "[]";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('[');
|
||||
int size = msgs.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Object msg = msgs.get(i);
|
||||
sb.append(msg);
|
||||
|
||||
if (i + 1 < size) {
|
||||
sb.append(", ");
|
||||
}
|
||||
}
|
||||
return sb.append(']').toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user