View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.channel.ChannelHandlerAdapter;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.handler.logging.LogLevel;
23  import io.netty.util.internal.UnstableApi;
24  import io.netty.util.internal.logging.InternalLogLevel;
25  import io.netty.util.internal.logging.InternalLogger;
26  import io.netty.util.internal.logging.InternalLoggerFactory;
27  
28  import static io.netty.util.internal.ObjectUtil.checkNotNull;
29  
30  /**
31   * Logs HTTP2 frames for debugging purposes.
32   */
33  @UnstableApi
34  public class Http2FrameLogger extends ChannelHandlerAdapter {
35  
36      public enum Direction {
37          INBOUND,
38          OUTBOUND
39      }
40  
41      private static final int BUFFER_LENGTH_THRESHOLD = 64;
42      private final InternalLogger logger;
43      private final InternalLogLevel level;
44  
45      public Http2FrameLogger(LogLevel level) {
46          this(level.toInternalLevel(), InternalLoggerFactory.getInstance(Http2FrameLogger.class));
47      }
48  
49      public Http2FrameLogger(LogLevel level, String name) {
50          this(level.toInternalLevel(), InternalLoggerFactory.getInstance(name));
51      }
52  
53      public Http2FrameLogger(LogLevel level, Class<?> clazz) {
54          this(level.toInternalLevel(), InternalLoggerFactory.getInstance(clazz));
55      }
56  
57      private Http2FrameLogger(InternalLogLevel level, InternalLogger logger) {
58          this.level = checkNotNull(level, "level");
59          this.logger = checkNotNull(logger, "logger");
60      }
61  
62      public boolean isEnabled() {
63          return logger.isEnabled(level);
64      }
65  
66      public void logData(Direction direction, ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
67              boolean endStream) {
68          if (isEnabled()) {
69              logger.log(level, "{} {} DATA: streamId={} padding={} endStream={} length={} bytes={}", ctx.channel(),
70                      direction.name(), streamId, padding, endStream, data.readableBytes(), toString(data));
71          }
72      }
73  
74      public void logHeaders(Direction direction, ChannelHandlerContext ctx, int streamId, Http2Headers headers,
75              int padding, boolean endStream) {
76          if (isEnabled()) {
77              logger.log(level, "{} {} HEADERS: streamId={} headers={} padding={} endStream={}", ctx.channel(),
78                      direction.name(), streamId, headers, padding, endStream);
79          }
80      }
81  
82      public void logHeaders(Direction direction, ChannelHandlerContext ctx, int streamId, Http2Headers headers,
83              int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) {
84          if (isEnabled()) {
85              logger.log(level, "{} {} HEADERS: streamId={} headers={} streamDependency={} weight={} exclusive={} " +
86                      "padding={} endStream={}", ctx.channel(),
87                      direction.name(), streamId, headers, streamDependency, weight, exclusive, padding, endStream);
88          }
89      }
90  
91      public void logPriority(Direction direction, ChannelHandlerContext ctx, int streamId, int streamDependency,
92              short weight, boolean exclusive) {
93          if (isEnabled()) {
94              logger.log(level, "{} {} PRIORITY: streamId={} streamDependency={} weight={} exclusive={}", ctx.channel(),
95                      direction.name(), streamId, streamDependency, weight, exclusive);
96          }
97      }
98  
99      public void logRstStream(Direction direction, ChannelHandlerContext ctx, int streamId, long errorCode) {
100         if (isEnabled()) {
101             logger.log(level, "{} {} RST_STREAM: streamId={} errorCode={}", ctx.channel(),
102                     direction.name(), streamId, errorCode);
103         }
104     }
105 
106     public void logSettingsAck(Direction direction, ChannelHandlerContext ctx) {
107         logger.log(level, "{} {} SETTINGS: ack=true", ctx.channel(), direction.name());
108     }
109 
110     public void logSettings(Direction direction, ChannelHandlerContext ctx, Http2Settings settings) {
111         if (isEnabled()) {
112             logger.log(level, "{} {} SETTINGS: ack=false settings={}", ctx.channel(), direction.name(), settings);
113         }
114     }
115 
116     public void logPing(Direction direction, ChannelHandlerContext ctx, long data) {
117         if (isEnabled()) {
118             logger.log(level, "{} {} PING: ack=false bytes={}", ctx.channel(),
119                     direction.name(), data);
120         }
121     }
122 
123     public void logPingAck(Direction direction, ChannelHandlerContext ctx, long data) {
124         if (isEnabled()) {
125             logger.log(level, "{} {} PING: ack=true bytes={}", ctx.channel(),
126                     direction.name(), data);
127         }
128     }
129 
130     public void logPushPromise(Direction direction, ChannelHandlerContext ctx, int streamId, int promisedStreamId,
131             Http2Headers headers, int padding) {
132         if (isEnabled()) {
133             logger.log(level, "{} {} PUSH_PROMISE: streamId={} promisedStreamId={} headers={} padding={}",
134                     ctx.channel(), direction.name(), streamId, promisedStreamId, headers, padding);
135         }
136     }
137 
138     public void logGoAway(Direction direction, ChannelHandlerContext ctx, int lastStreamId, long errorCode,
139             ByteBuf debugData) {
140         if (isEnabled()) {
141             logger.log(level, "{} {} GO_AWAY: lastStreamId={} errorCode={} length={} bytes={}", ctx.channel(),
142                     direction.name(), lastStreamId, errorCode, debugData.readableBytes(), toString(debugData));
143         }
144     }
145 
146     public void logWindowsUpdate(Direction direction, ChannelHandlerContext ctx, int streamId,
147             int windowSizeIncrement) {
148         if (isEnabled()) {
149             logger.log(level, "{} {} WINDOW_UPDATE: streamId={} windowSizeIncrement={}", ctx.channel(),
150                     direction.name(), streamId, windowSizeIncrement);
151         }
152     }
153 
154     public void logUnknownFrame(Direction direction, ChannelHandlerContext ctx, byte frameType, int streamId,
155             Http2Flags flags, ByteBuf data) {
156         if (isEnabled()) {
157             logger.log(level, "{} {} UNKNOWN: frameType={} streamId={} flags={} length={} bytes={}", ctx.channel(),
158                     direction.name(), frameType & 0xFF, streamId, flags.value(), data.readableBytes(), toString(data));
159         }
160     }
161 
162     private String toString(ByteBuf buf) {
163         if (level == InternalLogLevel.TRACE || buf.readableBytes() <= BUFFER_LENGTH_THRESHOLD) {
164             // Log the entire buffer.
165             return ByteBufUtil.hexDump(buf);
166         }
167 
168         // Otherwise just log the first 64 bytes.
169         int length = Math.min(buf.readableBytes(), BUFFER_LENGTH_THRESHOLD);
170         return ByteBufUtil.hexDump(buf, buf.readerIndex(), length) + "...";
171     }
172 }