codec-http2: Let users configure per-frame logs

Motivation:

codec-http2 is really loud!

Modification:

Allow users to select how to log in the Http2Codec.

Result:

We can run Http2Codec and log however we like.
This commit is contained in:
Moses Nakamura 2016-09-21 16:44:43 -07:00 committed by Scott Mitchell
parent 506ac2ca71
commit 0d2baaf6ae
3 changed files with 25 additions and 10 deletions

View File

@ -21,12 +21,15 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.util.internal.UnstableApi; import io.netty.util.internal.UnstableApi;
import static io.netty.handler.logging.LogLevel.INFO;
/** /**
* An HTTP/2 channel handler that adds a {@link Http2FrameCodec} and {@link Http2MultiplexCodec} to the pipeline before * An HTTP/2 channel handler that adds a {@link Http2FrameCodec} and {@link Http2MultiplexCodec} to the pipeline before
* removing itself. * removing itself.
*/ */
@UnstableApi @UnstableApi
public final class Http2Codec extends ChannelDuplexHandler { public final class Http2Codec extends ChannelDuplexHandler {
private static final Http2FrameLogger HTTP2_FRAME_LOGGER = new Http2FrameLogger(INFO, Http2Codec.class);
private final Http2FrameCodec frameCodec; private final Http2FrameCodec frameCodec;
private final Http2MultiplexCodec multiplexCodec; private final Http2MultiplexCodec multiplexCodec;
@ -39,7 +42,7 @@ public final class Http2Codec extends ChannelDuplexHandler {
* {@link ChannelHandler.Sharable}. * {@link ChannelHandler.Sharable}.
*/ */
public Http2Codec(boolean server, ChannelHandler streamHandler) { public Http2Codec(boolean server, ChannelHandler streamHandler) {
this(server, streamHandler, null); this(server, streamHandler, null, HTTP2_FRAME_LOGGER);
} }
/** /**
@ -51,14 +54,15 @@ public final class Http2Codec extends ChannelDuplexHandler {
* @param streamGroup event loop for registering child channels * @param streamGroup event loop for registering child channels
*/ */
public Http2Codec(boolean server, ChannelHandler streamHandler, public Http2Codec(boolean server, ChannelHandler streamHandler,
EventLoopGroup streamGroup) { EventLoopGroup streamGroup, Http2FrameLogger frameLogger) {
this(server, streamHandler, streamGroup, new DefaultHttp2FrameWriter()); this(server, streamHandler, streamGroup, new DefaultHttp2FrameWriter(), frameLogger);
} }
// Visible for testing // Visible for testing
Http2Codec(boolean server, ChannelHandler streamHandler, Http2Codec(boolean server, ChannelHandler streamHandler,
EventLoopGroup streamGroup, Http2FrameWriter frameWriter) { EventLoopGroup streamGroup, Http2FrameWriter frameWriter,
frameCodec = new Http2FrameCodec(server, frameWriter); Http2FrameLogger frameLogger) {
frameCodec = new Http2FrameCodec(server, frameWriter, frameLogger);
multiplexCodec = new Http2MultiplexCodec(server, streamGroup, streamHandler); multiplexCodec = new Http2MultiplexCodec(server, streamGroup, streamHandler);
} }

View File

@ -57,15 +57,25 @@ public class Http2FrameCodec extends ChannelDuplexHandler {
* @param server {@code true} this is a server * @param server {@code true} this is a server
*/ */
public Http2FrameCodec(boolean server) { public Http2FrameCodec(boolean server) {
this(server, new DefaultHttp2FrameWriter()); this(server, HTTP2_FRAME_LOGGER);
}
/**
* Construct a new handler.
*
* @param server {@code true} this is a server
*/
public Http2FrameCodec(boolean server, Http2FrameLogger frameLogger) {
this(server, new DefaultHttp2FrameWriter(), frameLogger);
} }
// Visible for testing // Visible for testing
Http2FrameCodec(boolean server, Http2FrameWriter frameWriter) { Http2FrameCodec(boolean server, Http2FrameWriter frameWriter, Http2FrameLogger frameLogger) {
Http2Connection connection = new DefaultHttp2Connection(server); Http2Connection connection = new DefaultHttp2Connection(server);
frameWriter = new Http2OutboundFrameLogger(frameWriter, HTTP2_FRAME_LOGGER); frameWriter = new Http2OutboundFrameLogger(frameWriter, frameLogger);
Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter); Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
Http2FrameReader reader = new Http2InboundFrameLogger(new DefaultHttp2FrameReader(), HTTP2_FRAME_LOGGER); Http2FrameReader frameReader = new DefaultHttp2FrameReader();
Http2FrameReader reader = new Http2InboundFrameLogger(frameReader, frameLogger);
Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, reader); Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, reader);
decoder.frameListener(new FrameListener()); decoder.frameListener(new FrameListener());
http2Handler = new InternalHttp2ConnectionHandler(decoder, encoder, new Http2Settings()); http2Handler = new InternalHttp2ConnectionHandler(decoder, encoder, new Http2Settings());

View File

@ -30,6 +30,7 @@ import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpScheme; import io.netty.handler.codec.http.HttpScheme;
import io.netty.handler.codec.http2.Http2Exception.StreamException; import io.netty.handler.codec.http2.Http2Exception.StreamException;
import io.netty.handler.codec.http2.Http2Stream.State; import io.netty.handler.codec.http2.Http2Stream.State;
import io.netty.handler.logging.LogLevel;
import io.netty.util.AbstractReferenceCounted; import io.netty.util.AbstractReferenceCounted;
import io.netty.util.AsciiString; import io.netty.util.AsciiString;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
@ -75,7 +76,7 @@ public class Http2FrameCodecTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
frameWriter = spy(new VerifiableHttp2FrameWriter()); frameWriter = spy(new VerifiableHttp2FrameWriter());
framingCodec = new Http2FrameCodec(true, frameWriter); framingCodec = new Http2FrameCodec(true, frameWriter, new Http2FrameLogger(LogLevel.TRACE));
frameListener = ((DefaultHttp2ConnectionDecoder) framingCodec.connectionHandler().decoder()) frameListener = ((DefaultHttp2ConnectionDecoder) framingCodec.connectionHandler().decoder())
.internalFrameListener(); .internalFrameListener();
inboundHandler = new LastInboundHandler(); inboundHandler = new LastInboundHandler();