| 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 7: Filters</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(); decorate();"> |
| 15 |
<script type="text/javascript">prefix='../';</script> |
| 16 |
<script type="text/javascript" src="../js/prettify.js"></script> |
| 17 |
<script type="text/javascript" src="../templates/header.js"></script> |
| 18 |
<script type="text/javascript" src="../js/dsl.js"></script> |
| 19 |
<script type="text/javascript" src="../js/jquery-min.js"></script> |
| 20 |
<script type="text/javascript" src="../js/decorator.js"></script> |
| 21 |
|
| 22 |
<div id="left"> |
| 23 |
<noscript>Please turn on Javascript to view this menu</noscript> |
| 24 |
<script src="../templates/left.js" type="text/javascript"></script> |
| 25 |
</div> |
| 26 |
<div id="right"> |
| 27 |
<script src="menu.js" type="text/javascript"></script> |
| 28 |
</div> |
| 29 |
<div id="content"> |
| 30 |
|
| 31 |
<h1>Chapter 7: Filters</h1> |
| 32 |
|
| 33 |
<a href="filters_ja.html">和訳 (Japanese translation)</a> |
| 34 |
|
| 35 |
<div class="quote"> |
| 36 |
<p><em>Have lots of ideas and throw away the bad ones. You aren't |
| 37 |
going to have good ideas unless you have lots of ideas and some |
| 38 |
sort of principle of selection.</em></p> |
| 39 |
|
| 40 |
<p>—LINUS PAULING</p> |
| 41 |
</div> |
| 42 |
|
| 43 |
<script src="../templates/creative.js" type="text/javascript"></script> |
| 44 |
|
| 45 |
|
| 46 |
<p>In the preceding chapters, the <a |
| 47 |
href="architecture.html#basic_selection">basic selection rule</a>, |
| 48 |
which lies at the heart of logback-classic, has been presented. In |
| 49 |
this chapter, additional filtering methods will be introduced. |
| 50 |
</p> |
| 51 |
|
| 52 |
|
| 53 |
<p>Logback filters are based on ternary logic allowing them to be |
| 54 |
assembled or chained together to compose an arbitrarily complex |
| 55 |
filtering policy. They are largely inspired by Linux's iptables. |
| 56 |
</p> |
| 57 |
|
| 58 |
<script src="../templates/setup.js" type="text/javascript"></script> |
| 59 |
|
| 60 |
<h2>In logback-classic</h2> |
| 61 |
|
| 62 |
|
| 63 |
<p>Logback-classic offers two types of filters, regular filters |
| 64 |
and turbo filters. |
| 65 |
</p> |
| 66 |
|
| 67 |
<h3 class="doAnchor" name="filter">Regular filters</h3> |
| 68 |
|
| 69 |
<p>Regular logback-classic filters extend the <a |
| 70 |
href="../xref/ch/qos/logback/core/filter/Filter.html"><code>Filter</code></a> |
| 71 |
abstract class which essentially consists of a single |
| 72 |
<code>decide()</code> method taking an <code>ILoggingEvent</code> |
| 73 |
instance as its parameter. |
| 74 |
</p> |
| 75 |
|
| 76 |
|
| 77 |
<p>Filters are organized as an ordered list and are based on |
| 78 |
ternary logic. The <code>decide(ILoggingEvent event)</code> method |
| 79 |
of each filter is called in sequence. This method returns one of |
| 80 |
the <a |
| 81 |
href="../xref/ch/qos/logback/core/spi/FilterReply.html"><code>FilterReply</code></a> |
| 82 |
enumeration values, i.e. one of <code>DENY</code>, |
| 83 |
<code>NEUTRAL</code> or <code>ACCEPT</code>. If the value |
| 84 |
returned by <code>decide</code>() is <code>DENY</code>, then the |
| 85 |
log event is dropped immediately without consulting the remaining |
| 86 |
filters. If the value returned is <code>NEUTRAL</code>, then the |
| 87 |
next filter in the list is consulted. If there are no further |
| 88 |
filters to consult, then the logging event is processed normally. |
| 89 |
If the returned value is <code>ACCEPT</code>, then the logging |
| 90 |
event is processed immediately skipping the invocation of the |
| 91 |
remaining filters. |
| 92 |
</p> |
| 93 |
|
| 94 |
<p>In logback-classic, filters can be added to |
| 95 |
<code>Appender</code> instances. By adding one or more filters to |
| 96 |
an appender, you can filter events by arbitrary criteria, such as |
| 97 |
the contents of the log message, the contents of the MDC, the time |
| 98 |
of day or any other part of the logging event. |
| 99 |
</p> |
| 100 |
|
| 101 |
<h3 class="doAnchor" name="yourOwnFilter">Implementing your own |
| 102 |
Filter</h3> |
| 103 |
|
| 104 |
<p>Creating your own filter is easy. All you have to do is extend |
| 105 |
the <code>Filter</code> abstract class and implement the |
| 106 |
<code>decide()</code> method. |
| 107 |
</p> |
| 108 |
|
| 109 |
<p>The SampleFilter class shown below provides an example. Its |
| 110 |
<code>decide</code> method returns ACCEPT for logging events |
| 111 |
containing the string "sample" in its message field. For other |
| 112 |
events, the value NEUTRAL is returned. |
| 113 |
</p> |
| 114 |
|
| 115 |
<em>Example: Basic custom filter (<a |
| 116 |
href="../xref/chapters/filters/SampleFilter.html">logback-examples/src/main/java/chapters/filters/SampleFilter.java</a>)</em> |
| 117 |
<pre class="prettyprint source">package chapters.filters; |
| 118 |
|
| 119 |
import ch.qos.logback.classic.spi.ILoggingEvent; |
| 120 |
import ch.qos.logback.core.filter.Filter; |
| 121 |
import ch.qos.logback.core.spi.FilterReply; |
| 122 |
|
| 123 |
public class SampleFilter extends Filter<ILoggingEvent> { |
| 124 |
|
| 125 |
@Override |
| 126 |
public FilterReply decide(ILoggingEvent event) { |
| 127 |
if (event.getMessage().contains("sample")) { |
| 128 |
return FilterReply.ACCEPT; |
| 129 |
} else { |
| 130 |
return FilterReply.NEUTRAL; |
| 131 |
} |
| 132 |
} |
| 133 |
}</pre> |
| 134 |
|
| 135 |
<p>The configuration files shown next attaches a |
| 136 |
<code>SampleFilter</code> to a <code>ConsoleAppender</code>. |
| 137 |
</p> |
| 138 |
|
| 139 |
<em>Example: SampleFilter configuration |
| 140 |
(logback-examples/src/main/resources/chapters/filters/SampleFilterConfig.xml)</em> |
| 141 |
<span class="asGroovy" onclick="return asGroovy('SampleFilterConfig');">View as .groovy</span> |
| 142 |
<pre id="SampleFilterConfig" class="prettyprint source"><configuration> |
| 143 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 144 |
|
| 145 |
<b><filter class="chapters.filters.SampleFilter" /></b> |
| 146 |
|
| 147 |
<encoder> |
| 148 |
<pattern> |
| 149 |
%-4relative [%thread] %-5level %logger - %msg%n |
| 150 |
</pattern> |
| 151 |
</encoder> |
| 152 |
</appender> |
| 153 |
|
| 154 |
<root> |
| 155 |
<appender-ref ref="STDOUT" /> |
| 156 |
</root> |
| 157 |
</configuration></pre> |
| 158 |
|
| 159 |
<p>With the help of Joran, logback's configuration framework, |
| 160 |
specifying properties or sub-components to filters is also |
| 161 |
easy. After adding the corresponding setter method in the filter |
| 162 |
class, specify the value of the property in an xml element named |
| 163 |
after the property, nesting it within a <code><filter></code> |
| 164 |
element. |
| 165 |
</p> |
| 166 |
|
| 167 |
<p>Often, the desired filter logic consists of two |
| 168 |
orthogonal parts, a match/mismatch test and a response depending |
| 169 |
on the match/mismatch. For example, for a given test, e.g. message |
| 170 |
equals "foobar", one filter might respond ACCEPT on match and |
| 171 |
NEUTRAL on mismatch, and another filter might respond NEUTRAL on |
| 172 |
match and DENY on mismatch. |
| 173 |
</p> |
| 174 |
|
| 175 |
<p>Taking notice of this orthogonality, logback ships with the <a |
| 176 |
href="../xref/ch/qos/logback/core/filter/AbstractMatcherFilter.html"> |
| 177 |
<code>AbstractMatcherFilter</code></a> class which provides a |
| 178 |
useful skeleton for specifying the appropriate response on match |
| 179 |
and on mismatch, with the help of two properties, named |
| 180 |
<em>OnMatch</em> and <em>OnMismatch</em>. Most of the regular |
| 181 |
filters included in logback are derived from |
| 182 |
<code>AbstractMatcherFilter</code>. |
| 183 |
</p> |
| 184 |
|
| 185 |
<h3 class="doAnchor" name="levelFilter">LevelFilter</h3> |
| 186 |
|
| 187 |
<p><a href="../xref/ch/qos/logback/classic/filter/LevelFilter.html"> |
| 188 |
<code>LevelFilter</code></a> filters events based on exact level |
| 189 |
matching. If the event's level is equal to the configured level, |
| 190 |
the filter accepts or denies the event, depending on the |
| 191 |
configuration of the <span class="option">onMatch</span> and <span |
| 192 |
class="option">onMismatch</span> properties. Here is a sample |
| 193 |
configuration file. |
| 194 |
</p> |
| 195 |
|
| 196 |
<em>Example: Sample LevelFilter configuration |
| 197 |
(logback-examples/src/main/resources/chapters/filters/levelFilterConfig.xml)</em> |
| 198 |
<span class="asGroovy" onclick="return asGroovy('levelFilterConfig');">View as .groovy</span> |
| 199 |
<pre id="levelFilterConfig" class="prettyprint source"><configuration> |
| 200 |
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> |
| 201 |
<b><filter class="ch.qos.logback.classic.filter.LevelFilter"> |
| 202 |
<level>INFO</level> |
| 203 |
<onMatch>ACCEPT</onMatch> |
| 204 |
<onMismatch>DENY</onMismatch> |
| 205 |
</filter></b> |
| 206 |
<encoder> |
| 207 |
<pattern> |
| 208 |
%-4relative [%thread] %-5level %logger{30} - %msg%n |
| 209 |
</pattern> |
| 210 |
</encoder> |
| 211 |
</appender> |
| 212 |
<root level="DEBUG"> |
| 213 |
<appender-ref ref="CONSOLE" /> |
| 214 |
</root> |
| 215 |
</configuration></pre> |
| 216 |
|
| 217 |
<h3 class="doAnchor" name="thresholdFilter">ThresholdFilter</h3> |
| 218 |
|
| 219 |
<p>The <a |
| 220 |
href="../xref/ch/qos/logback/classic/filter/ThresholdFilter.html"> |
| 221 |
<code>ThresholdFilter</code></a> filters events below the |
| 222 |
specified threshold. For events of level equal or above the |
| 223 |
threshold, <code>ThresholdFilter</code> will respond NEUTRAL when |
| 224 |
its <code>decide</code>() method is invoked. However, events with |
| 225 |
a level below the threshold will be denied. Here is a sample |
| 226 |
configuration file. |
| 227 |
</p> |
| 228 |
|
| 229 |
<em>Example: Sample ThresholdFilter configuration |
| 230 |
(logback-examples/src/main/resources/chapters/filters/thresholdFilterConfig.xml)</em> |
| 231 |
<span class="asGroovy" onclick="return asGroovy('thresholdFilterConfig');">View as .groovy</span> |
| 232 |
<pre id="thresholdFilterConfig" class="prettyprint source"><configuration> |
| 233 |
<appender name="CONSOLE" |
| 234 |
class="ch.qos.logback.core.ConsoleAppender"> |
| 235 |
<!-- deny all events with a level below INFO, that is TRACE and DEBUG --> |
| 236 |
<b><filter class="ch.qos.logback.classic.filter.ThresholdFilter"> |
| 237 |
<level>INFO</level> |
| 238 |
</filter></b> |
| 239 |
<encoder> |
| 240 |
<pattern> |
| 241 |
%-4relative [%thread] %-5level %logger{30} - %msg%n |
| 242 |
</pattern> |
| 243 |
</encoder> |
| 244 |
</appender> |
| 245 |
<root level="DEBUG"> |
| 246 |
<appender-ref ref="CONSOLE" /> |
| 247 |
</root> |
| 248 |
</configuration></pre> |
| 249 |
|
| 250 |
|
| 251 |
<h2 class="doAnchor" name="evalutatorFilter">EvaluatorFilter</h2> |
| 252 |
|
| 253 |
<p><a |
| 254 |
href="../xref/ch/qos/logback/core/filter/EvaluatorFilter.html"><code>EvaluatorFilter</code></a> |
| 255 |
is a generic filter encapsulating an |
| 256 |
<code>EventEvaluator</code>. As the name suggests, an |
| 257 |
<a |
| 258 |
href="../xref/ch/qos/logback/core/boolex/EventEvaluator.html"> |
| 259 |
<code>EventEvaluator</code></a> evaluates whether a given criteria |
| 260 |
is met for a given event. On match and on mismatch, |
| 261 |
the hosting <code>EvaluatorFilter</code> will return the value |
| 262 |
specified by the <span class="option">onMatch</span> |
| 263 |
or <span class="option">onMismatch</span> properties respectively. |
| 264 |
</p> |
| 265 |
|
| 266 |
|
| 267 |
<p>Note that <code>EventEvaluator</code> is an abstract class. You |
| 268 |
can implement your own event evaluation logic by sub-classing |
| 269 |
<code>EventEvaluator</code>. |
| 270 |
</p> |
| 271 |
|
| 272 |
|
| 273 |
<!-- ======================== GEventEvaluator ========================= --> |
| 274 |
|
| 275 |
<h3 class="doAnchor" name="GEventEvaluator">GEventEvaluator</h3> |
| 276 |
|
| 277 |
<p><a |
| 278 |
href="../xref/ch/qos/logback/classic/boolex/GEventEvaluator.html">GEventEvaluator</a> |
| 279 |
is a concrete <a |
| 280 |
href="../xref/ch/qos/logback/core/boolex/EventEvaluator.html"><code>EventEvaluator</code></a> |
| 281 |
implementation taking arbitrary Groovy language boolean |
| 282 |
expressions as the evaluation criteria. We refer such Groovy |
| 283 |
language boolean expressions as "groovy evaluation |
| 284 |
expressions". Groovy evaluation expressions enable hitherto |
| 285 |
unprecedented flexibility in event |
| 286 |
filtering. <code>GEventEvaluator</code> requires the Groovy |
| 287 |
runtime. Please see the <a |
| 288 |
href="../setup.html#groovy">corresponding section</a> of |
| 289 |
the setup document on adding the Groovy runtime to your class |
| 290 |
path. |
| 291 |
</p> |
| 292 |
|
| 293 |
<p>Evaluation expressions are compiled on-the-fly during the |
| 294 |
interpretation of the configuration file. As a user, you do not |
| 295 |
need to worry about the actual plumbing. However, it is your |
| 296 |
responsibility to ensure that the groovy-language expression is |
| 297 |
valid. |
| 298 |
</p> |
| 299 |
|
| 300 |
<p>The evaluation expression acts on the current logging event. |
| 301 |
Logback automatically inserts the current logging event of type <a |
| 302 |
href="../apidocs/ch/qos/logback/classic/spi/ILoggingEvent.html">ILoggingEvent</a> |
| 303 |
as a variable referred to as '<em>event</em>' as well as its |
| 304 |
shorthand referred to as '<em>e</em>'. The variables TRACE, DEBUG, |
| 305 |
INFO, WARN and ERROR are also exported into the scope of the |
| 306 |
expression. Thus, "event.level == DEBUG" and "e.level == DEBUG" |
| 307 |
are equivalent and valid groovy expressions which will return |
| 308 |
<code>true</code> only if the current logging event's level is |
| 309 |
identical to DEBUG. For other comparison operators on levels, the |
| 310 |
level field should be converted to integer with the |
| 311 |
<code>toInt()</code> operator. |
| 312 |
</p> |
| 313 |
|
| 314 |
<p>Here is a more complete example.</p> |
| 315 |
|
| 316 |
<span class="asGroovy" onclick="return asGroovy('GEventEvaluator');">View as .groovy</span> |
| 317 |
<pre id="GEventEvaluator" class="prettyprint source"><configuration> |
| 318 |
|
| 319 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 320 |
<b><filter class="ch.qos.logback.core.filter.EvaluatorFilter"> |
| 321 |
<evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator"> |
| 322 |
<expression> |
| 323 |
e.level.toInt() >= WARN.toInt() &amp;&amp; <!-- Stands for && in XML --> |
| 324 |
!(e.mdc?.get("req.userAgent") =~ /Googlebot|msnbot|Yahoo/ ) |
| 325 |
</expression> |
| 326 |
</evaluator> |
| 327 |
<OnMismatch>DENY</OnMismatch> |
| 328 |
<OnMatch>NEUTRAL</OnMatch> |
| 329 |
</filter></b> |
| 330 |
<encoder> |
| 331 |
<pattern> |
| 332 |
%-4relative [%thread] %-5level %logger - %msg%n |
| 333 |
</pattern> |
| 334 |
</encoder> |
| 335 |
</appender> |
| 336 |
|
| 337 |
<root level="DEBUG"> |
| 338 |
<appender-ref ref="STDOUT" /> |
| 339 |
</root> |
| 340 |
</configuration></pre> |
| 341 |
|
| 342 |
|
| 343 |
<p>The above filter will let events of level WARN and higher go |
| 344 |
through onto the console unless the error is generated by Web |
| 345 |
crawlers associated with Google, MSN or Yahoo. It does so by |
| 346 |
checking whether the MDC associated with the event contains a |
| 347 |
value for "req.userAgent" matching the |
| 348 |
<code>/Googlebot|msnbot|Yahoo/</code> regular expression. Note |
| 349 |
that since the MDC map can be null, we are also using Groovy's <a |
| 350 |
href="http://groovy.codehaus.org/Null+Object+Pattern">safe |
| 351 |
dereferencing operator</a>, that is the ?. operator. The equivalent |
| 352 |
logic would have been much longer if expressed in Java. |
| 353 |
</p> |
| 354 |
|
| 355 |
<p>If you are wondering how the identifier for the user agent was |
| 356 |
inserted into the MDC under the 'req.userAgent' key, it behooves |
| 357 |
us to mention that logback ships with a servlet filter named <a |
| 358 |
href="mdc.html#mis"><code>MDCInsertingServletFilter</code></a> |
| 359 |
designed for this purpose. It will be described in a later |
| 360 |
chapter. |
| 361 |
</p> |
| 362 |
|
| 363 |
<!-- ==================== JaninoEventEvaluator ======================== --> |
| 364 |
|
| 365 |
<h3 class="doAnchor" |
| 366 |
name="JaninoEventEvaluator">JaninoEventEvaluator</h3> |
| 367 |
|
| 368 |
|
| 369 |
<p>Logback-classic ships with another concrete |
| 370 |
<code>EventEvaluator</code> implementation called <a |
| 371 |
href="../xref/ch/qos/logback/classic/boolex/JaninoEventEvaluator.html">JaninoEventEvaluator</a> |
| 372 |
taking an arbitrary Java language block returning a boolean value |
| 373 |
as the evaluation criteria. We refer to such Java language boolean |
| 374 |
expressions as "<em>evaluation expressions</em>". Evaluation |
| 375 |
expressions enable great flexibility in event |
| 376 |
filtering. <code>JaninoEventEvaluator</code> requires the <a |
| 377 |
href="http://docs.codehaus.org/display/JANINO/Home">Janino |
| 378 |
library</a>. Please see the <a |
| 379 |
href="../setup.html#janino">corresponding section</a> of the setup |
| 380 |
document. Compared to <code>JaninoEventEvaluator</code>, |
| 381 |
<code>GEventEvaluator</code>, by virtue of the Groovy language, is |
| 382 |
significantly more convenient to use, but |
| 383 |
<code>JaninoEventEvaluator</code> will usually run (much) faster |
| 384 |
for equivalent expressions. |
| 385 |
</p> |
| 386 |
|
| 387 |
<p>Evaluation expressions are compiled on-the-fly during the |
| 388 |
interpretation of the configuration file. As a user, you do not |
| 389 |
need to worry about the actual plumbing. However, it is your |
| 390 |
responsibility to ensure that the Java language expression returns |
| 391 |
a boolean, i.e. that it evaluates to true or false. </p> |
| 392 |
|
| 393 |
|
| 394 |
<p>The evaluation expression is evaluated on the current logging |
| 395 |
event. Logback-classic automatically exports various fields of the |
| 396 |
logging event as variables accessible from the evaluation |
| 397 |
expression. The case-sensitive names of these exported variables |
| 398 |
are listed below. |
| 399 |
</p> |
| 400 |
|
| 401 |
<table class="bodyTable"> |
| 402 |
<tr> |
| 403 |
<th>Name</th> |
| 404 |
<th>Type</th> |
| 405 |
<th>Description</th> |
| 406 |
</tr> |
| 407 |
<tr> |
| 408 |
<td>event</td> |
| 409 |
<td><code>LoggingEvent</code></td> |
| 410 |
|
| 411 |
<td>The raw logging event associated with the logging |
| 412 |
request. All of the following variables are also available |
| 413 |
from the event. For example, <code>event.getMessage()</code> |
| 414 |
returns the same String value as the <em>message</em> variable |
| 415 |
described next. |
| 416 |
</td> |
| 417 |
</tr> |
| 418 |
|
| 419 |
<tr class="alt"> |
| 420 |
<td>message</td> |
| 421 |
<td><code>String</code></td> |
| 422 |
<td>The raw message of the logging request. For some logger |
| 423 |
<em>l</em>, when you write l.info("Hello {}", name); where |
| 424 |
name is assigned the value "Alice", then "Hello {}" is the |
| 425 |
message.</td> </tr> |
| 426 |
|
| 427 |
<tr> |
| 428 |
<td>formattedMessage</td> |
| 429 |
<td><code>String</code></td> |
| 430 |
<td>The formatted message in the logging request. For some |
| 431 |
logger <em>l</em>, when you write l.info("Hello {}", name); |
| 432 |
where name is assigned the value "Alice", then "Hello Alice" |
| 433 |
is the formatted message.</td> |
| 434 |
</tr> |
| 435 |
|
| 436 |
<tr class="alt"> |
| 437 |
<td>logger</td> |
| 438 |
<td><code>String</code></td> |
| 439 |
<td>The name of the logger. |
| 440 |
</td> |
| 441 |
</tr> |
| 442 |
|
| 443 |
<tr> |
| 444 |
<td>loggerContext</td> |
| 445 |
<td><a |
| 446 |
href="../xref/ch/qos/logback/classic/spi/LoggerContextVO.html"><code>LoggerContextVO</code></a></td> |
| 447 |
<td>A restricted (value object) view of the logger context to which the logging event belongs to. |
| 448 |
</td> |
| 449 |
</tr> |
| 450 |
|
| 451 |
|
| 452 |
<tr class="alt"> |
| 453 |
<td>level</td> |
| 454 |
<td><code>int</code></td> |
| 455 |
<td>The int value corresponding to the level. To help create |
| 456 |
easily expressions involving levels, the default value |
| 457 |
<em>DEBUG</em>, <em>INFO</em>, <em>WARN</em> and |
| 458 |
<em>ERROR</em> are also available. Thus, using <em>level > |
| 459 |
INFO</em> is a correct expression. |
| 460 |
</td> |
| 461 |
</tr> |
| 462 |
|
| 463 |
<tr> |
| 464 |
<td>timeStamp |
| 465 |
</td> |
| 466 |
<td><code>long</code></td> |
| 467 |
<td>The timestamp corresponding to the logging event's |
| 468 |
creation. |
| 469 |
</td> |
| 470 |
</tr> |
| 471 |
<tr class="alt"> |
| 472 |
<td>marker</td> |
| 473 |
<td><code>Marker</code></td> |
| 474 |
<td>The <code>Marker</code> object associated with the logging |
| 475 |
request. Note that marker can be null and it is your |
| 476 |
responsibility to check for this condition in order to avoid |
| 477 |
<code>NullPointerException</code>. |
| 478 |
</td> |
| 479 |
</tr> |
| 480 |
<tr> |
| 481 |
<td>mdc</td> |
| 482 |
<td><code>Map</code></td> |
| 483 |
<td>A map containing all the MDC values at the time of the |
| 484 |
creation of the logging event. A value can be accessed by |
| 485 |
using the following expression: <em>mdc.get("myKey")</em>. As |
| 486 |
of logback-classic version 0.9.30, the 'mdc' variable will |
| 487 |
never be null. |
| 488 |
|
| 489 |
<p>The <code>java.util.Map</code> type is non-parameterized |
| 490 |
because Janino does not support generics. It follows that the |
| 491 |
type returned by <code>mdc.get()</code> is <code>Object</code> |
| 492 |
and not <code>String</code>. To invoke <code>String</code> |
| 493 |
methods on the returned value, it must be cast as |
| 494 |
<code>String</code>. For example, |
| 495 |
<code>((String) mdc.get("k")).contains("val")</code>. |
| 496 |
</p> |
| 497 |
</td> |
| 498 |
</tr> |
| 499 |
|
| 500 |
<tr class="alt"> |
| 501 |
<td>throwable</td> |
| 502 |
<td>java.lang.Throwable</td> |
| 503 |
<td>If no exception is associated with the event, then the |
| 504 |
value of the "throwable" variable will be null. Unfortunately, |
| 505 |
"throwable" does not survive serialization. Thus, on remote |
| 506 |
systems, its value will always be null. For location |
| 507 |
independent expressions, use the <code>throwableProxy</code> |
| 508 |
variable described next. |
| 509 |
</td> |
| 510 |
</tr> |
| 511 |
|
| 512 |
<tr> |
| 513 |
<td>throwableProxy</td> |
| 514 |
<td><a |
| 515 |
href="../xref/ch/qos/logback/classic/spi/IThrowableProxy.html"><code>IThrowableProxy</code></a></td> |
| 516 |
<td>A proxy for the exception associated with the logging |
| 517 |
event. If no exception is associated with the event, then the |
| 518 |
value of the "throwableProxy" variable will be null. In |
| 519 |
contrast to "throwable", when an exception is associated with |
| 520 |
an event, the value of "throwableProxy" will be non-null even |
| 521 |
on remote systems, that is even after serialization. |
| 522 |
</td> |
| 523 |
</tr> |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
</table> |
| 528 |
|
| 529 |
<p>Here is a concrete example.</p> |
| 530 |
|
| 531 |
<em>Example: Basic event evaluator usage |
| 532 |
(logback-examples/src/main/resources/chapters/filters/basicEventEvaluator.xml)</em> |
| 533 |
|
| 534 |
<span class="asGroovy" onclick="return asGroovy('basicEventEvaluator');">View as .groovy</span> |
| 535 |
<pre id="basicEventEvaluator" class="prettyprint source longline"><configuration> |
| 536 |
|
| 537 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 538 |
<b><filter class="ch.qos.logback.core.filter.EvaluatorFilter"> |
| 539 |
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator --> |
| 540 |
<expression><span class="green">return message.contains("billing");</span></expression> |
| 541 |
</evaluator> |
| 542 |
<OnMismatch>NEUTRAL</OnMismatch> |
| 543 |
<OnMatch>DENY</OnMatch> |
| 544 |
</filter></b> |
| 545 |
<encoder> |
| 546 |
<pattern> |
| 547 |
%-4relative [%thread] %-5level %logger - %msg%n |
| 548 |
</pattern> |
| 549 |
</encoder> |
| 550 |
</appender> |
| 551 |
|
| 552 |
<root level="INFO"> |
| 553 |
<appender-ref ref="STDOUT" /> |
| 554 |
</root> |
| 555 |
</configuration></pre> |
| 556 |
|
| 557 |
<p>The bold part in the above configuration file adds an |
| 558 |
<code>EvaluatorFilter</code> to a <code>ConsoleAppender</code>. An |
| 559 |
evaluator of type <code>JaninoEventEvaluator</code> is then |
| 560 |
injected into the <code>EvaluatorFilter</code>. In the absence of |
| 561 |
<span class="attr">class</span> attribute in the |
| 562 |
<code><evaluator></code> element specified by the user, Joran |
| 563 |
will infer a default type of <code>JaninoEventEvaluator</code> |
| 564 |
for the evaluator. This is one of the <a |
| 565 |
href="onJoran.html#defaultClassMapping">few occurrences</a> where |
| 566 |
Joran implicitly infers the type of a component. |
| 567 |
</p> |
| 568 |
|
| 569 |
<p>The <em>expression</em> element corresponds to the evaluation |
| 570 |
expression just discussed. The expression |
| 571 |
<code>return message.contains("billing");</code> returns a boolean |
| 572 |
value. Notice that the <em>message</em> variable is exported |
| 573 |
automatically by <code>JaninoEventEvaluator</code>. |
| 574 |
</p> |
| 575 |
|
| 576 |
<p>Given that the <span class="option">OnMismatch</span> property is |
| 577 |
set to NEUTRAL and the <span class="option">OnMatch</span> |
| 578 |
property set to DENY, this evaluator filter will drop all logging |
| 579 |
events whose message contains the string "billing". |
| 580 |
</p> |
| 581 |
|
| 582 |
<p>The <a |
| 583 |
href="../xref/chapters/filters/FilterEvents.html"><code>FilterEvents</code></a> |
| 584 |
application issues ten logging requests, numbered 0 to 9. Let us |
| 585 |
first run <code>FilterEvents</code> class without any filters: |
| 586 |
</p> |
| 587 |
|
| 588 |
<div class="source"><pre> |
| 589 |
java chapters.filters.FilterEvents src/main/java/chapters/filters/basicConfiguration.xml |
| 590 |
</pre></div> |
| 591 |
|
| 592 |
<p>All requests will be displayed, as shown below:</p> |
| 593 |
|
| 594 |
<div class="source"><pre>0 [main] INFO chapters.filters.FilterEvents - logging statement 0 |
| 595 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 1 |
| 596 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 2 |
| 597 |
0 [main] DEBUG chapters.filters.FilterEvents - logging statement 3 |
| 598 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 4 |
| 599 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 5 |
| 600 |
0 [main] ERROR chapters.filters.FilterEvents - <b>billing statement 6</b> |
| 601 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 7 |
| 602 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 8 |
| 603 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 9</pre></div> |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
<p>Suppose that we want to get rid of the "billing statement". |
| 608 |
The <em>basicEventEvaluator.xml</em> configuration file listed |
| 609 |
above filters messages containing the string "billing" which is |
| 610 |
precisely the desired outcome.</p> |
| 611 |
|
| 612 |
<p>Running with <em>basicEventEvaluator.xml</em>:</p> |
| 613 |
<p class="source">java chapters.filters.FilterEvents src/main/java/chapters/filters/basicEventEvaluator.xml</p> |
| 614 |
<p>we obtain: |
| 615 |
</p> |
| 616 |
|
| 617 |
<p class="source">0 [main] INFO chapters.filters.FilterEvents - logging statement 0 |
| 618 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 1 |
| 619 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 2 |
| 620 |
0 [main] DEBUG chapters.filters.FilterEvents - logging statement 3 |
| 621 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 4 |
| 622 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 5 |
| 623 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 7 |
| 624 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 8 |
| 625 |
0 [main] INFO chapters.filters.FilterEvents - logging statement 9</p> |
| 626 |
|
| 627 |
|
| 628 |
<p>Evaluation expressions can be Java blocks. For example, the |
| 629 |
following is a valid expression.</p> |
| 630 |
|
| 631 |
<pre class="prettyprint source"><evaluator> |
| 632 |
<expression> |
| 633 |
if(logger.startsWith("org.apache.http")) |
| 634 |
return true; |
| 635 |
|
| 636 |
if(mdc == null || mdc.get("entity") == null) |
| 637 |
return false; |
| 638 |
|
| 639 |
String payee = (String) mdc.get("entity"); |
| 640 |
|
| 641 |
if(logger.equals("org.apache.http.wire") &amp;&amp; <!-- & encoded as &amp; --> |
| 642 |
payee.contains("someSpecialValue") &amp;&amp; |
| 643 |
!message.contains("someSecret")) { |
| 644 |
return true; |
| 645 |
} |
| 646 |
|
| 647 |
return false; |
| 648 |
</expression> |
| 649 |
</evaluator></pre> |
| 650 |
|
| 651 |
|
| 652 |
<h2 class="doAnchor" name="matcher">Matchers</h2> |
| 653 |
|
| 654 |
<p>While it is possible to do pattern matching by invoking the <a |
| 655 |
href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#matches%28java.lang.String%29">matches()</a> |
| 656 |
method in the <code>String</code> class, this incurs the cost of |
| 657 |
compiling of a brand new <code>Pattern</code> object each time the |
| 658 |
filter is invoked. To eliminate this overhead, you can predefine |
| 659 |
one or more <a |
| 660 |
href="../xref/ch/qos/logback/core/boolex/Matcher.html">Matcher</a> |
| 661 |
objects. Once a matcher is defined, it can be repeatedly |
| 662 |
referenced by name in the evaluator expression.</p> |
| 663 |
|
| 664 |
<p>An example should clarify the point:</p> |
| 665 |
|
| 666 |
<em>Example: Defining matchers in an event evaluator (logback-examples/src/main/resources/chapters/filters/evaluatorWithMatcher.xml)</em> |
| 667 |
<span class="asGroovy" onclick="return asGroovy('evaluatorWithMatcher');">View as .groovy</span> |
| 668 |
|
| 669 |
<pre id="evaluatorWithMatcher" class="prettyprint source"><configuration debug="true"> |
| 670 |
|
| 671 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 672 |
<filter class="ch.qos.logback.core.filter.EvaluatorFilter"> |
| 673 |
<evaluator> |
| 674 |
<b><matcher> |
| 675 |
<Name>odd</Name> |
| 676 |
<!-- filter out odd numbered statements --> |
| 677 |
<regex>statement [13579]</regex> |
| 678 |
</matcher> |
| 679 |
|
| 680 |
<expression>odd.matches(formattedMessage)</expression></b> |
| 681 |
</evaluator> |
| 682 |
<OnMismatch>NEUTRAL</OnMismatch> |
| 683 |
<OnMatch>DENY</OnMatch> |
| 684 |
</filter> |
| 685 |
<encoder> |
| 686 |
<pattern>%-4relative [%thread] %-5level %logger - %msg%n</pattern> |
| 687 |
</encoder> |
| 688 |
</appender> |
| 689 |
|
| 690 |
<root level="DEBUG"> |
| 691 |
<appender-ref ref="STDOUT" /> |
| 692 |
</root> |
| 693 |
</configuration></pre> |
| 694 |
|
| 695 |
<p>Running with <em>evaluatorWithMatcher.xml</em>:</p> |
| 696 |
<p class="source">java chapters.filters.FilterEvents src/main/java/chapters/filters/evaluatorWithMatcher.xml</p> |
| 697 |
<p>we obtain: |
| 698 |
</p> |
| 699 |
|
| 700 |
<p class="source">260 [main] INFO chapters.filters.FilterEvents - logging statement 0 |
| 701 |
264 [main] INFO chapters.filters.FilterEvents - logging statement 2 |
| 702 |
264 [main] INFO chapters.filters.FilterEvents - logging statement 4 |
| 703 |
266 [main] ERROR chapters.filters.FilterEvents - billing statement 6 |
| 704 |
266 [main] INFO chapters.filters.FilterEvents - logging statement 8</p> |
| 705 |
|
| 706 |
<p>If you need to define additional matchers, you can do so by |
| 707 |
adding further <code><matcher></code> elements.</p> |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
<!-- ================================================================ --> |
| 714 |
<!-- ===================== TURBO FILTER ============================= --> |
| 715 |
<!-- ================================================================ --> |
| 716 |
|
| 717 |
<h2 class="doAnchor" name="TurboFilter">TurboFilters</h2> |
| 718 |
|
| 719 |
<p><code>TurboFilter</code> objects all extend the |
| 720 |
<a href="../xref/ch/qos/logback/classic/turbo/TurboFilter.html"> |
| 721 |
<code>TurboFilter</code></a> abstract class. Like the regular |
| 722 |
filters, they use ternary logic to return their evaluation of |
| 723 |
the logging event. |
| 724 |
</p> |
| 725 |
|
| 726 |
<p>Overall, they work much like the previously mentioned |
| 727 |
filters. However, there are two main differences between |
| 728 |
<code>Filter</code> and <code>TurboFilter</code> objects. |
| 729 |
</p> |
| 730 |
|
| 731 |
<p><code>TurboFilter</code> objects are tied to the logging |
| 732 |
context. Hence, they are called not only when a given appender is |
| 733 |
used, but each and every time a logging request is issued. Their |
| 734 |
scope is wider than appender-attached filters. |
| 735 |
</p> |
| 736 |
|
| 737 |
<p>More importantly, they are called before the |
| 738 |
<code>LoggingEvent</code> object creation. |
| 739 |
<code>TurboFilter</code> objects do not require the instantiation |
| 740 |
of a logging event to filter a logging request. As such, turbo |
| 741 |
filters are intended for high performance filtering of logging |
| 742 |
events, even before the events are created. |
| 743 |
</p> |
| 744 |
|
| 745 |
|
| 746 |
<h3 class="doAnchor" name="yourOwnTurboFilter">Implementing your |
| 747 |
own TurboFilter</h3> |
| 748 |
|
| 749 |
<p>To create your own <code>TurboFilter</code> component, just |
| 750 |
extend the <code>TurboFilter</code> abstract class. As previously, |
| 751 |
when implementing a customized filter object, developing a custom |
| 752 |
<code>TurboFilter</code> only asks that one implement the |
| 753 |
<code>decide()</code> method. In the next example, we create a |
| 754 |
slightly more complex filter: |
| 755 |
</p> |
| 756 |
|
| 757 |
<em>Example: Basic custom <code>TurboFilter</code> (<a href="../xref/chapters/filters/SampleTurboFilter.html">logback-examples/src/main/java/chapters/filters/SampleTurboFilter.java</a>)</em> |
| 758 |
<pre class="prettyprint source">package chapters.filters; |
| 759 |
|
| 760 |
import org.slf4j.Marker; |
| 761 |
import org.slf4j.MarkerFactory; |
| 762 |
|
| 763 |
import ch.qos.logback.classic.Level; |
| 764 |
import ch.qos.logback.classic.Logger; |
| 765 |
import ch.qos.logback.classic.turbo.TurboFilter; |
| 766 |
import ch.qos.logback.core.spi.FilterReply; |
| 767 |
|
| 768 |
public class SampleTurboFilter extends TurboFilter { |
| 769 |
|
| 770 |
String marker; |
| 771 |
Marker markerToAccept; |
| 772 |
|
| 773 |
@Override |
| 774 |
public FilterReply decide(Marker marker, Logger logger, Level level, |
| 775 |
String format, Object[] params, Throwable t) { |
| 776 |
|
| 777 |
if (!isStarted()) { |
| 778 |
return FilterReply.NEUTRAL; |
| 779 |
} |
| 780 |
|
| 781 |
if ((markerToAccept.equals(marker))) { |
| 782 |
return FilterReply.ACCEPT; |
| 783 |
} else { |
| 784 |
return FilterReply.NEUTRAL; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
public String getMarker() { |
| 789 |
return marker; |
| 790 |
} |
| 791 |
|
| 792 |
public void setMarker(String markerStr) { |
| 793 |
this.marker = markerStr; |
| 794 |
} |
| 795 |
|
| 796 |
@Override |
| 797 |
public void start() { |
| 798 |
if (marker != null && marker.trim().length() > 0) { |
| 799 |
markerToAccept = MarkerFactory.getMarker(marker); |
| 800 |
super.start(); |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
</pre> |
| 805 |
|
| 806 |
<p>The <code>TurboFilter</code> above accepts events that contain |
| 807 |
a specific marker. If said marker is not found, then the filter |
| 808 |
passes the responsibility to the next filter in the chain. |
| 809 |
</p> |
| 810 |
|
| 811 |
<p>To allow more flexibility, the marker that will be tested can |
| 812 |
be specified in the configuration file, hence the getter and |
| 813 |
setter methods. We also implemented the <code>start()</code> |
| 814 |
method, to check that the option has been specified during the |
| 815 |
configuration process. |
| 816 |
</p> |
| 817 |
|
| 818 |
<p>Here is a sample configuration that makes use of our newly |
| 819 |
created <code>TurboFilter</code>. |
| 820 |
</p> |
| 821 |
|
| 822 |
<em>Example: Basic custom <code>TurboFilter</code> configuration |
| 823 |
(logback-examples/src/main/resources/chapters/filters/sampleTurboFilterConfig.xml)</em> |
| 824 |
|
| 825 |
<span class="asGroovy" onclick="return asGroovy('sampleTurboFilterConfig');">View as .groovy</span> |
| 826 |
|
| 827 |
<pre id="sampleTurboFilterConfig" class="prettyprint source"><configuration> |
| 828 |
<b><turboFilter class="chapters.filters.SampleTurboFilter"> |
| 829 |
<Marker>sample</Marker> |
| 830 |
</turboFilter></b> |
| 831 |
|
| 832 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 833 |
<encoder> |
| 834 |
<pattern> |
| 835 |
%-4relative [%thread] %-5level %logger - %msg%n |
| 836 |
</pattern> |
| 837 |
</encoder> |
| 838 |
</appender> |
| 839 |
|
| 840 |
<root> |
| 841 |
<appender-ref ref="STDOUT" /> |
| 842 |
</root> |
| 843 |
</configuration></pre> |
| 844 |
|
| 845 |
<p>Logback classic ships with several <code>TurboFilter</code> |
| 846 |
classes ready for use. The <a |
| 847 |
href="../xref/ch/qos/logback/classic/turbo/MDCFilter.html"><code>MDCFilter</code></a> |
| 848 |
checks the presence of a given value in the MDC whereas <a |
| 849 |
href="../apidocs/ch/qos/logback/classic/turbo/DynamicThresholdFilter.html"><code>DynamicThresholdFilter</code></a> |
| 850 |
allows filtering based on MDC key/level threshold associations. On |
| 851 |
the other hand, <a |
| 852 |
href="../xref/ch/qos/logback/classic/turbo/MarkerFilter.html"><code>MarkerFilter</code></a> |
| 853 |
checks for the presence of a specific marker associated with the |
| 854 |
logging request. |
| 855 |
</p> |
| 856 |
|
| 857 |
<p>Here is a sample configuration, using both |
| 858 |
<code>MDCFilter</code> and <code>MarkerFilter</code>. |
| 859 |
</p> |
| 860 |
|
| 861 |
<em>Example: <code>MDCFilter</code> and <code>MarkerFilter</code> |
| 862 |
configuration |
| 863 |
(logback-examples/src/main/resources/chapters/filters/turboFilters.xml)</em> |
| 864 |
|
| 865 |
<span class="asGroovy" onclick="return asGroovy('turboFilters');">View as .groovy</span> |
| 866 |
<pre id="turboFilters" class="prettyprint source"><configuration> |
| 867 |
|
| 868 |
<turboFilter class="ch.qos.logback.classic.turbo.MDCFilter"> |
| 869 |
<MDCKey>username</MDCKey> |
| 870 |
<Value>sebastien</Value> |
| 871 |
<OnMatch>ACCEPT</OnMatch> |
| 872 |
</turboFilter> |
| 873 |
|
| 874 |
<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter"> |
| 875 |
<Marker>billing</Marker> |
| 876 |
<OnMatch>DENY</OnMatch> |
| 877 |
</turboFilter> |
| 878 |
|
| 879 |
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> |
| 880 |
<encoder> |
| 881 |
<pattern>%date [%thread] %-5level %logger - %msg%n</pattern> |
| 882 |
</encoder> |
| 883 |
</appender> |
| 884 |
|
| 885 |
<root level="INFO"> |
| 886 |
<appender-ref ref="console" /> |
| 887 |
</root> |
| 888 |
</configuration></pre> |
| 889 |
|
| 890 |
<p>You can see this configuration in action by issuing the |
| 891 |
following command: |
| 892 |
</p> |
| 893 |
|
| 894 |
<p class="source">java chapters.filters.FilterEvents src/main/java/chapters/filters/turboFilters.xml</p> |
| 895 |
|
| 896 |
<p>As we've seen previously, the <a |
| 897 |
href="../xref/chapters/filters/FilterEvents.html"><code>FilterEvents</code></a> |
| 898 |
application issues 10 logging requests, numbered 0 to 9. Except |
| 899 |
for requests 3 and 6, all of the requests are of level |
| 900 |
<em>INFO</em>, the same level as the one assigned to the root |
| 901 |
logger. The 3rd request, is issued at the the <em>DEBUG</em> |
| 902 |
level, which is below the effective level. However, since the MDC |
| 903 |
key "username" is set to "sebastien" just before the 3rd request |
| 904 |
and removed just afterwards, the <code>MDCFilter</code> |
| 905 |
specifically accepts the request (and only that request). The 6th |
| 906 |
request, issued at the <em>ERROR</em> level, is marked as |
| 907 |
"billing". As such, it is denied by the MarkerFilter (the second |
| 908 |
turbo filter in the configuration). |
| 909 |
</p> |
| 910 |
|
| 911 |
<p>Thus, the output of <code>FilterEvents</code> application |
| 912 |
configured with <em>turboFilters.xml</em> file shown above is: |
| 913 |
</p> |
| 914 |
|
| 915 |
<p class="source">2006-12-04 15:17:22,859 [main] INFO chapters.filters.FilterEvents - logging statement 0 |
| 916 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 1 |
| 917 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 2 |
| 918 |
2006-12-04 15:17:22,875 [main] DEBUG chapters.filters.FilterEvents - logging statement 3 |
| 919 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 4 |
| 920 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 5 |
| 921 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 7 |
| 922 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 8 |
| 923 |
2006-12-04 15:17:22,875 [main] INFO chapters.filters.FilterEvents - logging statement 9</p> |
| 924 |
|
| 925 |
|
| 926 |
<p>One can see that the 3rd request, which should not be displayed |
| 927 |
if we only followed the overall <em>INFO</em> level, appears |
| 928 |
anyway, because it matched the first <code>TurboFilter</code> |
| 929 |
requirements and was accepted. |
| 930 |
</p> |
| 931 |
|
| 932 |
<p>On the other hand, the 6th request, that is an <em>ERROR</em> |
| 933 |
level request should have been displayed. But it satisfied the |
| 934 |
second <code>TurboFilter</code> whose <span |
| 935 |
class="option">OnMatch</span> option is set to <em>DENY</em>. |
| 936 |
Thus, the 6th request was not displayed. |
| 937 |
</p> |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
<h3 class="doAnchor" |
| 943 |
name="DuplicateMessageFilter">DuplicateMessageFilter</h3> |
| 944 |
|
| 945 |
<p>The <code>DuplicateMessageFilter</code> merits a separate |
| 946 |
presentation. This filter detects duplicate messages, and beyond |
| 947 |
a certain number of repetitions, drops repeated messages. |
| 948 |
</p> |
| 949 |
|
| 950 |
<p>To detect repetition, this filter uses simple String equality |
| 951 |
between messages. It does not detect messages which are very |
| 952 |
similar, varying only by few characters. For example, if you |
| 953 |
write: |
| 954 |
</p> |
| 955 |
|
| 956 |
<pre class="prettyprint source">logger.debug("Hello "+name0); |
| 957 |
logger.debug("Hello "+name1);</pre> |
| 958 |
|
| 959 |
<p>Assuming <code>name0</code> and <code>name1</code> have |
| 960 |
different values, the two "Hello" messages will be considered as |
| 961 |
unrelated. Depending on user demand, future releases may check for |
| 962 |
string similarity, eliminating repetitions of similar but not |
| 963 |
identical messages. |
| 964 |
</p> |
| 965 |
|
| 966 |
<p>Note that in case of parameterized logging, only the raw |
| 967 |
message is taken into consideration. For example, in the next two |
| 968 |
requests, the raw messages, i.e. "Hello {}.", are identical, and |
| 969 |
thus considered as repetitions. |
| 970 |
</p> |
| 971 |
|
| 972 |
<pre class="prettyprint source">logger.debug("Hello {}.", name0); |
| 973 |
logger.debug("Hello {}.", name1);</pre> |
| 974 |
|
| 975 |
<p>The number of allowed repetitions can be specified by the <span |
| 976 |
class="option">AllowedRepetitions</span> property. For example, if |
| 977 |
the property is set to 1, then the 2nd and subsequent |
| 978 |
occurrences of the same message will be dropped. Similarly, if the |
| 979 |
property is set to 2, then the 3rd and subsequent occurrences |
| 980 |
of the same message will be dropped. By default, the <span |
| 981 |
class="option">AllowedRepetitions</span> property is set to 5. |
| 982 |
</p> |
| 983 |
|
| 984 |
<p>In order to detect repetitions, this filter needs to keep |
| 985 |
references to old messages in an internal cache. The size of this |
| 986 |
cache is determined by the <span class="option">CacheSize</span> |
| 987 |
property. By the default, this is set to 100. |
| 988 |
</p> |
| 989 |
|
| 990 |
|
| 991 |
<em>Example: <code>DuplicateMessageFilter</code> |
| 992 |
configuration (logback-examples/src/main/resources/chapters/filters/duplicateMessage.xml)</em> |
| 993 |
|
| 994 |
<span class="asGroovy" onclick="return asGroovy('duplicateMessage');">View as .groovy</span> |
| 995 |
<pre id="duplicateMessage" class="prettyprint source"><configuration> |
| 996 |
|
| 997 |
<b><turboFilter class="ch.qos.logback.classic.turbo.DuplicateMessageFilter"/></b> |
| 998 |
|
| 999 |
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> |
| 1000 |
<encoder> |
| 1001 |
<pattern>%date [%thread] %-5level %logger - %msg%n</pattern> |
| 1002 |
</encoder> |
| 1003 |
</appender> |
| 1004 |
|
| 1005 |
<root level="INFO"> |
| 1006 |
<appender-ref ref="console" /> |
| 1007 |
</root> |
| 1008 |
</configuration></pre> |
| 1009 |
|
| 1010 |
<p>Thus, the output for <code>FilterEvents</code> application |
| 1011 |
configured with <em>duplicateMessage.xml</em> is: |
| 1012 |
</p> |
| 1013 |
|
| 1014 |
<p class="source">2008-12-19 15:04:26,156 [main] INFO chapters.filters.FilterEvents - logging statement 0 |
| 1015 |
2008-12-19 15:04:26,156 [main] INFO chapters.filters.FilterEvents - logging statement 1 |
| 1016 |
2008-12-19 15:04:26,156 [main] INFO chapters.filters.FilterEvents - logging statement 2 |
| 1017 |
2008-12-19 15:04:26,156 [main] INFO chapters.filters.FilterEvents - logging statement 4 |
| 1018 |
2008-12-19 15:04:26,156 [main] INFO chapters.filters.FilterEvents - logging statement 5 |
| 1019 |
2008-12-19 15:04:26,171 [main] ERROR chapters.filters.FilterEvents - billing statement 6</p> |
| 1020 |
|
| 1021 |
<p>"logging statement 0" is the first <em>occurrence</em> of the |
| 1022 |
message "logging statement {}". "logging statement 1" is the first |
| 1023 |
<em>repetition</em>, "logging statement 2" is the second |
| 1024 |
repetition. Interestingly enough, "logging statement 3" of level |
| 1025 |
DEBUG, is the <em>third</em> repetition, even though it is later |
| 1026 |
dropped by virtue of the <a |
| 1027 |
href="architecture.html#basic_selection">basic selection |
| 1028 |
rule</a>. This can be explained by the fact that turbo filters are |
| 1029 |
invoked before other types of filters, including the basic |
| 1030 |
selection rule. Thus, <code>DuplicateMessageFilter</code> |
| 1031 |
considers "logging statement 3" as a repetition, oblivious to the |
| 1032 |
fact that it will be dropped further down in the processing |
| 1033 |
chain. "logging statement 4" is the fourth repetition and "logging |
| 1034 |
statement 5" the fifth. Statements 6 and beyond are dropped |
| 1035 |
because only 5 repetitions are allowed by default. |
| 1036 |
</p> |
| 1037 |
|
| 1038 |
<h1 class="doAnchor" name="logbac-access">In logback-access</h1> |
| 1039 |
|
| 1040 |
<p>Logback-access offers most of the features available with |
| 1041 |
logback-classic. In particular, <code>Filter</code> objects are |
| 1042 |
available and work in the same way as their logback-classic |
| 1043 |
counterparts, with one notable difference. Instead of |
| 1044 |
<code>LoggingEvent</code> instances logback-access filters act |
| 1045 |
upon <a |
| 1046 |
href="../xref/ch/qos/logback/access/spi/AccessEvent.html"><code>AccessEvent</code></a> |
| 1047 |
instances. At present time, logback-access ships with a limited |
| 1048 |
number of filters described below. If you would like to suggest |
| 1049 |
additional filters, please contact the logback-dev mailing list. |
| 1050 |
</p> |
| 1051 |
|
| 1052 |
<h2 class="doAnchor" |
| 1053 |
name="countingFilter"><code>CountingFilter</code></h2> |
| 1054 |
|
| 1055 |
<p>With the help of <a |
| 1056 |
href="xref/ch/qos/logback/access/filter/CountingFilter.html"><code>CountingFilter</code></a> |
| 1057 |
class, logback-access can provide statistical data about access to |
| 1058 |
the web-server. Upon initialization, <code>CountingFilter</code> |
| 1059 |
registers itself as an MBean onto the platform's JMX server. You |
| 1060 |
can then interrogate that MBean for statistical data, |
| 1061 |
e.g. averages by minute, hour, day, week, or month. Other |
| 1062 |
statistics such the count for the preceding week, day, hour or |
| 1063 |
month as well as the total count are also available. |
| 1064 |
</p> |
| 1065 |
|
| 1066 |
<p>The following <em>logback-access.xml</em> configuration file |
| 1067 |
declares a <code>CountingFilter</code>. </p> |
| 1068 |
|
| 1069 |
<pre class="prettyprint source"><configuration> |
| 1070 |
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> |
| 1071 |
|
| 1072 |
<b><filter class="ch.qos.logback.access.filter.CountingFilter"> |
| 1073 |
<name>countingFilter</name> |
| 1074 |
</filter></b> |
| 1075 |
|
| 1076 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 1077 |
<encoder> |
| 1078 |
<pattern>%h %l %u %t \"%r\" %s %b</pattern> |
| 1079 |
</encoder> |
| 1080 |
</appender> |
| 1081 |
|
| 1082 |
<appender-ref ref="STDOUT" /> |
| 1083 |
</configuration></pre> |
| 1084 |
|
| 1085 |
<p>You can examine the various statistics maintained by |
| 1086 |
<code>CountingFilter</code> on your platform's JMX server via the |
| 1087 |
<code>jconsole</code> application. </p> |
| 1088 |
|
| 1089 |
|
| 1090 |
<img alt="CountingFilter via jconsole" src="images/chapters/filters/countingFilter.png" /> |
| 1091 |
|
| 1092 |
|
| 1093 |
<h3 class="doAnchor" name="access_EvalutorFilter">EvaluatorFilter</h3> |
| 1094 |
|
| 1095 |
|
| 1096 |
<p><a |
| 1097 |
href="../xref/ch/qos/logback/core/filter/EvaluatorFilter.html"><code>EvaluatorFilter</code></a> |
| 1098 |
is a generic filter encapsulating an |
| 1099 |
<code>EventEvaluator</code>. As the name suggests, an |
| 1100 |
<a |
| 1101 |
href="../xref/ch/qos/logback/core/boolex/EventEvaluator.html"> |
| 1102 |
<code>EventEvaluator</code></a> evaluates whether a given criteria |
| 1103 |
is met for a given event. On match and on mismatch, |
| 1104 |
the hosting <code>EvaluatorFilter</code> will return the value |
| 1105 |
specified by the <span class="option">onMatch</span> or |
| 1106 |
<span class="option">onMismatch</span> properties respectively. Note that |
| 1107 |
<code>EvaluatorFilter</code> has been previously discussed in the |
| 1108 |
context of logback-classic (<a href="#evalutatorFilter">see |
| 1109 |
above</a>). The present text is mostly a repetition of the |
| 1110 |
previous discussion.</p> |
| 1111 |
|
| 1112 |
|
| 1113 |
<p>Note that <code>EventEvaluator</code> is an abstract class. You |
| 1114 |
can implement your own event evaluation logic by sub-classing |
| 1115 |
<code>EventEvaluator</code>. Logback-access ships with a concrete |
| 1116 |
implementation named <a |
| 1117 |
href="../xref/ch/qos/logback/access/boolex/JaninoEventEvaluator.html">JaninoEventEvaluator</a>. |
| 1118 |
It takes arbitrary Java language boolean expressions as the |
| 1119 |
evaluation criteria. We refer to such Java language blocks as |
| 1120 |
"<em>evaluation expressions</em>". Evaluation expressions enable |
| 1121 |
great flexibility in event |
| 1122 |
filtering. <code>JaninoEventEvaluator</code> requires the <a |
| 1123 |
href="http://docs.codehaus.org/display/JANINO/Home">Janino |
| 1124 |
library</a>. Please see the <a |
| 1125 |
href="../setup.html#janino">corresponding section</a> of the setup |
| 1126 |
document. |
| 1127 |
</p> |
| 1128 |
|
| 1129 |
<p>Evaluation expressions are compiled on-the-fly during the |
| 1130 |
interpretation of the configuration file. As a user, you do not |
| 1131 |
need to worry about the actual plumbing. However, it is your |
| 1132 |
responsibility to ensure that the Java language expression returns |
| 1133 |
a boolean, i.e. that it evaluates to true or false. </p> |
| 1134 |
|
| 1135 |
|
| 1136 |
<p>The evaluation expression is evaluated on the current access |
| 1137 |
event. Logback-access automatically exports the current |
| 1138 |
<code>AccessEvent</code> instance under the variable name |
| 1139 |
<b><code>event</code></b>. You can read the various data |
| 1140 |
associated with the HTTP request as well as the HTTP response via |
| 1141 |
the <code>event</code> variable. Please refer to the <a |
| 1142 |
href="../xref/ch/qos/logback/access/spi/AccessEvent.html"><code>AccessEvent</code> |
| 1143 |
class source code</a> for the exact list. |
| 1144 |
</p> |
| 1145 |
|
| 1146 |
<p>The next logback-access configuration file illustrates |
| 1147 |
filtering based on the <a |
| 1148 |
href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">404 |
| 1149 |
(Not Found)</a> HTTP response code. Every request resulting in a |
| 1150 |
404 will be printed on the console.</p> |
| 1151 |
|
| 1152 |
<em>Example: Access Evaluator (logback-examples/src/main/resources/chapters/filters/accessEventEvaluator.xml)</em> |
| 1153 |
<pre class="prettyprint source"><configuration> |
| 1154 |
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> |
| 1155 |
|
| 1156 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 1157 |
<b><filter class="ch.qos.logback.core.filter.EvaluatorFilter"> |
| 1158 |
<evaluator> |
| 1159 |
<expression>event.getStatusCode() == 404</expression> |
| 1160 |
</evaluator> |
| 1161 |
<onMismatch>DENY</onMismatch> |
| 1162 |
</filter></b> |
| 1163 |
<encoder><pattern>%h %l %u %t %r %s %b</pattern></encoder> |
| 1164 |
</appender> |
| 1165 |
|
| 1166 |
<appender-ref ref="STDOUT" /> |
| 1167 |
</configuration></pre> |
| 1168 |
|
| 1169 |
<p>In the next example, we still log requests resulting in 404 errors, |
| 1170 |
except those requests asking for CSS files. |
| 1171 |
</p> |
| 1172 |
|
| 1173 |
|
| 1174 |
<em>Example 6.10: Access Evaluator (logback-examples/src/main/resources/chapters/filters/accessEventEvaluator2.xml)</em> |
| 1175 |
<pre class="prettyprint source"><configuration> |
| 1176 |
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> |
| 1177 |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
| 1178 |
<filter class="ch.qos.logback.core.filter.EvaluatorFilter"> |
| 1179 |
<evaluator name="Eval404"> |
| 1180 |
<expression> |
| 1181 |
<b>(event.getStatusCode() == 404)</b> |
| 1182 |
<b>&amp;&amp;</b> <!-- ampersand characters need to be escaped --> |
| 1183 |
<b>!(event.getRequestURI().contains(".css"))</b> |
| 1184 |
</expression> |
| 1185 |
</evaluator> |
| 1186 |
<onMismatch>DENY</onMismatch> |
| 1187 |
</filter> |
| 1188 |
|
| 1189 |
<encoder><pattern>%h %l %u %t %r %s %b</pattern></encoder> |
| 1190 |
</appender> |
| 1191 |
|
| 1192 |
<appender-ref ref="STDOUT" /> |
| 1193 |
</configuration> |
| 1194 |
</pre> |
| 1195 |
|
| 1196 |
<script src="../templates/footer.js" type="text/javascript"></script> |
| 1197 |
|
| 1198 |
</div> |
| 1199 |
</body> |
| 1200 |
</html> |