LoggingHandler does not override channelReadComplete or channelWritabilityChanged

Motivation:

`io.netty.handler.logging.LoggingHandler` does not log when these
events happen.

Modifiations:

Add overrides with logging to these methods.

Result:

Logging now happens for these two events.
This commit is contained in:
Kevin Oliver 2017-03-23 21:24:19 -07:00 committed by Scott Mitchell
parent 225d10e1ad
commit 34e0007f07
2 changed files with 40 additions and 5 deletions

View File

@ -225,10 +225,18 @@ public class LoggingHandler extends ChannelDuplexHandler {
ctx.deregister(promise);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
if (logger.isEnabled(internalLevel)) {
logger.log(internalLevel, format(ctx, "READ COMPLETE"));
}
ctx.fireChannelReadComplete();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (logger.isEnabled(internalLevel)) {
logger.log(internalLevel, format(ctx, "RECEIVED", msg));
logger.log(internalLevel, format(ctx, "READ", msg));
}
ctx.fireChannelRead(msg);
}
@ -241,6 +249,14 @@ public class LoggingHandler extends ChannelDuplexHandler {
ctx.write(msg, promise);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
if (logger.isEnabled(internalLevel)) {
logger.log(internalLevel, format(ctx, "WRITABILITY CHANGED"));
}
ctx.fireChannelWritabilityChanged();
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
if (logger.isEnabled(internalLevel)) {

View File

@ -111,6 +111,17 @@ public class LoggingHandlerTest {
verify(appender).doAppend(argThat(new RegexLogMatcher(".+ACTIVE$")));
}
@Test
public void shouldLogChannelWritabilityChanged() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
// this is used to switch the channel to become unwritable
channel.config().setWriteBufferLowWaterMark(5);
channel.config().setWriteBufferHighWaterMark(10);
channel.write("hello", channel.newPromise());
verify(appender).doAppend(argThat(new RegexLogMatcher(".+WRITABILITY CHANGED$")));
}
@Test
public void shouldLogChannelRegistered() {
new EmbeddedChannel(new LoggingHandler());
@ -194,7 +205,7 @@ public class LoggingHandlerTest {
String msg = "hello";
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
channel.writeInbound(msg);
verify(appender).doAppend(argThat(new RegexLogMatcher(".+RECEIVED: " + msg + '$')));
verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ: " + msg + '$')));
String handledMsg = channel.readInbound();
assertThat(msg, is(sameInstance(handledMsg)));
@ -206,7 +217,7 @@ public class LoggingHandlerTest {
ByteBuf msg = Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
channel.writeInbound(msg);
verify(appender).doAppend(argThat(new RegexLogMatcher(".+RECEIVED: " + msg.readableBytes() + "B$")));
verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ: " + msg.readableBytes() + "B$")));
ByteBuf handledMsg = channel.readInbound();
assertThat(msg, is(sameInstance(handledMsg)));
@ -219,7 +230,7 @@ public class LoggingHandlerTest {
ByteBuf msg = Unpooled.EMPTY_BUFFER;
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
channel.writeInbound(msg);
verify(appender).doAppend(argThat(new RegexLogMatcher(".+RECEIVED: 0B$")));
verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ: 0B$")));
ByteBuf handledMsg = channel.readInbound();
assertThat(msg, is(sameInstance(handledMsg)));
@ -237,7 +248,7 @@ public class LoggingHandlerTest {
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
channel.writeInbound(msg);
verify(appender).doAppend(argThat(new RegexLogMatcher(".+RECEIVED: foobar, 5B$")));
verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ: foobar, 5B$")));
ByteBufHolder handledMsg = channel.readInbound();
assertThat(msg, is(sameInstance(handledMsg)));
@ -245,6 +256,14 @@ public class LoggingHandlerTest {
assertThat(channel.readInbound(), is(nullValue()));
}
@Test
public void shouldLogChannelReadComplete() throws Exception {
ByteBuf msg = Unpooled.EMPTY_BUFFER;
EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
channel.writeInbound(msg);
verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ COMPLETE$")));
}
/**
* A custom EasyMock matcher that matches on Logback messages.
*/