View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.testing.nio;
29  
30  import java.io.IOException;
31  import java.util.List;
32  
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HttpConnection;
35  import org.apache.hc.core5.http2.frame.FramePrinter;
36  import org.apache.hc.core5.http2.frame.RawFrame;
37  import org.apache.hc.core5.http2.impl.nio.H2StreamListener;
38  import org.apache.hc.core5.testing.classic.LoggingSupport;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  public class LoggingH2StreamListener implements H2StreamListener {
43  
44      public final static LoggingH2StreamListenerStreamListener.html#LoggingH2StreamListener">LoggingH2StreamListener INSTANCE = new LoggingH2StreamListener();
45  
46      private final Logger headerLog;
47      private final Logger frameLog;
48      private final Logger framePayloadLog;
49      private final Logger flowCtrlLog;
50      private final FramePrinter framePrinter;
51  
52      private LoggingH2StreamListener() {
53          this.framePrinter = new FramePrinter();
54          this.headerLog = LoggerFactory.getLogger("org.apache.hc.core5.http.headers");
55          this.frameLog = LoggerFactory.getLogger("org.apache.hc.core5.http2.frame");
56          this.framePayloadLog = LoggerFactory.getLogger("org.apache.hc.core5.http2.frame.payload");
57          this.flowCtrlLog = LoggerFactory.getLogger("org.apache.hc.core5.http2.flow");
58      }
59  
60      private void logFrameInfo(final String prefix, final RawFrame frame) {
61          try {
62              final LogAppendableppendable.html#LogAppendable">LogAppendable logAppendable = new LogAppendable(frameLog, prefix);
63              framePrinter.printFrameInfo(frame, logAppendable);
64              logAppendable.flush();
65          } catch (final IOException ignore) {
66              // ignore
67          }
68      }
69  
70      private void logFramePayload(final String prefix, final RawFrame frame) {
71          try {
72              final LogAppendableppendable.html#LogAppendable">LogAppendable logAppendable = new LogAppendable(framePayloadLog, prefix);
73              framePrinter.printPayload(frame, logAppendable);
74              logAppendable.flush();
75          } catch (final IOException ignore) {
76              // ignore
77          }
78      }
79  
80      private void logFlowControl(final String prefix, final int streamId, final int delta, final int actualSize) {
81          flowCtrlLog.debug("{} stream {} flow control {} -> {}", prefix, streamId, delta, actualSize);
82      }
83  
84      @Override
85      public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
86          if (headerLog.isDebugEnabled()) {
87              final String prefix = LoggingSupport.getId(connection);
88              for (int i = 0; i < headers.size(); i++) {
89                  headerLog.debug("{} << {}", prefix, headers.get(i));
90              }
91          }
92      }
93  
94      @Override
95      public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
96          if (headerLog.isDebugEnabled()) {
97              final String prefix = LoggingSupport.getId(connection);
98              for (int i = 0; i < headers.size(); i++) {
99                  headerLog.debug("{} >> {}", prefix, headers.get(i));
100             }
101         }
102     }
103 
104     @Override
105     public void onFrameInput(final HttpConnection connection, final int streamId, final RawFrame frame) {
106         if (frameLog.isDebugEnabled()) {
107             logFrameInfo(LoggingSupport.getId(connection) + " <<", frame);
108         }
109         if (framePayloadLog.isDebugEnabled()) {
110             logFramePayload(LoggingSupport.getId(connection) + " <<", frame);
111         }
112     }
113 
114     @Override
115     public void onFrameOutput(final HttpConnection connection, final int streamId, final RawFrame frame) {
116         if (frameLog.isDebugEnabled()) {
117             logFrameInfo(LoggingSupport.getId(connection) + " >>", frame);
118         }
119         if (framePayloadLog.isDebugEnabled()) {
120             logFramePayload(LoggingSupport.getId(connection) + " >>", frame);
121         }
122     }
123 
124     @Override
125     public void onInputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
126         if (flowCtrlLog.isDebugEnabled()) {
127             logFlowControl(LoggingSupport.getId(connection) + "  in", streamId, delta, actualSize);
128         }
129     }
130 
131     @Override
132     public void onOutputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
133         if (flowCtrlLog.isDebugEnabled()) {
134             logFlowControl(LoggingSupport.getId(connection) + " out", streamId, delta, actualSize);
135         }
136     }
137 
138 }