Debian Codegraph @

logback-site/src/site/pages/manual/receivers.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 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
6 <title>Chapter 14: Receivers</title>
7 <link rel="stylesheet" type="text/css" href="../css/common.css" />
8 <link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" />
9 <link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" />
10 <link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen"/>
11 </head>
12 <body onload="prettyPrint(); decorate();">
13 <script type="text/javascript">prefix='../';</script>
14 <script type="text/javascript" src="../js/prettify.js"></script>
15 <script type="text/javascript" src="../templates/header.js"></script>
16 <script type="text/javascript" src="../js/dsl.js"></script>
17 <script type="text/javascript" src="../js/jquery-min.js"></script>
18 <script type="text/javascript" src="../js/decorator.js"></script>
19 <div id="left">
20 <noscript>Please turn on Javascript to view this menu</noscript>
21 <script src="../templates/left.js" type="text/javascript"></script>
22 </div>
23 <div id="right">
24 <script src="menu.js" type="text/javascript"></script>
25 </div>
26
27 <div id="content">
28
29 <h1>Chapter 14: Receivers</h1>
30
31 <a href="receivers_ja.html">&#x548C;&#x8A33; (Japanese translation)</a>
32
33 <div class="quote">
34
35 <p><em>You cannot swim for new horizons until you have courage
36 to lose sight of the shore.</em></p>
37
38 <p>&mdash;WILLIAM FAULKNER</p>
39 </div>
40
41
42 <script src="../templates/creative.js" type="text/javascript"></script>
43 <script src="../templates/setup.js" type="text/javascript"></script>
44
45 <h2 class="doAnchor" name="whatIsAReceiver">What is a Receiver?</h2>
46
47 <p>A <em>receiver</em> is a Logback component that receives logging
48 events from a remote appender and logs each received event according
49 to local policy. Using a combination of socket-based appenders and
50 receivers, it is possible to construct sophisticated topologies
51 for distribution of application logging events over a network.</p>
52
53 <p>A receiver extends the <a
54 href="../xref/ch/qos/logback/classic/net/ReceiverBase.html">
55 <code>ch.qos.logback.classic.net.ReceiverBase</code></a> class.
56 By virtue of the fact that a receiver extends this class, a
57 receiver participates in the Logback component <a
58 href="../xref/ch/qos/logback/core/spi/LifeCycle.html">LifeCycle</a>
59 and a receiver is <a
60 href="../xref/ch/qos/logback/core/spi/ContextAware.html">
61 ContextAware</a>.</p>
62
63 <p>Historically, support for logging event delivery over a network
64 connection in Logback has been provided by <code>SocketAppender</code>
65 and the corresponding <code>SimpleSocketServer</code>. The appender
66 acts as a client, initiating a network connection to the server
67 application, and delivering logging events via the network connection.
68 The receiver component and corresponding appender support offers much
69 greater flexibility.</p>
70
71 <p>A receiver component is configured in <em>logback.xml</em>, just
72 like any other logback component. This allows the full capabilities
73 of <a href="onJoran.html">Joran</a> to be utilized in configuring
74 a receiver component. Moreover, <em>any</em> application can
75 receive logging events from remote appenders by simply configuring
76 one or more receiver components.</p>
77
78 <p>Connection initiation between an appender and a receiver
79 can occur in either direction. A receiver can act in the role of a
80 server, passively listening for connections from remote appender
81 clients. Alternatively, a receiver can act in the client role,
82 initiating a connection to a remote appender which is acting in the
83 server role. Regardless of the respective roles of the
84 appender and receiver, <em>logging events always flow from
85 the appender towards the receiver</em>.</p>
86
87 <p>The flexibility to allow a receiver to initiate the connection
88 to an appender is particularly useful in certain situations:
89 </p>
90 <ul>
91 <li>For security reasons, a central logging server may be
92 located behind a network firewall that does not allow incoming
93 connections. Using receiver components acting in the client
94 role, the central logging server (inside the firewall)
95 can initiate connections to the applications of interest
96 (outside the firewall).
97 </li>
98 <li>It is often desirable for developer tools (such as IDE plugins)
99 and enterprise management applications to have access to the
100 logging event stream of running applications. Traditionally,
101 Logback has supported this (for example in Logback Beagle) by
102 requiring the recipient application (e.g. a developer tool running
103 in an IDE) to act in the server role, passively listening for
104 connections from a remote appender. This can prove difficult to
105 manage, especially for tools running on a developer's workstation,
106 which may indeed by mobile. However, such tools can now be
107 implemented using a Logback receiver component acting in the
108 client role, initiating a connection to a remote appender in
109 order to receive logging events for local display, filtering,
110 and alerting.
111 </li>
112 </ul>
113
114 <p>A logback configuration can include any number of receiver components
115 acting in any combination of the server or client roles. The only
116 restrictions are that each receiver acting in the server role must
117 listen on a distinct port, and each receiver acting in the client
118 role will connect to exactly one remote appender.</p>
119
120 <h2 class="doAnchor" name="receiverServerComponents">Receivers
121 that Act in the Server Role</h2>
122
123 <p>A receiver that is configured to act in the server role passively
124 listens for incoming connections from remote appenders. This is
125 functionally equivalent to using the standalone
126 <code>SimpleSocketServer</code> application, except that by using
127 the receiver component, <em>any</em> application that uses Logback
128 Classic can receive logging events from remote appenders by simply
129 configuring the receiver in <em>logback.xml</em>.</p>
130
131 <p>
132 <img border="1" src="images/chapters/receivers/serverSocketReceiver.png" "/>
133 </p>
134
135 <p>Logback includes two receiver components that act in the
136 server role; <a
137 href="../xref/ch/qos/logback/classic/net/server/ServerSocketReceiver.html">
138 <code>ServerSocketReceiver</code></a> and its SSL-enabled
139 subtype
140 <a href="../xref/ch/qos/logback/classic/net/server/SSLServerSocketReceiver.html">
141 <code>SSLServerSocketReceiver</code></a>. Both of these receiver
142 components are designed to accept connections from incoming
143 <code>SocketAppender</code> (or <code>SSLSocketAppender</code>)
144 clients.</p>
145
146 <p>The <code>ServerSocketReceiver</code> components provide the
147 following configurable properties:</p>
148
149 <table class="bodyTable striped">
150 <tr>
151 <th>Property Name</th>
152 <th>Type</th>
153 <th>Description</th>
154 </tr>
155 <tr>
156 <td><span class="prop" container="serverSocketReceiver">address</span></td>
157 <td><code>String</code></td>
158 <td>The local network interface address on which the receiver
159 will listen. If this property is not specified, the receiver
160 will listen on all network interfaces.</td>
161 </tr>
162 <tr>
163 <td><span class="prop" container="serverSocketReceiver">port</span></td>
164 <td><code>int</code></td>
165 <td>The TCP port on which the receiver will listen. If this
166 property is not specified, a default value will be used.</td>
167 </tr>
168 <tr>
169 <td><span class="prop" container="serverSocketReceiver">ssl</span></td>
170 <td><code>SSLConfiguration</code></td>
171 <td>Supported only for <code>SSLServerSocketReceiver</code>, this
172 property provides the SSL configuration that will be used by
173 the receiver, as described in <a href="usingSSL.html">Using SSL</a>.
174 </td>
175 </tr>
176 </table>
177
178 <h3 class="doAnchor" name="usingServerSocketReceiver">Using
179 ServerSocketReceiver</h3>
180
181 <p>The following configuration uses the
182 <code>ServerSocketReceiver</code> component with a minimal local
183 appender and logger configuration. Logging events received from
184 a remote appender will be matched by the root logger and delivered
185 to the local console appender.</p>
186
187 <p class="example">Example: Basic ServerSocketReceiver Configuration
188 (logback-examples/src/main/resources/chapters/receivers/socket/receiver1.xml)</p>
189
190 <span class="asGroovy" onclick="return asGroovy('receiver1');">View as .groovy</span>
191 <pre id="receiver1" class="prettyprint source">&lt;configuration debug="true">
192
193 &lt;appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
194 &lt;encoder>
195 &lt;pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n&lt;/pattern>
196 &lt;/encoder>
197 &lt;/appender>
198
199 &lt;root level="DEBUG">
200 &lt;appender-ref ref="CONSOLE" />
201 &lt;/root>
202
203 &lt;receiver class="ch.qos.logback.classic.net.server.ServerSocketReceiver">
204 &lt;port>${port}&lt;/port>
205 &lt;/receiver>
206
207 &lt;/configuration></pre>
208
209 <p>Note that the receiver component's <em>class</em>
210 attribute identifies the receiver subtype that we wish to use. In
211 this example we are using <code>ServerSocketReceiver</code>.</p>
212
213 <p>Our example server application is very similar in function and
214 design to <code>SimpleSocketServer</code>. It simply accepts a
215 path for a logback configuration file as a command line argument,
216 and runs the given configuration. While our example is somewhat
217 trivial, keep in mind that you can configure logback's
218 <code>ServerSocketReceiver</code> (or <code>SSLServerSocketReceiver</code>)
219 component in <em>any</em> application.
220 </p>
221
222 <p>From a shell in the <em>logback-examples</em> directory,
223 we can run our example server application as follows:</p>
224
225 <p class="source">java -Dport=6000 <a href="../xref/chapters/receivers/socket/ReceiverExample.html">chapters.receivers.socket.ReceiverExample</a> \
226 src/main/java/chapters/receivers/socket/receiver1.xml</p>
227
228 <p>We can connect to the running receiver using a client application
229 that is configured with a <code>SocketAppender</code>. Our example
230 client application simply loads a logback configuration that will
231 connect a socket appender to our example receiver. It then awaits
232 input from the user in the form of a message that will be relayed to
233 the receiver. We can run the example client application as follows:
234 </p>
235
236 <p class="source">java -Dhost=localhost -Dport=6000 \
237 <a href="../xref/chapters/receivers/socket/AppenderExample.html">chapters.receivers.socket.AppenderExample </a>\
238 src/main/java/chapters/receivers/socket/appender1.xml</p>
239
240 <h3 class="doAnchor" name="usingSSLServerSocketReceiver">Using
241 SSLServerSocketReceiver</h3>
242
243 <p>The following configuration repeats the same minimal appender
244 and logger configuration, but uses the SSL-enabled receiver component
245 that acts in the server role.</p>
246
247 <p class="example">Example: Basic SSLServerSocketReceiver Configuration
248 (logback-examples/src/main/resources/chapters/receivers/socket/receiver2.xml)</p>
249 <span class="asGroovy" onclick="return asGroovy('receiver2');">View as .groovy</span>
250 <pre id="receiver2" class="prettyprint source">&lt;configuration debug="true">
251
252 &lt;appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
253 &lt;encoder>
254 &lt;pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n&lt;/pattern>
255 &lt;/encoder>
256 &lt;/appender>
257
258 &lt;root level="DEBUG">
259 &lt;appender-ref ref="CONSOLE" />
260 &lt;/root>
261
262 &lt;receiver class="ch.qos.logback.classic.net.server.SSLServerSocketReceiver">
263 &lt;port>${port}&lt;/port>
264 &lt;ssl>
265 &lt;keyStore>
266 &lt;location>${keystore}&lt;/location>
267 &lt;password>${password}&lt;/password>
268 &lt;/keyStore>
269 &lt;/ssl>
270 &lt;/receiver>
271
272 &lt;/configuration></pre>
273
274 <p>The essential differences between this configuration and the
275 previous example using <code>ServerSocketReceiver</code> are the
276 specification of <code>SSLServerSocketReceiver</code> in the
277 <em>class</em> attribute and the presence of the nested
278 <span class="prop">ssl</span> property, which is used here to
279 specify the location and password for the key store containing the
280 receiver's private key and certificate, using substitution variables.
281 See <a href="usingSSL.html">Using SSL</a> for details on
282 configuring SSL properties for Logback components.</p>
283
284 <p>We can run this configuration using the same example server
285 configuration, with just a couple of additional configuration
286 properties:</p>
287
288 <p class="source">java -Dport=6001 \
289 -Dkeystore=file:src/main/java/chapters/appenders/socket/ssl/keystore.jks \
290 -Dpassword=changeit \
291 chapters.receivers.socket.ReceiverExample \
292 src/main/java/chapters/receivers/socket/receiver2.xml</p>
293
294 <p>Note that the <em>keystore</em> property given on the command
295 line specifies a file URL that identifies the location of the key
296 store. You may also use a classpath URL as described in
297 <a href="usingSSL.html">Using SSL</a>.
298 </p>
299
300 <p>We can connect to the running receiver using a client application
301 that is configured with a <code>SSLSocketAppender</code>. We use
302 the sample example client application used in the previous example,
303 with a configuration file that uses an SSL-enabled appender. We
304 run the example as follows:
305 </p>
306
307 <p class="source">java -Dhost=localhost -Dport=6001 \
308 -Dtruststore=file:src/main/java/chapters/appenders/socket/ssl/truststore.jks \
309 -Dpassword=changeit \
310 chapters.receivers.socket.AppenderExample \
311 src/main/java/chapters/receivers/socket/appender2.xml</p>
312
313 <p>Note that our example is using a self-signed X.509 credential that
314 is suitable for testing and experimentation, only. <strong>In a
315 production setting, you should obtain an appropriate X.509 credential
316 to identify your SSL-enabled logback components</strong>. See
317 <a href="usingSSL.html">Using SSL</a> for more information.</p>
318
319 <h2 class="doAnchor" name="receiverClientComponents">Receivers
320 that Act in the Client Role</h2>
321
322 <p>A receiver that is configured to act in the client role initiates
323 a connection to a remote appender. The remote appender must be a
324 server type, such as <code>ServerSocketAppender</code>.</p>
325
326 <p>
327 <img border="1" src="images/chapters/receivers/socketReceiver.png"/>
328 </p>
329
330 <p>Logback includes two receiver components that act in the client
331 role; <a href="../xref/ch/qos/logback/classic/net/SocketReceiver.html">
332 <code>SocketReceiver</code></a> and its SSL-enabled subtype
333 <a href="../xref/ch/qos/logback/classic/net/SSLSocketReceiver.html">
334 <code>SSLSocketReceiver</code></a>. Both of these receiver
335 components are designed to initiate a connection to a remote appender
336 that is a <code>ServerSocketAppender</code>
337 (or <code>SSLServerSocketAppender</code>).</p>
338
339 <p>The following configuration properties are supported by
340 <code>SocketReceiver</code> subtypes:</p>
341
342 <table class="bodyTable striped">
343 <tr>
344 <th>Property Name</th>
345 <th>Type</th>
346 <th>Description</th>
347 </tr>
348 <tr>
349 <td><span class="prop" container="SocketReceiver">remoteHost</span></td>
350 <td><code>String</code></td>
351 <td>The hostname or address of the remote server socket appender.</td>
352 </tr>
353 <tr>
354 <td><span class="prop" container="SocketReceiver">port</span></td>
355 <td><code>int</code></td>
356 <td>The port number of the remote server socket appender.</td>
357 </tr>
358 <tr>
359 <td><span class="prop" container="socket">reconnectionDelay</span></td>
360 <td><code>int</code></td>
361 <td>
362 A positive integer representing the number of milliseconds to wait
363 before attempting to reconnect after a connection failure. The
364 default value is 30000 (30 seconds).
365 </td>
366 </tr>
367 <tr>
368 <td><span class="prop" container="SocketReceiver">ssl</span></td>
369 <td><code>SSLConfiguration</code></td>
370 <td>Supported only for <code>SSLSocketReceiver</code>, this
371 property provides the SSL configuration that will be used for
372 this receiver, as described in <a href="usingSSL.html">Using SSL</a>.
373 </td>
374 </tr>
375 </table>
376
377 <h3 class="doAnchor" name="usingSocketReceiver">Using
378 SocketReceiver</h3>
379
380 <p>The configuration used for <code>SocketReceiver</code>
381 is quite similar to the previous example that used
382 <code>ServerSocketReceiver</code>. The differences relate to the
383 fact that the roles of client and server are reversed; a receiver
384 of type <code>SocketReceiver</code> is a client, and the remote
385 appender acts as a server.</p>
386
387 <p class="example">Example: Basic SocketReceiver Configuration
388 (logback-examples/src/main/resources/chapters/receivers/socket/receiver3.xml)</p>
389 <span class="asGroovy" onclick="return asGroovy('receiver3');">View as .groovy</span>
390 <pre id="receiver3" class="prettyprint source">&lt;configuration debug="true">
391
392 &lt;appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
393 &lt;encoder>
394 &lt;pattern>%date %-5level [%thread] %logger - %message%n&lt;/pattern>
395 &lt;/encoder>
396 &lt;/appender>
397
398 &lt;root level="DEBUG">
399 &lt;appender-ref ref="CONSOLE" />
400 &lt;/root>
401
402 &lt;receiver class="ch.qos.logback.classic.net.SocketReceiver">
403 &lt;remoteHost>${host}&lt;/remoteHost>
404 &lt;port>${port}&lt;/port>
405 &lt;reconnectionDelay>10000&lt;/reconnectionDelay>
406 &lt;/receiver>
407
408 &lt;/configuration></pre>
409
410 <p>This configuration will cause logback to connect to a
411 <code>ServerSocketAppender</code> running on the host and port specified
412 by the <em>host</em> and <em>port</em> substitution variables. Logging
413 events received from the remote appender will be logged locally
414 (according to the configuration shown here) via a console appender.
415 </p>
416
417 <p>Assuming you are in the <em>logback-examples/</em> directory,
418 you can run this example configuration using the following command:</p>
419
420
421
422 <p>The example loads the configuration and then simply waits for logging
423 events from the remote appender. If you run this example when the remote
424 appender is not running, you'll see <em>connection refused</em> messages
425 appearing in the log output, periodically. The receiver will
426 periodically attempt to reconnect to the remote appender until it
427 succeeds or until the logger context is shut down. The delay
428 interval between attempts is configurable using the
429 <span class="prop">reconnectionDelay</span> property as shown in the
430 example configuration.
431 </p>
432
433 <p class="source">java -Dhost=localhost -Dport=6000 \
434 chapters.receivers.socket.ReceiverExample \
435 src/main/java/chapters/receivers/socket/receiver3.xml</p>
436
437
438 <p>We can provide a remote appender to which our example receiver
439 can connect, using the same appender example used previously. The
440 example loads a logback configuration containing a
441 <code>ServerSocketAppender</code>, and then waits input from the
442 user consisting of a message that will be delivered to connected
443 receivers. We can run the example appender application as follows:
444 </p>
445
446 <p class="source">java -Dport=6000 \
447 chapters.receivers.socket.AppenderExample \
448 src/main/java/chapters/receivers/socket/appender3.xml</p>
449
450 <p>If you enter a message to send when the receiver is not connected,
451 note that the message is simply discarded.</p>
452
453 <h3 class="doAnchor" name="usingSSLSocketReceiver">Using
454 SocketSSLReceiver</h3>
455
456
457 <p>The configuration needed for <code>SSLSocketReceiver</code> is very
458 similar to that used with <code>SocketReceiver</code>. The essential
459 differences are in the class specified for the receiver and the ability
460 to nest the <span class="prop">ssl</span> property to specify SSL
461 configuration properties. The following example illustrates a basic
462 configuration:
463 </p>
464
465 <p class="example">Example: Basic SSLSocketReceiver Configuration
466 (logback-examples/src/main/resources/chapters/receivers/socket/receiver4.xml)</p>
467 <span class="asGroovy" onclick="return asGroovy('receiver4');">View as .groovy</span>
468 <pre id="receiver4" class="prettyprint source">&lt;configuration debug="true">
469
470 &lt;appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
471 &lt;encoder>
472 &lt;pattern>%date %-5level [%thread] %logger - %message%n&lt;/pattern>
473 &lt;/encoder>
474 &lt;/appender>
475
476 &lt;root level="DEBUG">
477 &lt;appender-ref ref="CONSOLE" />
478 &lt;/root>
479
480 &lt;receiver class="ch.qos.logback.classic.net.SSLSocketReceiver">
481 &lt;remoteHost>${host}&lt;/remoteHost>
482 &lt;port>${port}&lt;/port>
483 &lt;reconnectionDelay>10000&lt;/reconnectionDelay>
484 &lt;ssl>
485 &lt;trustStore>
486 &lt;location>${truststore}&lt;/location>
487 &lt;password>${password}&lt;/password>
488 &lt;/trustStore>
489 &lt;/ssl>
490 &lt;/receiver>
491
492 &lt;/configuration></pre>
493
494 <p>Note that the <em>class</em> attribute now specifies
495 <code>SSLSocketReceiver</code> and that in addition to the configuration
496 properties shown in the previous example, this configuration contains
497 an SSL configuration specifying the location and password for a
498 trust store that will be used in validating that the remote appender is
499 trusted. See <a href="usingSSL.html">Using SSL</a> for more information
500 on configuring SSL properties.
501 </p>
502
503 <p>You can run this example configuration using the following command:</p>
504
505 <p class="source">java -Dhost=localhost -Dport=6001 \
506 -Dtruststore=file:src/main/java/chapters/appenders/socket/ssl/truststore.jks \
507 -Dpassword=changeit \
508 chapters.receivers.socket.ReceiverExample \
509 src/main/java/chapters/receivers/socket/receiver4.xml</p>
510
511 <p>Once started, the receiver attempts to connect to the specified
512 remote appender. Assuming that the appender is not yet running, you
513 will see a "connection refused" message appearing in the log output
514 periodically; the receiver will periodically retry the connection to
515 the remote appender after delaying for the period of time specified by
516 the <span class="prop">reconnectionDelay</span> property.
517 </p>
518
519 <p>We can provide a remote appender to which our example receiver
520 can connect, using the same appender example used previously. The
521 example loads a logback configuration containing a
522 <code>SSLServerSocketAppender</code>, and then awaits input from the
523 user consisting of a message that will be delivered to connected
524 receivers. We can run the example appender application as follows:
525 </p>
526
527 <p class="source">java -Dport=6001 \
528 -Dkeystore=file:src/main/java/chapters/appenders/socket/ssl/keystore.jks \
529 -Dpassword=changeit \
530 chapters.receivers.socket.AppenderExample \
531 src/main/java/chapters/receivers/socket/appender4.xml</p>
532
533 <p>If you enter a message to send when the receiver is not connected,
534 note that the message is simply discarded.</p>
535
536 <p>It is important to note once again that our example is using a
537 self-signed X.509 credential that is suitable for testing and
538 experimentation, only. <strong>In a production setting, you should
539 obtain an appropriate X.509 credential to identify your SSL-enabled
540 logback components</strong>. See <a href="usingSSL.html">Using SSL</a>
541 for more information.</p>
542
543 <script src="../templates/footer.js" type="text/javascript"></script>
544
545 </div>
546 </body>
547 </html>