Debian Codegraph @

logback-site/src/site/pages/reasonsToSwitch.html

raw
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
7 <title>Reasons to prefer logback</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
12 </head>
13 <body onload="decorate();">
14 <script type="text/javascript">prefix='';</script>
15 <script src="templates/header.js" type="text/javascript"></script>
16 <script type="text/javascript" src="js/jquery-min.js"></script>
17 <script type="text/javascript" src="js/decorator.js"></script>
18 <div id="left">
19 <noscript>Please turn on Javascript to view this menu</noscript>
20 <script src="templates/left.js" type="text/javascript"></script>
21 </div>
22 <div id="right">
23 <script src="templates/right.js" type="text/javascript"></script>
24 </div>
25
26 <div id="content">
27
28 <h2>Reasons to prefer logback over log4j</h2>
29
30 <p>Logback brings a very large number of improvements over log4j,
31 big and small. They are too many to enumerate exhaustively.
32 Nevertheless, here is a non-exhaustive list of reasons for
33 switching to logback from log4j. Keep in mind that logback is
34 conceptually very similar to log4j as both projects were founded
35 by the same developer. If you are already familiar with log4j, you
36 will quickly feel at home using logback. If you like log4j, you
37 will probably love logback.</p>
38
39
40 <h3 class="doAnchor" name="fasterImpl">Faster implementation</h3>
41
42 <p>Based on our previous work on log4j, logback internals have
43 been re-written to perform about ten times faster on certain
44 critical execution paths. Not only are logback components faster,
45 they have a smaller memory footprint as well.</p>
46
47 <h3 class="doAnchor" name="tdd">Extensive battery of tests</h3>
48
49 <p>Logback comes with a very extensive battery of tests developed
50 over the course of several years and untold hours of work. While
51 log4j is also tested, logback takes testing to a completely
52 different level. In our opinion, this is the single most important
53 reason to prefer logback over log4j. You want your logging
54 framework to be rock solid and dependable even under adverse
55 conditions.
56 </p>
57
58 <h3 class="doAnchor" name="slf4j">logback-classic speaks SLF4J
59 natively</h3>
60
61 <p>Since the <code>Logger</code> class in logback-classic
62 implements the SLF4J API natively, you incur zero overhead when
63 invoking an SLF4J logger with logback-classic as the underlying
64 implementation. Moreover, since logback-classic strongly
65 encourages the use of SLF4J as its client API, if you need to
66 switch to log4j or to j.u.l., you can do so by replacing one jar
67 file with another. You will not need to touch your code logging
68 via the SLF4J API. This can drastically reduce the work involved
69 in switching logging frameworks.
70 </p>
71
72 <h3 class="doAnchor" name="docs">Extensive documentation</h3>
73
74 <p>Logback ships with detailed and constantly updated
75 documentation.</p>
76
77 <h3 class="doAnchor" name="DSL">Configuration files in XML or
78 Groovy</h3>
79
80 <p>The traditional way of configuring logback is via an XML
81 file. Most of the examples in the documentation use this XML
82 syntax. However, as of logback version 0.9.22, <a
83 href="manual/groovy.html">configuration files written in
84 Groovy</a> are also supported. Compared to XML, Groovy-style
85 configuration is more intuitive, consistent and has a shorter
86 syntax.
87 </p>
88
89 <p>
90 There is also a <a
91 href="http://logback.qos.ch/translator/asGroovy.html">tool to
92 automatically migrate your logback.xml files to
93 logback.groovy</a>.
94 </p>
95
96 <h3 class="doAnchor" name="autoScan">Automatic reloading of
97 configuration files</h3>
98
99 <p>Logback-classic can <a
100 href="manual/configuration.html#autoScan">automatically reload its
101 configuration file upon modification</a>. The scanning process is
102 fast, contention-free, and dynamically scales to millions of
103 invocations per second spread over hundreds of threads. It also
104 plays well within application servers and more generally within
105 the JEE environment as it does <em>not</em> involve the creation
106 of a separate thread for scanning.
107 </p>
108
109 <h3 class="doAnchor" name="grace">Graceful recovery from I/O
110 failures</h3>
111
112 <p>Logback's <code>FileAppender</code> and all its sub-classes,
113 including <code>RollingFileAppender</code>, can gracefully recover
114 from I/O failures. Thus, if a file server fails temporarily, you
115 no longer need to restart your application just to get logging
116 working again. As soon as the file server comes back up, the
117 relevant logback appender will transparently and quickly recover
118 from the previous error condition.
119 </p>
120
121 <h3 class="doAnchor" name="maxHistory">Automatic removal of old
122 log archives</h3>
123
124 <p>By setting the <span class="option">maxHistory</span> property
125 of <a
126 href="manual/appenders.html#TimeBasedRollingPolicy">TimeBasedRollingPolicy</a>
127 or <a
128 href="manual/appenders.html#SizeAndTimeBasedFNATP">SizeAndTimeBasedFNATP</a>,
129 you can control the maximum number of archived files. If your
130 rolling policy calls for monthly rollover and you wish to keep one
131 year's worth of logs, simply set the <span
132 class="option">maxHistory</span> property to 12. Archived log
133 files older than 12 months will be automatically removed.
134 </p>
135
136 <h3 class="doAnchor" name="compression">Automatic compression of
137 archived log files</h3>
138
139 <p><a
140 href="manual/appenders.html#RollingFileAppender">RollingFileAppender</a>
141 can automatically compress archived log files during
142 rollover. Compression always occurs asynchronously so that even
143 for large log files, your application is not blocked for the
144 duration of the compression.
145 </p>
146
147 <h3 class="doAnchor" name="prudent">Prudent mode</h3>
148
149 <p>In <a href="manual/appenders.html#prudent">prudent mode</a>,
150 multiple <code>FileAppender</code> instances running on multiple
151 JVMs can safely write to the same log file. With certain
152 limitations, prudent mode extends to
153 <code>RollingFileAppender</code>.
154 </p>
155
156 <h3 class="doAnchor" name="lilith">Lilith</h3>
157
158 <p><a href="http://lilith.huxhorn.de/">Lilith</a> is a logging and
159 access event viewer for logback. It is comparable to log4j's
160 chainsaw, except that Lilith is designed to handle large amounts of
161 logging data without flinching.</p>
162
163 <h3 class="doAnchor" name="conditional">Conditional processing of
164 configuration files</h3>
165
166 <p>Developers often need to juggle between several logback
167 configuration files targeting different environments such as
168 development, testing and production. These configuration files
169 have substantial parts in common, differing only in a few
170 places. To avoid duplication, logback supports <a
171 href="manual/configuration.html#conditional">conditional
172 processing of configuration files</a> with the help of
173 <code>&lt;if></code>, <code>&lt;then></code> and
174 <code>&lt;else></code> elements so that a single configuration
175 file can adequately target several environments.
176 </p>
177
178
179 <h3 class="doAnchor" name="filters">Filters</h3>
180
181 <p>Logback comes with a wide array of <a
182 href="manual/filters.html">filtering capabilities</a> going much
183 further than what log4j has to offer. For example, let's assume
184 that you have a business-critical application deployed on a
185 production server. Given the large volume of transactions
186 processed, logging level is set to WARN so that only warnings and
187 errors are logged. Now imagine that you are confronted with a bug
188 that can be reproduced on the production system but remains
189 elusive on the test platform due to unspecified differences
190 between those two environments (production/testing).
191 </p>
192
193 <p>With log4j, your only choice is to lower the logging level to
194 DEBUG on the production system in an attempt to identify the
195 problem. Unfortunately, this will generate large volume of logging
196 data, making analysis difficult. More importantly, extensive
197 logging can impact the performance of your application on the
198 production system.</p>
199
200 <p>With logback, you have the option of keeping logging at the
201 WARN level for all users except for the one user, say Alice, who
202 is responsible for identifying the problem. When Alice is logged
203 on, she will be logging at level DEBUG while other users can
204 continue to log at the WARN level. This feat can be accomplished
205 by adding 4 lines of XML to your configuration file. Search for
206 <code>MDCFilter</code> in the <a
207 href="manual/filters.html#TurboFilter">relevant section</a> of the
208 manual.
209 </p>
210
211
212 <h3 class="doAnchor" name="sift">SiftingAppender</h3>
213
214 <p><a
215 href="manual/appenders.html#SiftingAppender">SiftingAppender</a>
216 is an amazingly versatile appender. It can be used to separate (or
217 sift) logging according to <b>any</b> given runtime attribute. For
218 example, <code>SiftingAppender</code> can separate logging events
219 according to user sessions, so that the logs generated by each
220 user go into distinct log files, one log file per user.
221 </p>
222
223 <h3 class="doAnchor" name="packagingData">Stack traces with
224 packaging data</h3>
225
226 <p>When logback prints an exception, the stack trace will include
227 packaging data. Here is a sample stack trace generated by the <a
228 href="demo.html">logback-demo</a> web-application.</p>
229
230 <pre>14:28:48.835 [btpool0-7] INFO c.q.l.demo.prime.PrimeAction - 99 is not a valid value
231 java.lang.Exception: 99 is invalid
232 at ch.qos.logback.demo.prime.PrimeAction.execute(PrimeAction.java:28) [classes/:na]
233 at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) [struts-1.2.9.jar:1.2.9]
234 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) [struts-1.2.9.jar:1.2.9]
235 at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) [struts-1.2.9.jar:1.2.9]
236 at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) [servlet-api-2.5-6.1.12.jar:6.1.12]
237 at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502) [jetty-6.1.12.jar:6.1.12]
238 at ch.qos.logback.demo.UserServletFilter.doFilter(UserServletFilter.java:44) [classes/:na]
239 at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115) [jetty-6.1.12.jar:6.1.12]
240 at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:361) [jetty-6.1.12.jar:6.1.12]
241 at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417) [jetty-6.1.12.jar:6.1.12]
242 at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) [jetty-6.1.12.jar:6.1.12]</pre>
243
244 <p>From the above, you can recognize that the application is using
245 Struts version 1.2.9 and was deployed under jetty version
246 6.1.12. Thus, stack traces will quickly inform the reader about
247 the classes intervening in the exception but also the package and
248 package versions they belong to. When your customers send you a
249 stack trace, as a developer you will no longer need to ask them
250 to send you information about the versions of packages they are
251 using. The information will be part of the stack trace. See <a
252 href="manual/layouts.html#xThrowable">"%xThrowable" conversion
253 word</a> for details.
254 </p>
255
256 <p>This feature can be quite helpful to the point that some users
257 mistakenly consider it a <a
258 href="http://www.jetbrains.net/devnet/message/5259058">feature of
259 their IDE</a>.
260 </p>
261
262 <h3 class="doAnchor" name="logbackAccess">Logback-access,
263 i.e. HTTP-access logging with brains, is an integral part of
264 logback</h3>
265
266 <p>Last but not least, the logback-access module, part of the
267 logback distribution, integrates with Servlet containers such as
268 Jetty or Tomcat to provide rich and powerful HTTP-access log
269 functionality. Since logback-access was part of the initial
270 design, all the logback-classic features you love are
271 available in logback-access as well.</p>
272
273 <h3 class="doAnchor" name="inSummary">In summary</h3>
274
275 <p>We have listed a number of reasons for preferring logback over
276 log4j. Given that logback builds upon on our previous work on
277 log4j, simply put, logback is just a better log4j.</p>
278
279 <script src="templates/footer.js" type="text/javascript"></script>
280 </div>
281
282 </body>
283 </html>