| 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>Chapter 9: Logging separation</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 |
</head> |
| 13 |
<body onload="prettyPrint(); decorate();"> |
| 14 |
<script type="text/javascript">prefix='../';</script> |
| 15 |
<script type="text/javascript" src="../js/prettify.js"></script> |
| 16 |
<script type="text/javascript" src="../templates/header.js"></script> |
| 17 |
<script type="text/javascript" src="../js/jquery-min.js"></script> |
| 18 |
<script type="text/javascript" src="../js/decorator.js"></script> |
| 19 |
|
| 20 |
<div id="left"> |
| 21 |
<noscript>Please turn on Javascript to view this menu</noscript> |
| 22 |
<script src="../templates/left.js" type="text/javascript"></script> |
| 23 |
</div> |
| 24 |
<div id="right"> |
| 25 |
<script src="menu.js" type="text/javascript"></script> |
| 26 |
</div> |
| 27 |
<div id="content"> |
| 28 |
|
| 29 |
<h1>Chapter 9: Logging separation</h1> |
| 30 |
|
| 31 |
<a href="loggingSeparation_ja.html">和訳 (Japanese translation)</a> |
| 32 |
|
| 33 |
<div class="quote"> |
| 34 |
<p><em>It is not knowledge, but the act of learning, not |
| 35 |
possession but the act of getting there, which grants the greatest |
| 36 |
enjoyment. When I have clarified and exhausted a subject, then I |
| 37 |
turn away from it, in order to go into darkness again; the |
| 38 |
never-satisfied man is so strange if he has completed a structure, |
| 39 |
then it is not in order to dwell in it peacefully, but in order to |
| 40 |
begin another. I imagine the world conqueror must feel thus, who, |
| 41 |
after one kingdom is scarcely conquered, stretches out his arms |
| 42 |
for others.</em></p> |
| 43 |
|
| 44 |
<p>—KARL FRIEDRICH GAUSS, Letter to Bolyai, 1808.</p> |
| 45 |
|
| 46 |
<p><em>Style, like sheer silk, too often hides eczema.</em></p> |
| 47 |
|
| 48 |
<p>—ALBERT CAMUS, <em>The Fall</em></p> |
| 49 |
|
| 50 |
</div> |
| 51 |
|
| 52 |
<script src="../templates/creative.js" type="text/javascript"></script> |
| 53 |
<script src="../templates/setup.js" type="text/javascript"></script> |
| 54 |
|
| 55 |
<h2>The problem: Logging Separation</h2> |
| 56 |
|
| 57 |
<p>The chapter deals with a relatively difficult problem of |
| 58 |
providing a separate logging environment for multiple applications |
| 59 |
running on the same web or EJB container. In the remainder of this |
| 60 |
chapter the term "application" will be used to refer to either a |
| 61 |
web-application or a J2EE application interchangeably. In a |
| 62 |
separated logging environment, each application sees a distinct |
| 63 |
logback environment, so that the logback configuration of one |
| 64 |
application does not interfere with the settings of another. In |
| 65 |
more technical terms, each web-application has a distinct copy of |
| 66 |
<code>LoggerContext</code> reserved for its own use. Recall that |
| 67 |
in logback, each logger object is manufactured by a |
| 68 |
<code>LoggerContext</code> to which it remains attached for as |
| 69 |
long as the logger object lives in memory. A variant of this |
| 70 |
problem is the separation of application logging and the logging |
| 71 |
of the container itself. |
| 72 |
</p> |
| 73 |
|
| 74 |
<h2 class="doAnchor" name="easy">The simplest and easiest |
| 75 |
approach</h2> |
| 76 |
|
| 77 |
<p>Assuming your container supports child-first class loading, |
| 78 |
separation of logging can be accomplished by embedding a copy of |
| 79 |
slf4j and logback jar files in each of your applications. For |
| 80 |
web-applications, placing slf4j and logback jar files under the |
| 81 |
<em>WEB-INF/lib</em> directory of the web-application is |
| 82 |
sufficient to endow each web-application with a separate logging |
| 83 |
environment. A copy of the <em>logback.xml</em> configuration file |
| 84 |
placed under <em>WEB-INF/classes</em> will be picked up when |
| 85 |
logback is loaded into memory. |
| 86 |
</p> |
| 87 |
|
| 88 |
<p>By virtue of class loader separation provided by the container, |
| 89 |
each web-application will load its own copy of |
| 90 |
<code>LoggerContext</code> which will pickup its own copy of |
| 91 |
<em>logback.xml</em>.</p> |
| 92 |
|
| 93 |
<p>Easy as pie.</p> |
| 94 |
|
| 95 |
<p>Well, not exactly. Sometimes you will be forced to place SLF4J |
| 96 |
and logback artifacts in a place accessible from all applications, |
| 97 |
typically because a shared library uses SLF4J. In that case, all |
| 98 |
applications will share the same logging environment. There are |
| 99 |
various other scenarios where a copy of SLF4J and logback |
| 100 |
artifacts is necessarily placed at a spot where it can be seen by |
| 101 |
all applications making logging separation by class loader |
| 102 |
separation impossible. All hope is not lost. Please read on. |
| 103 |
</p> |
| 104 |
|
| 105 |
<h2 class="doAnchor" name="contextSelectors">Context |
| 106 |
Selectors</h2> |
| 107 |
|
| 108 |
<p>Logback provides a mechanism for a single instance of SLF4J and |
| 109 |
logback classes loaded into memory to provide multiple logger |
| 110 |
contexts. When you write: |
| 111 |
</p> |
| 112 |
|
| 113 |
<pre class="prettyprint source">Logger logger = LoggerFactory.getLogger("foo");</pre> |
| 114 |
|
| 115 |
<p>the <code>getLogger</code>() method in |
| 116 |
<code>LoggerFactory</code> class will ask the SLF4J binding for a |
| 117 |
<code>ILoggerFactory</code>. When SLF4J is bound to logback, the |
| 118 |
task of returning an <code>ILoggerFactory</code> is delegated to |
| 119 |
an instance of <a |
| 120 |
href="../apidocs/ch/qos/logback/classic/selector/ContextSelector.html">ContextSelector</a>. Note |
| 121 |
that <code>ContextSelector</code> implementations always return |
| 122 |
instances <code>LoggerContext</code>. This class implements the |
| 123 |
<code>ILoggerFactory</code> interface. In other words, a context |
| 124 |
selector has the option to returning any |
| 125 |
<code>LoggerContext</code> instance it sees fit according to its |
| 126 |
own criteria. Hence the name context <em>selector</em>. |
| 127 |
</p> |
| 128 |
|
| 129 |
<p>By default, the logback binding uses <a |
| 130 |
href="../xref/ch/qos/logback/classic/selector/DefaultContextSelector.html">DefaultContextSelector</a> |
| 131 |
which always returns the same <code>LoggerContext</code>, called |
| 132 |
the default logger context.</p> |
| 133 |
|
| 134 |
<p>You can specify a different context selector by setting the |
| 135 |
<em>logback.ContextSelector</em> system property. Suppose you |
| 136 |
would like to specify that context selector to an instance of the |
| 137 |
<code>myPackage.myContextSelector</code> class, you would add the |
| 138 |
following system property: </p> |
| 139 |
|
| 140 |
<p class="source">-Dlogback.ContextSelector=myPackage.myContextSelector</p> |
| 141 |
|
| 142 |
<p>The context selector needs to implement the |
| 143 |
<code>ContextSelector</code> interface and have a constructor |
| 144 |
method admitting a <code>LoggerContext</code> instance as its only |
| 145 |
parameter. |
| 146 |
</p> |
| 147 |
|
| 148 |
|
| 149 |
<h3 class="doAnchor" |
| 150 |
name="ContextJNDISelector">ContextJNDISelector</h3> |
| 151 |
|
| 152 |
<p>Logback-classic ships with a selector called |
| 153 |
<code>ContextJNDISelector</code> which selects the logger context |
| 154 |
based on data available via JNDI lookup. This approach leverages |
| 155 |
JNDI data separation mandated by the J2EE specification. Thus, the |
| 156 |
same environment variable can be set to carry a different value in |
| 157 |
different applications. In other words, calling |
| 158 |
<code>LoggerFactory.getLogger()</code> from different applications |
| 159 |
will return a logger attached to a different logger context, even |
| 160 |
if there is a single LoggerFactory class loaded into memory shared |
| 161 |
by all applications. That's logging separation for you. |
| 162 |
</p> |
| 163 |
|
| 164 |
<p>To enable <code>ContextJNDISelector</code>, the |
| 165 |
<em>logback.ContextSelector</em> system property needs to be set |
| 166 |
to "JNDI", as follows:</p> |
| 167 |
|
| 168 |
<p class="source">-Dlogback.ContextSelector=JNDI</p> |
| 169 |
|
| 170 |
<p>Note that the value <code>JNDI</code> is a convenient shorthand |
| 171 |
for |
| 172 |
<code>ch.qos.logback.classic.selector.ContextJNDISelector</code>.</p> |
| 173 |
|
| 174 |
<h3 class="doAnchor" name="settingJNDIVariables">Setting JNDI |
| 175 |
variables in applications</h3> |
| 176 |
|
| 177 |
<p>In each of your applications, you need to name the logging |
| 178 |
context for the application. For a web-application, JNDI |
| 179 |
environment entries are specified within the <em>web.xml</em> |
| 180 |
file. If "kenobi" was the name of your application, you would add |
| 181 |
the following XML element to kenobi's web.xml file:</p> |
| 182 |
|
| 183 |
<pre class="prettyprint source"><env-entry> |
| 184 |
<env-entry-name>logback/context-name</env-entry-name> |
| 185 |
<env-entry-type>java.lang.String</env-entry-type> |
| 186 |
<env-entry-value>kenobi</env-entry-value> |
| 187 |
</env-entry></pre> |
| 188 |
|
| 189 |
<p>Assuming you have enabled <code>ContextJNDISelector</code>, |
| 190 |
logging for Kenobi will be done using a logger context named |
| 191 |
"kenobi". Moreover, the "kenobi" logger context will be |
| 192 |
initialized by <em>convention</em> by looking up the configuration |
| 193 |
file called <em>logback-kenobi.xml</em> as a <em>resource</em> |
| 194 |
using the thread context class loader. Thus, for example for the |
| 195 |
kenobi web-application, <em>logback-kenobi.xml</em> should be |
| 196 |
placed under the <em>WEB-INF/classes</em> folder. |
| 197 |
</p> |
| 198 |
|
| 199 |
<p>If you wish to, you may specify a different configuration file |
| 200 |
other than the convention, by setting the |
| 201 |
"logback/configuration-resource" JNDI variable. For example, for |
| 202 |
the kenobi web-application, if you wish to specify |
| 203 |
<em>aFolder/my_config.xml</em> instead of the conventional |
| 204 |
<em>logback-kenobi.xml</em>, you would add the following XML |
| 205 |
element to web.xml |
| 206 |
</p> |
| 207 |
|
| 208 |
|
| 209 |
<pre class="prettyprint source"><env-entry> |
| 210 |
<env-entry-name>logback/configuration-resource</env-entry-name> |
| 211 |
<env-entry-type>java.lang.String</env-entry-type> |
| 212 |
<env-entry-value>aFolder/my_config.xml</env-entry-value> |
| 213 |
</env-entry></pre> |
| 214 |
|
| 215 |
<p>The file <em>my_config.xml</em> should be placed under |
| 216 |
<em>WEB-INF/classes/aFolder/</em>. The important point to remember |
| 217 |
is that the configuration is looked up as a Java resource using |
| 218 |
the current thread's context class loader. |
| 219 |
</p> |
| 220 |
|
| 221 |
|
| 222 |
<h3 class="doAnchor" name="jndiTomcat">Configuring Tomcat for |
| 223 |
ContextJNDISelector</h3> |
| 224 |
|
| 225 |
<p>First, place the logback jars (that is |
| 226 |
logback-classic-${project.version}.jar, |
| 227 |
logback-core-${project.version}.jar and |
| 228 |
slf4j-api-${slf4j.version}.jar) in Tomcat's global (shared) class |
| 229 |
folder. In Tomcat 6.x, this directory is |
| 230 |
<em>$TOMCAT_HOME/lib/</em>. |
| 231 |
</p> |
| 232 |
|
| 233 |
<p>The <em>logback.ContextSelector</em> system property can be set |
| 234 |
by adding the following line to the <em>catalina.sh</em> script, |
| 235 |
catalina.bat in Windows, found under <em>$TOMCAT_HOME/bin</em> |
| 236 |
folder.</p> |
| 237 |
|
| 238 |
<p class="source">JAVA_OPTS="$JAVA_OPTS -Dlogback.ContextSelector=JNDI"</p> |
| 239 |
|
| 240 |
|
| 241 |
<h3 class="doAnchor" name="hotDeploy">Hot deploying |
| 242 |
applications</h3> |
| 243 |
|
| 244 |
<p>When the web-application is recycled or shutdown, we strongly |
| 245 |
recommend that the incumbent <code>LoggerContext</code> be closed |
| 246 |
so that it can be properly garbage collected. Logback ships with a |
| 247 |
<code>ServletContextListener</code> called <a |
| 248 |
href="../xref/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.html"><code>ContextDetachingSCL</code></a>, |
| 249 |
designed specifically for detaching the |
| 250 |
<code>ContextSelector</code> instance associated with the older |
| 251 |
web-application instance. It can be installed by adding the |
| 252 |
following lines into your web-applications <em>web.xml</em> |
| 253 |
file.</p> |
| 254 |
|
| 255 |
<pre class="prettyprint source"><listener> |
| 256 |
<listener-class>ch.qos.logback.classic.selector.servlet.ContextDetachingSCL</listener-class> |
| 257 |
</listener></pre> |
| 258 |
|
| 259 |
<p><span class="label notice">Note</span> Most containers invoke |
| 260 |
the <code>contextInitialized()</code> method of listeners in the |
| 261 |
order in which they are declared but invoke their |
| 262 |
<code>contextDestroyed()</code> method in reverse order. It |
| 263 |
follows that if you have multiple |
| 264 |
<code>ServletContextListener</code> declarations in your |
| 265 |
<em>web.xml</em>, then <code>ContextDetachingSCL</code> should be |
| 266 |
declared <em>first</em> so that its |
| 267 |
<code>contextDestroyed()</code> method is invoked <em>last</em> |
| 268 |
during application shutdown. </p> |
| 269 |
|
| 270 |
<h3 class="doAnchor" name="betterPerf">Better performance</h3> |
| 271 |
|
| 272 |
<p>When <code>ContextJNDISelector</code> is active, each time a |
| 273 |
logger is retrieved, a JNDI lookup must be performed. This can |
| 274 |
negatively impact performance, especially if you are using |
| 275 |
non-static (a.k.a. instance) logger references. Logback ships with a |
| 276 |
servlet filter named <a |
| 277 |
href="../xref/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.html">LoggerContextFilter</a>, |
| 278 |
specifically designed to avoid the JNDI lookup cost. It can |
| 279 |
be installed by adding the following lines to your application's |
| 280 |
web.xml file.</p> |
| 281 |
|
| 282 |
<pre class="prettyprint source"><filter> |
| 283 |
<filter-name>LoggerContextFilter</filter-name> |
| 284 |
<filter-class>ch.qos.logback.classic.selector.servlet.LoggerContextFilter</filter-class> |
| 285 |
</filter> |
| 286 |
<filter-mapping> |
| 287 |
<filter-name>LoggerContextFilter</filter-name> |
| 288 |
<url-pattern>/*</url-pattern> |
| 289 |
</filter-mapping></pre> |
| 290 |
|
| 291 |
<p>At the beginning of each http-request, |
| 292 |
<code>LoggerContextFilter</code> will obtain the logger context |
| 293 |
associated with the application and then place it in a |
| 294 |
<code>ThreadLocal</code> variable. <code>ContextJNDISelector</code> |
| 295 |
will first check if the <code>ThreadLocal</code> variable is |
| 296 |
set. If it is set, then JNDI lookup will skipped. Note that at the |
| 297 |
end of the http request, the <code>ThreadLocal</code> variable will |
| 298 |
be nulled. Installing <code>LoggerContextFilter</code> improves |
| 299 |
logger retrieval performance by a wide margin. |
| 300 |
</p> |
| 301 |
|
| 302 |
<p>Nulling the <code>ThreadLocal</code> variable allows garbage |
| 303 |
collection of the web-application when it is stopped or |
| 304 |
recycled.</p> |
| 305 |
|
| 306 |
|
| 307 |
<h2 class="doAnchor" name="tamingStaticRefs">Taming static |
| 308 |
references in shared libraries</h2> |
| 309 |
|
| 310 |
<p><code>ContextJNDISelector</code> works nicely to create logging |
| 311 |
separation when SLF4J and logback artifacts are shared by all |
| 312 |
applications. When <code>ContextJNDISelector</code> is active, each |
| 313 |
call to <code>LoggerFactory.getLogger()</code> will return a logger |
| 314 |
belonging to a logger context associated with the calling/current |
| 315 |
application.</p> |
| 316 |
|
| 317 |
<p>The common idiom for referencing a logger is via a static |
| 318 |
reference. For example, |
| 319 |
</p> |
| 320 |
|
| 321 |
<pre class="prettyprint source">public class Foo { |
| 322 |
<b>static</b> Logger logger = LoggerFactory.getLogger(Foo.class); |
| 323 |
... |
| 324 |
}</pre> |
| 325 |
|
| 326 |
<p>Static logger references are both memory and CPU |
| 327 |
efficient. Only one logger reference is used for all instances of |
| 328 |
the class. Moreover, the logger instance is retrieved only once, |
| 329 |
when the class is loaded into memory. If the host class belongs to |
| 330 |
some application, say kenobi, then the static logger will be |
| 331 |
attached to kenobi's logger context by virtue of |
| 332 |
<code>ContextJNDISelector</code>. Similarly, if the host class |
| 333 |
belongs to some other application, say yoda, then its static |
| 334 |
logger reference will be attached to yoda's logger context, again |
| 335 |
by virtue of <code>ContextJNDISelector</code>. |
| 336 |
</p> |
| 337 |
|
| 338 |
<p>If a class, say <code>Mustafar</code>, belongs to a library |
| 339 |
shared by both <em>kenobi</em> and <em>yoda</em>, as long as |
| 340 |
<code>Mustafar</code> has non static loggers, each invocation of |
| 341 |
<code>LoggerFactory.getLogger()</code> will return a logger |
| 342 |
belonging to a logger context associated with the calling/current |
| 343 |
application. But if <code>Mustafar</code> has a static logger |
| 344 |
reference, then its logger will be attached to the logger context of the |
| 345 |
application that calls it first. Thus, |
| 346 |
<code>ContextJNDISelector</code> does not provide logging |
| 347 |
separation in case of shared classes using static logger |
| 348 |
references. This corner case has eluded a solution for eons.</p> |
| 349 |
|
| 350 |
|
| 351 |
<p>The only way to solve this issue transparently and perfectly |
| 352 |
would be to introduce another level of indirection inside loggers |
| 353 |
so that each logger-shell somehow delegated work to an inner |
| 354 |
logger attached to the appropriate context. This approach would be |
| 355 |
quite difficult to implement and would incur a significant |
| 356 |
computational overhead. It is not an approach we plan to pursue. |
| 357 |
</p> |
| 358 |
|
| 359 |
<p>It goes without saying that one could trivially solve the |
| 360 |
"shared class static logger" problem by moving the shared classes |
| 361 |
inside the web-apps (unshare them). If unsharing is not possible, |
| 362 |
then we can solicit the magical powers of <a |
| 363 |
href="appenders.html#SiftingAppender"><code>SiftingAppender</code></a> |
| 364 |
in order to separate logging using JNDI data as separation |
| 365 |
criteria. |
| 366 |
</p> |
| 367 |
|
| 368 |
<p>Logback ships with a discriminator called <a |
| 369 |
href="../xref/ch/qos/logback/classic/sift/JNDIBasedContextDiscriminator.html">JNDIBasedContextDiscriminator</a> |
| 370 |
which returns the name of the current logger context as computed |
| 371 |
by <code>ContextJNDISelector</code>. The |
| 372 |
<code>SiftingAppender</code> and |
| 373 |
<code>JNDIBasedContextDiscriminator</code> combination will create |
| 374 |
separate appenders for each web-application. |
| 375 |
</p> |
| 376 |
|
| 377 |
<pre class="prettyprint source"><configuration> |
| 378 |
|
| 379 |
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> |
| 380 |
|
| 381 |
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> |
| 382 |
<discriminator class="ch.qos.logback.classic.sift.JNDIBasedContextDiscriminator"> |
| 383 |
<defaultValue>unknown</defaultValue> |
| 384 |
</discriminator> |
| 385 |
<sift> |
| 386 |
<appender name="FILE-${contextName}" class="ch.qos.logback.core.FileAppender"> |
| 387 |
<file><b>${contextName}.log</b></file> |
| 388 |
<encoder> |
| 389 |
<pattern>%-50(%level %logger{35}) cn=%contextName - %msg%n</pattern> |
| 390 |
</encoder> |
| 391 |
</appender> |
| 392 |
</sift> |
| 393 |
</appender> |
| 394 |
|
| 395 |
<root level="DEBUG"> |
| 396 |
<appender-ref ref="SIFT" /> |
| 397 |
</root> |
| 398 |
</configuration></pre> |
| 399 |
|
| 400 |
|
| 401 |
<p>If kenobi and yoda are web-applications, then the above |
| 402 |
configuration will output yoda's log output to <em>yoda.log</em> |
| 403 |
and kenobi's logs to <em>kenobi.log</em>; this even works for logs generated |
| 404 |
by static logger references located in shared classes.</p> |
| 405 |
|
| 406 |
<p>You can try out the technique just described with the help of the |
| 407 |
<a |
| 408 |
href="http://github.com/ceki/logback-starwars">logback-starwars</a> |
| 409 |
project. |
| 410 |
</p> |
| 411 |
|
| 412 |
|
| 413 |
<p>The above approach solves the logging separation problem but is |
| 414 |
rather complex. It requires the proper installation of |
| 415 |
<code>ContextJNDISelector</code> and mandates that appenders be |
| 416 |
wrapped by <code>SiftingAppender</code> which is a non-trivial beast |
| 417 |
in itself. |
| 418 |
</p> |
| 419 |
|
| 420 |
<p>Note that each logging context can be configured using the same |
| 421 |
file or alternatively different files. The choice is up to |
| 422 |
you. Instructing all contexts to use the same configuration file |
| 423 |
is simpler as only one file has to be maintained. Maintaining a |
| 424 |
distinct configuration file for each application is harder to |
| 425 |
maintain but allows for more flexibility.</p> |
| 426 |
|
| 427 |
<p>So are we done yet? Can we declare victory and go home? Well, not |
| 428 |
quite.</p> |
| 429 |
|
| 430 |
<p>Let's assume the web-application <code>yoda</code> is initialized |
| 431 |
before <code>kenobi</code>. To initialize <code>yoda</code>, visit |
| 432 |
<code>http://localhost:port/yoda/servlet</code> which will invoke |
| 433 |
the <code>YodaServlet</code>. This servlet just says hello and logs |
| 434 |
message before calling the <code>foo</code> method in |
| 435 |
<code>Mustafar</code> which not-surprisingly logs a simple message |
| 436 |
and returns. |
| 437 |
</p> |
| 438 |
|
| 439 |
<p>After <code>YodaServlet</code> is called, the contents of |
| 440 |
<em>yoda.log</em> file should contain</p> |
| 441 |
|
| 442 |
<pre class="source">DEBUG ch.qos.starwars.yoda.YodaServlet cn=yoda - in doGet() |
| 443 |
DEBUG ch.qos.starwars.shared.Mustafar cn=yoda - in foo()</pre> |
| 444 |
|
| 445 |
<p>Note how both log entries are associated with the "yoda" context |
| 446 |
name. At this stage and until the server stops, the |
| 447 |
<code>ch.qos.starwars.shared.Mustafar</code> logger is attached to |
| 448 |
the 'yoda' context and will remain so until the server is stopped. |
| 449 |
</p> |
| 450 |
|
| 451 |
<p>Visiting <code>http://localhost:port/kenobi/servlet</code> will |
| 452 |
output the following in <em>kenobi.log</em>.</p> |
| 453 |
|
| 454 |
<pre class="source">DEBUG ch.qos.starwars.kenobi.KenobiServlet <b>cn=kenobi</b> - in doGet() |
| 455 |
DEBUG ch.qos.starwars.shared.Mustafar <b>cn=yoda</b> - in foo()</pre> |
| 456 |
|
| 457 |
<p>Note that even if the |
| 458 |
<code>ch.qos.starwars.shared.Mustafar</code> logger outputs to |
| 459 |
<em>kenobi.log</em> it is still attached to 'yoda'. Thus, we have |
| 460 |
two distinct logging contexts logging to the same file, in this |
| 461 |
case <em>kenobi.log</em>. Each of these contexts reference |
| 462 |
<code>FileAppender</code> instances, nested within distinct |
| 463 |
<code>SiftingAppender</code> instances, that are logging to the same |
| 464 |
file. Although logging separation seems to function according to |
| 465 |
our wishes, FileAppender instances cannot safely write to the same |
| 466 |
file unless they enable <span class="option">prudent</span> |
| 467 |
mode. Otherwise, the target file will be corrupted.</p> |
| 468 |
|
| 469 |
<p>Here is the configuration file enabling prudent mode:</p> |
| 470 |
|
| 471 |
<pre class="prettyprint source"><configuration> |
| 472 |
|
| 473 |
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> |
| 474 |
|
| 475 |
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> |
| 476 |
<discriminator class="ch.qos.logback.classic.sift.JNDIBasedContextDiscriminator"> |
| 477 |
<defaultValue>unknown</defaultValue> |
| 478 |
</discriminator> |
| 479 |
<sift> |
| 480 |
<appender name="FILE-${contextName}" class="ch.qos.logback.core.FileAppender"> |
| 481 |
<file>${contextName}.log</file> |
| 482 |
<b><prudent>true</prudent></b> |
| 483 |
<encoder> |
| 484 |
<pattern>%-50(%level %logger{35}) cn=%contextName - %msg%n</pattern> |
| 485 |
</encoder> |
| 486 |
</appender> |
| 487 |
</sift> |
| 488 |
</appender> |
| 489 |
|
| 490 |
<root level="DEBUG"> |
| 491 |
<appender-ref ref="SIFT" /> |
| 492 |
</root> |
| 493 |
</configuration></pre> |
| 494 |
|
| 495 |
|
| 496 |
<p>If you were able to keep up with the discussion thus far and |
| 497 |
have actually tried the logback-starwars examples, then you must be |
| 498 |
truly obsessed with logging. You should consider seeking <a |
| 499 |
href="http://www.qos.ch/shop/products/professionalSupport">professional |
| 500 |
help</a>.</p> |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
<script src="../templates/footer.js" |
| 505 |
type="text/javascript"></script> </div> |
| 506 |
|
| 507 |
</body> |
| 508 |
</html> |