/usr/share/doc/liblogback-java/manual/architecture.html is in liblogback-java-doc 1:1.1.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Chapter 2: Architecture</title>
<link rel="stylesheet" type="text/css" href="../css/common.css" />
<link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" />
<link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" />
</head>
<body onload="prettyPrint(); decorate();">
<script type="text/javascript">prefix='../';</script>
<script type="text/javascript" src="../js/prettify.js"></script>
<script type="text/javascript" src="../templates/header.js"></script>
<script type="text/javascript" src="../js/jquery-min.js"></script>
<script type="text/javascript" src="../js/decorator.js"></script>
<div id="left">
<noscript>Please turn on Javascript to view this menu</noscript>
<script src="../templates/left.js" type="text/javascript"></script>
</div>
<div id="right">
<script src="menu.js" type="text/javascript"></script>
</div>
<div id="content">
<h1>Chapter 2: Architecture</h1>
<div class="quote">
<p><em>All true classification is genealogical.</em></p>
<p>—CHARLES DARWIN, <em>The Origin of Species</em></p>
<p><em>It is difficult, if not impossible, for anyone to learn a
subject purely by reading about it, without applying the
information to specific problems and thereby forcing himself to
think about what has been read. Furthermore, we all learn best
the things that we have discovered ourselves.</em>
</p>
<p>—DONALD KNUTH, <em>The Art of Computer Programming</em></p>
</div>
<script src="../templates/creative.js" type="text/javascript"></script>
<h2>Logback's architecture</h2>
<p>Logback's basic architecture is sufficiently generic so as to
apply under different circumstances. At the present time, logback
is divided into three modules, logback-core, logback-classic and
logback-access.
</p>
<p>The <em>core</em> module lays the groundwork for the other two
modules. The <em>classic</em> module extends <em>core</em>. The
classic module corresponds to a significantly improved version of
log4j. Logback-classic natively implements the <a
href="http://www.slf4j.org">SLF4J API</a> so that you can readily
switch back and forth between logback and other logging systems
such as log4j or java.util.logging (JUL) introduced in JDK
1.4. The third module called <em>access</em> integrates with
Servlet containers to provide HTTP-access log functionality. A
separate document covers <a href="../access.html">access module
documentation</a>.
</p>
<p>In the remainder of this document, we will write "logback" to
refer to the logback-classic module.
</p>
<h2>Logger, Appenders and Layouts</h2>
<p>Logback is built upon three main classes: <code>Logger</code>,
<code>Appender</code> and <code>Layout</code>. These three types
of components work together to enable developers to log messages
according to message type and level, and to control at runtime how
these messages are formatted and where they are reported.
</p>
<p>The <code>Logger</code> class is part of the logback-classic
module. On the other hand, the <code>Appender</code> and
<code>Layout</code> interfaces are part of logback-core. As a
general-purpose module, logback-core has no notion of
loggers.
</p>
<h3 class="doAnchor" name="LoggerContext">Logger context</h3>
<p>The first and foremost advantage of any logging API over plain
<code>System.out.println</code> resides in its ability to disable
certain log statements while allowing others to print
unhindered. This capability assumes that the logging space, that
is, the space of all possible logging statements, is categorized
according to some developer-chosen criteria. In logback-classic,
this categorization is an inherent part of loggers. Every single
logger is attached to a <code>LoggerContext</code> which is
responsible for manufacturing loggers as well as arranging them in
a tree like hierarchy.
</p>
<p>Loggers are named entities. Their names are case-sensitive and
they follow the hierarchical naming rule:
</p>
<div class="definition">
<div class="deftitle">Named Hierarchy</div>
<p>
A logger is said to be an ancestor of another logger if
its name followed by a dot is a prefix of the descendant
logger name. A logger is said to be a parent of a child
logger if there are no ancestors between itself and the
descendant logger.
</p>
</div>
<p>For example, the logger named <code>"com.foo"</code> is a
parent of the logger named <code>"com.foo.Bar"</code>. Similarly,
<code>"java"</code> is a parent of <code>"java.util"</code> and an
ancestor of <code>"java.util.Vector"</code>. This naming scheme
should be familiar to most developers.
</p>
<p>The root logger resides at the top of the logger hierarchy. It
is exceptional in that it is part of every hierarchy at its
inception. Like every logger, it can be retrieved by its name, as
follows:
</p>
<pre class="prettyprint source">Logger rootLogger = LoggerFactory.getLogger(<a
href="http://www.slf4j.org/apidocs/constant-values.html#org.slf4j.Logger.ROOT_LOGGER_NAME">org.slf4j.Logger.ROOT_LOGGER_NAME</a>);</pre>
<p>All other loggers are also retrieved with the class static
<code>getLogger</code> method found in the <a
href="http://www.slf4j.org/api/org/slf4j/Logger.html">org.slf4j.LoggerFactory</a>
class. This method takes the name of the desired logger as a
parameter. Some of the basic methods in the <code>Logger</code>
interface are listed below.
</p>
<pre class="prettyprint source">package org.slf4j;
public interface Logger {
// Printing methods:
public void trace(String message);
public void debug(String message);
public void info(String message);
public void warn(String message);
public void error(String message);
}</pre>
<h3 class="doAnchor" name="effectiveLevel">Effective Level aka
Level Inheritance </h3>
<p>Loggers may be assigned levels. The set of possible levels
(TRACE, DEBUG, INFO, WARN and ERROR) are defined in the
<code>ch.qos.logback.classic.Level</code> class. Note that in
logback, the <code>Level</code> class is final and cannot be
sub-classed, as a much more flexible approach exists in the form
of <code>Marker</code> objects.
</p>
<p>If a given logger is not assigned a level, then it inherits one
from its closest ancestor with an assigned level. More formally:
</p>
<div class="definition">
<p>The effective level for a given logger <em>L</em>, is equal
to the first non-null level in its hierarchy, starting at
<em>L</em> itself and proceeding upwards in the hierarchy
towards the root logger.
</p>
</div>
<p>To ensure that all loggers can eventually inherit a level, the
root logger always has an assigned level. By default, this level
is DEBUG.
</p>
<p>Below are four examples with various assigned level values and
the resulting effective (inherited) levels according to the level
inheritance rule.
</p>
<em>Example 1</em>
<table class="bodyTable">
<tr>
<th>Logger name</th>
<th>Assigned level</th>
<th>Effective level</th>
</tr>
<tr class="alt">
<td>root</td>
<td>DEBUG</td>
<td>DEBUG</td>
</tr>
<tr>
<td>X</td>
<td>none</td>
<td>DEBUG</td>
</tr>
<tr class="alt">
<td>X.Y</td>
<td>none</td>
<td>DEBUG</td>
</tr>
<tr>
<td>X.Y.Z</td>
<td>none</td>
<td>DEBUG</td>
</tr>
</table>
<p> In example 1 above, only the root logger is assigned a level.
This level value, <code>DEBUG</code>, is inherited by the other
loggers <code>X</code>, <code>X.Y</code> and <code>X.Y.Z</code>
</p>
<em>Example 2</em>
<table class="bodyTable">
<tr>
<th>Logger name</th>
<th>Assigned level</th>
<th>Effective level</th>
</tr>
<tr class="alt" align="left">
<td>root</td>
<td>ERROR</td>
<td>ERROR</td>
</tr>
<tr align="left">
<td>X</td>
<td>INFO</td>
<td>INFO</td>
</tr>
<tr class="alt" align="left">
<td>X.Y</td>
<td>DEBUG</td>
<td>DEBUG</td>
</tr>
<tr align="left">
<td>X.Y.Z</td>
<td>WARN</td>
<td>WARN</td>
</tr>
</table>
<p>In example 2 above, all loggers have an assigned level value.
Level inheritance does not come into play.
</p>
<em>Example 3</em>
<table class="bodyTable">
<tr>
<th>Logger name</th>
<th>Assigned level</th>
<th>Effective level</th>
</tr>
<tr class="alt" align="left">
<td>root</td>
<td>DEBUG</td>
<td>DEBUG</td>
</tr>
<tr align="left">
<td>X</td>
<td>INFO</td>
<td>INFO</td>
</tr>
<tr class="alt" align="left">
<td>X.Y</td>
<td>none</td>
<td>INFO</td>
</tr>
<tr align="left">
<td>X.Y.Z</td>
<td>ERROR</td>
<td>ERROR</td>
</tr>
</table>
<p>In example 3 above, the loggers <code>root</code>,
<code>X</code> and <code>X.Y.Z</code> are assigned the levels
<code>DEBUG</code>, <code>INFO</code> and <code>ERROR</code>
respectively. Logger <code>X.Y</code> inherits its level value
from its parent <code>X</code>.
</p>
<em>Example 4</em>
<table class="bodyTable">
<tr>
<th>Logger name</th>
<th>Assigned level</th>
<th>Effective level</th>
</tr>
<tr class="alt" align="left">
<td>root</td>
<td>DEBUG</td>
<td>DEBUG</td>
</tr>
<tr align="left">
<td>X</td>
<td>INFO</td>
<td>INFO</td>
</tr>
<tr class="alt" align="left">
<td>X.Y</td>
<td>none</td>
<td>INFO</td>
</tr>
<tr align="left">
<td>X.Y.Z</td>
<td>none</td>
<td>INFO</td>
</tr>
</table>
<p>In example 4 above, the loggers <code>root</code> and
<code>X</code> and are assigned the levels <code>DEBUG</code> and
<code>INFO</code> respectively. The loggers <code>X.Y</code> and
<code>X.Y.Z</code> inherit their level value from their nearest
parent <code>X</code>, which has an assigned level.
</p>
<h3 class="doAnchor" name="basic_selection">Printing methods and
the basic selection rule</h3>
<p>By definition, the printing method determines the level of a
logging request. For example, if <code>L</code> is a logger
instance, then the statement <code>L.info("..")</code> is a
logging statement of level INFO.
</p>
<p>A logging request is said to be <em>enabled</em> if its level
is higher than or equal to the effective level of its
logger. Otherwise, the request is said to be <em>disabled</em>. As
described previously, a logger without an assigned level will
inherit one from its nearest ancestor. This rule is summarized
below.
</p>
<div class="definition">
<div class="deftitle">Basic Selection Rule</div>
<p>A log request of level <em>p</em> issued to a logger having
an effective level <em>q</em>, is enabled if
<em>p >= q</em>.
</p>
</div>
<p>This rule is at the heart of logback. It assumes that levels
are ordered as follows:
<code>TRACE < DEBUG < INFO <
WARN < ERROR</code>.
</p>
<p>In a more graphic way, here is how the selection rule works. In
the following table, the vertical header shows the level of
the logging request, designated by <em>p</em>, while the
horizontal header shows effective level of the logger, designated
by <em>q</em>. The intersection of the rows (level request) and
columns (effective level) is the boolean resulting from the basic
selection rule.
</p>
<table width="80%">
<tr>
<td class="lgray_bg" rowspan="2">level of <br/>request <em>p</em></td>
<td style="border-top: 1px solid #DDDDDD;"
align="center" colspan="6">effective level <em>q</em></td>
</tr>
<tr align="left">
<th style="border-bottom: 1px solid #DDDDDD;">TRACE</th>
<th style="border-bottom: 1px solid #DDDDDD;">DEBUG</th>
<th style="border-bottom: 1px solid #DDDDDD;">INFO</th>
<th style="border-bottom: 1px solid #DDDDDD;">WARN</th>
<th style="border-bottom: 1px solid #DDDDDD;">ERROR</th>
<th style="border-bottom: 1px solid #DDDDDD;">OFF</th>
</tr>
<tr align="left" >
<th class="lgray_bg">TRACE</th>
<td><span class="greenBold">YES</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
</tr>
<tr align="left">
<th class="lgray_bg">DEBUG</th>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
</tr>
<tr align="left" >
<th class="lgray_bg">INFO</th>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
</tr>
<tr align="left" >
<th class="lgray_bg">WARN</th>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="redBold">NO</span></td>
<td><span class="redBold">NO</span></td>
</tr>
<tr align="left" >
<th class="lgray_bg">ERROR</th>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="greenBold">YES</span></td>
<td><span class="redBold">NO</span></td>
</tr>
</table>
<p>Here is an example of the basic selection rule.</p>
<pre class="prettyprint source">import ch.qos.logback.classic.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
....
// get a logger instance named "com.foo". Let us further assume that the
// logger is of type ch.qos.logback.classic.Logger so that we can
// set its level
ch.qos.logback.classic.Logger logger =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.foo");
//set its Level to <span class="blue">INFO</span>. The setLevel() method requires a logback logger
logger.setLevel(Level. <span class="blue">INFO</span>);
Logger barlogger = LoggerFactory.getLogger("com.foo.Bar");
// This request is enabled, because <span class="green bold">WARN</span> >= <span class="blue">INFO</span>
logger.<span class="green bold">warn</span>("Low fuel level.");
// This request is disabled, because <span class="green bold">DEBUG</span> < <span class="blue">INFO</span>.
logger.<span class="green bold">debug</span>("Starting search for nearest gas station.");
// The logger instance barlogger, named "com.foo.Bar",
// will inherit its level from the logger named
// "com.foo" Thus, the following request is enabled
// because <span class="green bold">INFO</span> >= <span class="blue">INFO</span>.
barlogger.<span class="green bold">info</span>("Located nearest gas station.");
// This request is disabled, because <span class="green bold">DEBUG</span> < <span class="blue">INFO</span>.
barlogger.<span class="green bold">debug</span>("Exiting gas station search");</pre>
<a name="RetrievingLoggers"></a>
<h3>Retrieving Loggers</h3>
<p>
Calling the <code><a href="../apidocs/org/slf4j/LoggerFactory.html#getLogger(java.lang.String)">LoggerFactory.getLogger</a></code>
method with the same name will always return a reference to
the exact same logger object.
</p>
<p>For example, in</p>
<pre class="prettyprint source">Logger x = LoggerFactory.getLogger("wombat");
Logger y = LoggerFactory.getLogger("wombat");</pre>
<p>
<code>x</code> and <code>y</code> refer to
<em>exactly</em> the same logger object.
</p>
<p>Thus, it is possible to configure a logger and then to retrieve
the same instance somewhere else in the code without passing
around references. In fundamental contradiction to biological
parenthood, where parents always precede their children, logback
loggers can be created and configured in any order. In particular,
a "parent" logger will find and link to its descendants even if it
is instantiated after them.
</p>
<p>Configuration of the logback environment is typically done at
application initialization. The preferred way is by reading a
configuration file. This approach will be discussed shortly.
</p>
<p>Logback makes it easy to name loggers by <em>software
component</em>. This can be accomplished by instantiating a
logger in each class, with the logger name equal to the fully
qualified name of the class. This is a useful and straightforward
method of defining loggers. As the log output bears the name of
the generating logger, this naming strategy makes it easy to
identify the origin of a log message. However, this is only one
possible, albeit common, strategy for naming loggers. Logback does
not restrict the possible set of loggers. As a developer, you are
free to name loggers as you wish.
</p>
<p>Nevertheless, naming loggers after the class where they are
located seems to be the best general strategy known so far.
</p>
<a name="AppendersAndLayouts"></a>
<h3>Appenders and Layouts</h3>
<p>The ability to selectively enable or disable logging requests
based on their logger is only part of the picture. Logback allows
logging requests to print to multiple destinations. In logback
speak, an output destination is called an appender. Currently,
appenders exist for the console, files, remote socket servers, to
MySQL, PostgreSQL, Oracle and other databases, JMS, and remote
UNIX Syslog daemons.
<!--It is also possible to log asynchronously. -->
</p>
<p>More than one appender can be attached to a logger.</p>
<p>The <code><a
href="../apidocs/ch/qos/logback/classic/Logger.html#addAppender(ch.qos.logback.core.Appender)">addAppender</a></code>
method adds an appender to a given logger. Each enabled logging
request for a given logger will be forwarded to all the appenders
in that logger as well as the appenders higher in the
hierarchy. In other words, appenders are inherited additively from
the logger hierarchy. For example, if a console appender is added
to the root logger, then all enabled logging requests will at
least print on the console. If in addition a file appender is
added to a logger, say <em>L</em>, then enabled logging requests
for <em>L</em> and <em>L</em>'s children will print on a file
<em>and</em> on the console. It is possible to override this
default behavior so that appender accumulation is no longer
additive by setting the additivity flag of a logger to false.
</p>
<p>The rules governing appender additivity are summarized below.
</p>
<div class="definition">
<h4 class="deftitle"><a name="additivity"
href="#additivity"><span class="anchor"/></a>Appender
Additivity</h4>
<p>The output of a log statement of logger <em>L</em> will go to
all the appenders in <em>L</em> and its ancestors. This is the
meaning of the term "appender additivity".
</p>
<p>However, if an ancestor of logger <em>L</em>, say <em>P</em>,
has the additivity flag set to false, then <em>L</em>'s output
will be directed to all the appenders in <em>L</em> and its
ancestors up to and including <em>P</em> but not the appenders in
any of the ancestors of <em>P</em>.
</p>
<p>Loggers have their additivity flag set to true by default.
</p>
</div>
The table below shows an example:
<table class="bodyTable">
<tr>
<th>Logger Name</th>
<th>Attached Appenders</th>
<th>Additivity Flag</th>
<th>Output Targets</th>
<th>Comment</th>
</tr>
<tr>
<td>root</td>
<td>A1</td>
<td>not applicable</td>
<td>A1</td>
<td>Since the root logger stands at the top of the logger
hierarchy, the additivity flag does not apply to it.
</td>
</tr>
<tr class="alt">
<td>x</td>
<td>A-x1, A-x2</td>
<td>true</td>
<td>A1, A-x1, A-x2</td>
<td>Appenders of "x" and of root.</td>
</tr>
<tr>
<td>x.y</td>
<td>none</td>
<td>true</td>
<td>A1, A-x1, A-x2</td>
<td>Appenders of "x" and of root.</td>
</tr>
<tr class="alt">
<td>x.y.z</td>
<td>A-xyz1</td>
<td>true</td>
<td>A1, A-x1, A-x2, A-xyz1</td>
<td>Appenders of "x.y.z", "x" and of root.</td>
</tr>
<tr>
<td>security</td>
<td>A-sec</td>
<td class="blue"><span class="blue">false</span></td>
<td>A-sec</td>
<td>
No appender accumulation since the additivity flag is set to
<code>false</code>. Only appender A-sec will be used.
</td>
</tr>
<tr class="alt">
<td>security.access</td>
<td>none</td>
<td>true</td>
<td>A-sec</td>
<td>
Only appenders of "security" because the additivity
flag in "security" is set to
<code>false</code>.
</td>
</tr>
</table>
<p>More often than not, users wish to customize not only the
output destination but also the output format. This is
accomplished by associating a <em>layout</em> with an
appender. The layout is responsible for formatting the logging
request according to the user's wishes, whereas an appender takes
care of sending the formatted output to its destination. The
<code>PatternLayout</code>, part of the standard logback
distribution, lets the user specify the output format according to
conversion patterns similar to the C language <code>printf</code>
function.
</p>
<p>For example, the PatternLayout with the conversion pattern
"%-4relative [%thread] %-5level %logger{32} - %msg%n" will output
something akin to:
</p>
<div class="prettyprint source"><pre>176 [main] DEBUG manual.architecture.HelloWorld2 - Hello world.</pre></div>
<p>The first field is the number of milliseconds elapsed since the
start of the program. The second field is the thread making the
log request. The third field is the level of the log request. The
fourth field is the name of the logger associated with the log
request. The text after the '-' is the message of the request.
</p>
<h3 class="doAnchor" name="parametrized">Parameterized
logging</h3>
<p>Given that loggers in logback-classic implement the <a
href="http://www.slf4j.org/api/org/slf4j/Logger.html">SLF4J's
Logger interface</a>, certain printing methods admit more than one
parameter. These printing method variants are mainly intended to
improve performance while minimizing the impact on the readability
of the code.
</p>
<p>For some Logger <code>logger</code>, writing,</p>
<pre class="prettyprint source">logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));</pre>
<p>incurs the cost of constructing the message parameter, that is
converting both integer <code>i</code> and <code>entry[i]</code>
to a String, and concatenating intermediate strings. This is
regardless of whether the message will be logged or not.
</p>
<p>One possible way to avoid the cost of parameter construction is
by surrounding the log statement with a test. Here is an example.
</p>
<pre class="prettyprint source">if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}</pre>
<p>This way you will not incur the cost of parameter construction
if debugging is disabled for <code>logger</code>. On the other
hand, if the logger is enabled for the DEBUG level, you will incur
the cost of evaluating whether the logger is enabled or not,
twice: once in <code>debugEnabled</code> and once in
<code>debug</code>. In practice, this overhead is insignificant
because evaluating a logger takes less than 1% of the time it
takes to actually log a request.
</p>
<h4>Better alternative</h4>
<p>There exists a convenient alternative based on message
formats. Assuming <code>entry</code> is an object, you can write:
</p>
<pre class="prettyprint source">Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);</pre>
<p>Only after evaluating whether to log or not, and only if the decision
is positive, will the logger implementation format the message and
replace the '{}' pair with the string value of <code>entry</code>.
In other words, this form does not incur the cost of parameter
construction when the log statement is disabled.
</p>
<p>The following two lines will yield the exact same output.
However, in case of a <em>disabled</em> logging statement, the
second variant will outperform the first variant by a factor of at
least 30.
</p>
<pre class="prettyprint source">logger.debug("The new entry is "+entry+".");
logger.debug("The new entry is {}.", entry);</pre>
<p>A two argument variant is also available. For example, you can
write:
</p>
<pre class="prettyprint source">logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);</pre>
<p>If three or more arguments need to be passed, an
<code>Object[]</code> variant is also available. For example, you
can write:
</p>
<pre class="prettyprint source">Object[] paramArray = {newVal, below, above};
logger.debug("Value {} was inserted between {} and {}.", paramArray);</pre>
<a name="UnderTheHood"></a>
<h3>A peek under the hood</h3>
<p>After we have introduced the essential logback components, we are
now ready to describe the steps that the logback framework takes
when the user invokes a logger's printing method. Let us now analyze
the steps logback takes when the user invokes the
<code>info()</code> method of a logger named <em>com.wombat</em>.
</p>
<h4>1. Get the filter chain decision</h4>
<p>If it exists, the <code>TurboFilter</code> chain is
invoked. Turbo filters can set a context-wide threshold, or filter
out certain events based on information such as <code>Marker</code>,
<code>Level</code>, <code>Logger</code>, message, or the
<code>Throwable</code> that are associated with each logging
request. If the reply of the filter chain is
<code>FilterReply.DENY</code>, then the logging request is
dropped. If it is <code>FilterReply.NEUTRAL</code>, then we continue
with the next step, i.e. step 2. In case the reply is
<code>FilterReply.ACCEPT</code>, we skip the next and directly jump
to step 3.
</p>
<h4>2. Apply the <a href="#basic_selection">basic selection
rule</a></h4>
<p>At this step, logback compares the effective level of the logger
with the level of the request. If the logging request is disabled
according to this test, then logback will drop the request without
further processing. Otherwise, it proceeds to the next step.
</p>
<h4>3. Create a <code>LoggingEvent</code> object</h4>
<p>If the request survived the previous filters, logback will
create a <code>ch.qos.logback.classic.LoggingEvent</code> object
containing all the relevant parameters of the request, such as the
logger of the request, the request level, the message itself, the
exception that might have been passed along with the request, the
current time, the current thread, various data about the class that
issued the logging request and the <code>MDC</code>. Note that some
of these fields are initialized lazily, that is only when they are
actually needed. The <code>MDC</code> is used to decorate the
logging request with additional contextual information. MDC is
discussed in a <a href="mdc.html">subsequent chapter</a>.</p>
<h4>4. Invoking appenders</h4>
<p>After the creation of a <code>LoggingEvent</code> object, logback
will invoke the <code>doAppend()</code> methods of all the
applicable appenders, that is, the appenders inherited from the
logger context.
</p>
<p>All appenders shipped with the logback distribution extend the
<code>AppenderBase</code> abstract class that implements the
<code>doAppend</code> method in a synchronized block ensuring
thread-safety. The <code>doAppend()</code> method of
<code>AppenderBase</code> also invokes custom filters attached to
the appender, if any such filters exist. Custom filters, which can
be dynamically attached to any appender, are presented in a <a
href="filters.html">separate chapter</a>.
</p>
<h4>5. Formatting the output</h4>
<p>It is responsibility of the invoked appender to format the
logging event. However, some (but not all) appenders delegate the
task of formatting the logging event to a layout. A layout formats
the <code>LoggingEvent</code> instance and returns the result as a
String. Note that some appenders, such as the
<code>SocketAppender</code>, do not transform the logging event into
a string but serialize it instead. Consequently, they do not
have nor require a layout.
</p>
<h4>6. Sending out the <code>LoggingEvent</code></h4>
<p>After the logging event is fully formatted it is sent to its
destination by each appender.
</p>
<p>
Here is a sequence UML diagram to show how everything works. You might
want to click on the image to display its bigger version.
</p>
<a href="underTheHood.html">
<img src="images/chapters/architecture/underTheHoodSequence2_small.gif"
alt="underTheHoodSequence2_small.gif"/>
</a>
<h3 class="doAnchor" name="performance">Performance</h3>
<p>One of the often-cited arguments against logging is its
computational cost. This is a legitimate concern as even
moderately-sized applications can generate thousands of log
requests. Much of our development effort is spent measuring and
tweaking logback's performance. Independently of these efforts, the
user should still be aware of the following performance issues.
</p>
<h4>1. Logging performance when logging is turned off entirely</h4>
<p>You can turn off logging entirely by setting the level of the
root logger to <code>Level.OFF</code>, the highest possible level.
When logging is turned off entirely, the cost of a log request
consists of a method invocation plus an integer comparison. On a
3.2Ghz Pentium D machine this cost is typically around 20
nanoseconds.
</p>
<p>However, any method invocation involves the "hidden" cost of
parameter construction. For example, for some logger <em>x</em>
writing,
</p>
<pre class="prettyprint source">x.debug("Entry number: " + i + "is " + entry[i]);</pre>
<p>incurs the cost of constructing the message parameter,
i.e. converting both integer <code>i</code> and
<code>entry[i]</code> to a string, and concatenating intermediate
strings, regardless of whether the message will be logged or not.
</p>
<p>The cost of parameter construction can be quite high and depends
on the size of the parameters involved. To avoid the cost of
parameter construction you can take advantage of SLF4J's parameterized
logging:
</p>
<pre class="prettyprint source">x.debug("Entry number: {} is {}", i, entry[i]);</pre>
<p>This variant will not incur the cost of parameter
construction. Compared to the previous call to the
<code>debug()</code> method, it will be faster by a wide margin.
The message will be formatted only if the logging request is to be
sent to attached appenders. Moreover, the component that formats
messages is highly optimized.
</p>
<p>Notwithstanding the above placing log statements in tight loops,
i.e. very frequently invoked code, is a lose-lose proposal, likely
to result in degraded performance. Logging in tight loops will slow
down your application even if logging is turned off, and if logging
is turned on, will generate massive (and hence useless) output.
</p>
<h4>2. The performance of deciding whether to log or not to log when
logging is turned on.</h4>
<p>In logback, there is no need to walk the logger hierarchy. A
logger knows its effective level (that is, its level, once level
inheritance has been taken into consideration) when it is
created. Should the level of a parent logger be changed, then all
child loggers are contacted to take notice of the change. Thus,
before accepting or denying a request based on the effective level,
the logger can make a quasi-instantaneous decision, without needing
to consult its ancestors.
</p>
<h4>3. Actual logging (formatting and writing to the output device)</h4>
<p>This is the cost of formatting the log output and sending it to
its target destination. Here again, a serious effort was made to
make layouts (formatters) perform as quickly as possible. The same
is true for appenders. The typical cost of actually logging is about
9 to 12 microseconds when logging to a file on the local machine.
It goes up to several milliseconds when logging to a database on a
remote server.
</p>
<p>Although feature-rich, one of the foremost design goals of
logback was speed of execution, a requirement which is second only
to reliability. Some logback components have been rewritten several
times to improve performance.
</p>
<script src="../templates/footer.js" type="text/javascript"></script>
</div>
</body>
</html>
|