Debian Codegraph @

logback-site/src/site/pages/recipes/captureHttp.html

raw
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
7 <title>Capturing HTTP communications</title>
8 <link rel="stylesheet" type="text/css" href="../css/common.css" />
9 <link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" />
10 <link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" />
11 <link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" />
12
13 </head>
14 <body onload="prettyPrint()">
15 <script type="text/javascript">prefix='../';</script>
16 <script type="text/javascript" src="../js/prettify.js"></script>
17 <script src="../templates/header.js" type="text/javascript"></script>
18 <div id="left">
19 <noscript>Please turn on Javascript to view this menu</noscript>
20 <script src="../templates/left.js" type="text/javascript"></script>
21 </div>
22 <div id="content">
23
24 <h2>Capturing incoming HTTP requests and outgoing responses</h2>
25
26 <p>Logback ships with a module called logback-access which
27 integrates with Servlet containers such as Jetty or Tomcat to
28 provide rich and powerful HTTP-access log functionality.
29 </p>
30
31 <p>When diagnosing issues in a web-application, it is very
32 helpful to be able to capture incoming HTTP requests and the
33 outgoing response. This is particularly true for a
34 web-applications offering web-services using <a
35 href="http://en.wikipedia.org/wiki/SOAP">SOAP</a>. Having access
36 to the "raw" SOAP message makes it much easier to diagnose
37 errors caused by misspelled namespaces or missing fields.
38 </p>
39
40 <p>Certain Web-Service stacks can be easily configured to log
41 SOAP traffic. For instance, with JBoss' Web-services stack, you
42 can set the level of the
43 <code>org.jboss.ws.core.MessageTrace</code> logger to
44 <code>TRACE</code> in order to enable SOAP message tracing as <a
45 href="http://community.jboss.org/wiki/JBossWS-Log4j">documented
46 in the Jboss wiki</a>.
47 </p>
48
49 <p>If you are using a different Web-Services stack, e.g. Metro,
50 it might not be very convenient to enable SOAP message
51 tracing. In that case, and assuming your application is deployed
52 on Tomcat, you might want to enable <a
53 href="http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Request_Dumper_Filter">RequestDumperFilter</a>.
54 Unfortunately, not only is <code>RequestDumperFilter</code>
55 rather hard to install, it does not dump the payload of the
56 incoming request nor the outgoing response.
57 </p>
58
59 <p>With the help of <a href="../access.html#teeFilter">
60 logback-access its TeeFilter</a> you can capture the full input
61 and output for each request as explained below.
62
63 </p>
64
65 <h3><a name="capturing" href="#capturing">Capturing</a></h3>
66
67 <p>The <code>TeeFilter</code>, as any other servlet filter,
68 needs to be declared in your web-application's <em>web.xml</em>
69 file. Here is the declaration to add to your web-application's
70 <em>web.xml</em> file.
71 </p>
72
73 <pre class="prettyprint source">&lt;filter&gt;
74 &lt;filter-name&gt;TeeFilter&lt;/filter-name&gt;
75 &lt;filter-class&gt;ch.qos.logback.access.servlet.TeeFilter&lt;/filter-class&gt;
76 &lt;/filter&gt;
77
78 &lt;filter-mapping&gt;
79 &lt;filter-name&gt;TeeFilter&lt;/filter-name&gt;
80 &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
81 &lt;/filter-mapping&gt;</pre>
82
83 <p>The <code>TeeFilter</code> requires the logback-access module
84 to be installed in your web-server. The installation of
85 logback-access is explained <a
86 href="../access.html">elsewhere</a>. Once you have installed
87 logback-access into your Servlet container, e.g. Tomcat or
88 Jetty, you can configure logback-access according to your wishes
89 with the help of a configuration file named
90 <em>logback-access.xml</em>.
91 </p>
92
93 <p>Here is a sample <em>logback-access.xml</em> configuration
94 file which will output the full contents of the request and
95 response on the console.
96 </p>
97
98 <pre class="prettyprint source">&lt;configuration&gt;
99 &lt;appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"&gt;
100 &lt;encoder&gt;
101 &lt;pattern&gt;<b>%fullRequest</b>%n%n<b>%fullResponse</b>&lt;/pattern&gt;
102 &lt;/encoder&gt;
103 &lt;/appender&gt;
104
105 &lt;appender-ref ref="STDOUT" /&gt;
106 &lt;/configuration&gt;</pre>
107
108 <p>For the list of conversion words supported by logback-access'
109 <code>PatternLayout</code> please refer to <a
110 href="manual/layouts.html#AccessPatternLayout">its documentation</a>.
111 </p>
112
113 <p>Here is the output generated when accessing the <a
114 href="../demo.html">logback-demo</a> application configured as shown
115 above, yields:</p>
116
117 <p class="source"><b>GET /logback-demo/index.jsp HTTP/1.1</b>
118 Host: localhost:8080
119 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20070312 Firefox/1.5.0
120 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8
121 Accept-Language: en-us,en;q=0.5
122 Accept-Encoding: gzip,deflate
123 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
124 Keep-Alive: 300
125 Connection: keep-alive
126 Referer: http://localhost:8080/logback-demo/login.jsp
127 Cookie: JSESSIONID=15c7tqi9ehlwk; OID324nkzcmr=null; OID32862zgoa=null;
128
129
130
131 <b>HTTP/1.1 200 OK</b>
132 Content-Type: text/html; charset=iso-8859-1
133 Expires: Thu, 01 Jan 1970 00:00:00 GMT
134 Set-Cookie: JSESSIONID=bgebt99ce9om;path=/logback-demo
135
136
137 &lt;html&gt;
138 &lt;head&gt;
139 &lt;LINK REL=StyleSheet HREF="css/pk.css" /&gt;
140 &lt;/head&gt;
141 &lt;body&gt;
142
143 &lt;h2&gt;Logback demo center&lt;/h2&gt;
144
145 [snip, so that text is reasonably sized]</p>
146
147 <p>&nbsp;</p>
148
149 <h3><a name="disabling" href="#disabling">Disabling
150 <code>TeeFilter</code> in the production environment</a></h3>
151
152 <p>Due to its intrusive nature, <code>TeeFilter</code> can slow
153 down performance. Moreover, although we have fixed all currently
154 known bugs, <code>TeeFilter</code> has broken otherwise
155 correctly behaving applications in the past. Thus, while
156 extremely useful during problem hunting, we do not recommend
157 having <code>TeeFilter</code> active in production systems. In
158 order to avoid shipping different code for test and production
159 environments, <code>TeeFilter</code> supports includes and
160 excludes parameters. <code>TeeFilter</code> will be active if
161 the current host is listed in the includes list and absent in
162 the excludes list. By special convention, an empty
163 <em>includes</em> list is interpreted as to contain all possible
164 host names in the universe.
165 </p>
166
167 <p>Assume we wish to capture HTTP traffic on all hosts except on
168 orion and gemini, the hostnames of the the production systems,
169 we would write:</p>
170
171 <pre class="prettyprint source">&lt;filter>
172 &lt;filter-name>TeeFilter&lt;/filter-name>
173 &lt;filter-class>ch.qos.logback.access.servlet.TeeFilter&lt;/filter-class>
174 &lt;init-param>
175 <b>&lt;!-- exclude captures on production systems --></b>
176 &lt;param-name><b>excludes</b>&lt;/param-name>
177 &lt;param-value>orion, gemini&lt;/param-value>
178 &lt;/init-param>
179 &lt;/filter> </pre>
180
181 <p>If it is easier to explicitly name the integration machines,
182 you could list them in the includes list and omit the excludes
183 list.
184 </p>
185
186 <h3><a name="filtering" href="#filtering">Filtering captured
187 requests</a></h3>
188
189 <p>Let assume that our web-application is deployed in a
190 cluster. The cluster is located behind a load-balancer which
191 probes each member of the cluster once or twice a second to
192 check whether it is alive. Whenever a member becomes unavailable
193 the load-balancer will immediately (well, after at most one
194 second) divert traffic from that member.
195 </p>
196
197 <p>While such a load-balancing strategy will ensure
198 high-availability of your web-application, it will also
199 seriously pollute the access-logs with the contents of each
200 probe made by the load-balancer.
201 </p>
202
203 <p>We need to a way to filter-out these probes so that they no
204 longer contaminate the log output. In other words, we need to
205 distinguish probes emanating from the load-balancer from other
206 requests. The contents of the probes can give us clues about
207 possible distinguishing criteria.</p>
208
209 <p>Here is a sample probe as logged by logback-access:</p>
210
211 <pre class="source"><b>HEAD</b> /myapp/<b>probe</b> HTTP/1.1
212 connection: Close
213 host: 192.168.1.1
214
215 HTTP/1.1 200 OK
216 Expires: Thu, 01 Jan 1970 01:00:00 CET
217 X-Powered-By: Servlet 2.4;
218 Cache-Control: no-cache
219 Pragma: No-cache </pre>
220
221 <p>From the above, we can see that the load-balancer probes
222 employ the <a
223 href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4">HTTP
224 HEAD method</a> instead of the usual GET or POST. We can also
225 see that the request URI for the probes contains the string
226 "probe".</p>
227
228 <p>Here is a <em>logback-access.xml</em> configuration file
229 which will deny any <code>AccessEvent</code> where the method is
230 HEAD. Note that events are evaluated using
231 <code>JaninoEventEvaluator</code> which requires Janino.</p>
232
233 <pre class="prettyprint source">&lt;configuration>
234 &lt;!-- always a good idea to install OnConsoleStatusListener -->
235 &lt;statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
236
237 &lt;appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
238 &lt;filter class="ch.qos.logback.core.filter.EvaluatorFilter">
239 &lt;!-- in the absence of a class attribute the &lt;evaluator> element
240 defaults to ch.qos.logback.access.boolex.JaninoEventEvaluator --&gt;
241 &lt;evaluator>
242 &lt;expression><b>event.getMethod().equals("HEAD")</b>&lt;/expression>
243 &lt;/evaluator>
244 &lt;onMismatch>NEUTRAL&lt;/onMismatch>
245 &lt;onMatch>DENY&lt;/onMatch>
246 &lt;/filter>
247
248 &lt;encoder>
249 &lt;pattern>%fullRequest%n%n%fullResponse&lt;/pattern>
250 &lt;/encoder>
251 &lt;/appender>
252
253 &lt;appender-ref ref="STDOUT" />
254 &lt;/configuration> </pre>
255
256 <script src="../templates/footer.js" type="text/javascript"></script>
257 </div>
258 </body>
259 </html>
260