| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 |
<head> |
| 5 |
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> |
| 6 |
<title>Chapter 1: Introduction</title> |
| 7 |
<link rel="stylesheet" type="text/css" href="../css/common.css" /> |
| 8 |
<link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" /> |
| 9 |
<link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" /> |
| 10 |
<link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" /> |
| 11 |
|
| 12 |
</head> |
| 13 |
<body onload="prettyPrint(); decorate();"> |
| 14 |
<script type="text/javascript">prefix='../';</script> |
| 15 |
<script type="text/javascript" src="../js/prettify.js"></script> |
| 16 |
<script src="../templates/header.js" type="text/javascript"> </script> |
| 17 |
<script type="text/javascript" src="../js/jquery-min.js"></script> |
| 18 |
<script type="text/javascript" src="../js/decorator.js"></script> |
| 19 |
<div id="left"> |
| 20 |
<noscript>Please turn on Javascript to view this menu</noscript> |
| 21 |
<script src="../templates/left.js" type="text/javascript"></script> |
| 22 |
</div> |
| 23 |
<div id="right"> |
| 24 |
<script src="menu.js" type="text/javascript"></script> |
| 25 |
</div> |
| 26 |
<div id="content"> |
| 27 |
|
| 28 |
<h1>Chapter 1: Introduction</h1> |
| 29 |
|
| 30 |
<div class="quote"> |
| 31 |
<p><em> The morale effects are startling. Enthusiasm jumps when |
| 32 |
there is a running system, even a simple one. Efforts redouble when |
| 33 |
the first picture from a new graphics software system appears on the |
| 34 |
screen, even if it is only a rectangle. One always has, at every |
| 35 |
stage in the process, a working system. I find that teams can grow |
| 36 |
much more complex entities in four months than they can |
| 37 |
build.</em></p> |
| 38 |
|
| 39 |
<p>—FREDERICK P. BROOKS, JR., <em>The Mythical Man-Month</em></p> |
| 40 |
</div> |
| 41 |
|
| 42 |
|
| 43 |
<script src="../templates/creative.js" type="text/javascript"></script> |
| 44 |
|
| 45 |
|
| 46 |
<h2>What is logback?</h2> |
| 47 |
|
| 48 |
<a href="introduction_ja.html">和訳 (Japanese translation)</a> |
| 49 |
|
| 50 |
<p>Logback is intended as a successor to the popular log4j |
| 51 |
project. It was designed by Ceki Gülcü, log4j's |
| 52 |
founder. It builds upon a decade of experience gained in |
| 53 |
designing industrial-strength logging systems. The resulting |
| 54 |
product, i.e. logback, is faster and has a smaller footprint than |
| 55 |
all existing logging systems, sometimes by a wide margin. Just as |
| 56 |
importantly, logback offers <a |
| 57 |
href="../reasonsToSwitch.html">unique and rather useful |
| 58 |
features</a> missing in other logging systems. |
| 59 |
</p> |
| 60 |
|
| 61 |
<h2>First Baby Step</h2> |
| 62 |
|
| 63 |
<script src="../templates/setup.js" type="text/javascript"></script> |
| 64 |
|
| 65 |
<a name="Requirements"></a> |
| 66 |
<h3>Requirements</h3> |
| 67 |
|
| 68 |
<p>Logback-classic module requires the presence of |
| 69 |
<em>slf4j-api.jar</em> and <em>logback-core.jar</em> in addition to |
| 70 |
<em>logback-classic.jar</em> on the classpath. |
| 71 |
</p> |
| 72 |
|
| 73 |
<p>The <em>logback-*.jar</em> files are part of the logback |
| 74 |
distribution whereas <em>slf4j-api-${slf4j.version}.jar</em> ships |
| 75 |
with <a href="http://www.slf4j.org">SLF4J</a>, a separate project. |
| 76 |
</p> |
| 77 |
|
| 78 |
<p>Let us now begin experimenting with logback.</p> |
| 79 |
|
| 80 |
<em>Example 1.1: Basic template for logging (<a href="../xref/chapters/introduction/HelloWorld1.html">logback-examples/src/main/java/chapters/introduction/HelloWorld1.java</a>)</em> |
| 81 |
<pre class="prettyprint source">package chapters.introduction; |
| 82 |
|
| 83 |
import org.slf4j.Logger; |
| 84 |
import org.slf4j.LoggerFactory; |
| 85 |
|
| 86 |
public class HelloWorld1 { |
| 87 |
|
| 88 |
public static void main(String[] args) { |
| 89 |
|
| 90 |
Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld1"); |
| 91 |
logger.debug("Hello world."); |
| 92 |
|
| 93 |
} |
| 94 |
}</pre> |
| 95 |
|
| 96 |
<p><code>HelloWorld1</code> class is defined in the |
| 97 |
<code>chapters.introduction</code> package. It starts by importing the <a |
| 98 |
href="http://slf4j.org/api/org/slf4j/Logger.html"><code>Logger</code></a> |
| 99 |
and <a |
| 100 |
href="http://slf4j.org/api/org/slf4j/LoggerFactory.html"><code>LoggerFactory</code></a> |
| 101 |
classes defined in the SLF4J API, specifically within the |
| 102 |
<code>org.slf4j</code> package. |
| 103 |
</p> |
| 104 |
|
| 105 |
|
| 106 |
<p>On the first line of the main() method, the variable named |
| 107 |
<code>logger</code> is assigned a <code>Logger</code> instance |
| 108 |
retrieved by invoking the static <code>getLogger</code> method |
| 109 |
from the <code>LoggerFactory</code> class. This logger is named |
| 110 |
"chapters.introduction.HelloWorld1". The main method proceeds to call the |
| 111 |
<code>debug</code> method of this logger passing "Hello World" as |
| 112 |
an argument. We say that the main method contains a logging |
| 113 |
statement of level DEBUG with the message "Hello world". |
| 114 |
</p> |
| 115 |
|
| 116 |
<p>Note that the above example does not reference any logback |
| 117 |
classes. In most cases, as far as logging is concerned, your |
| 118 |
classes will only need to import SLF4J classes. Thus, the vast |
| 119 |
majority, if not all, of your classes will use the SLF4J |
| 120 |
API and will be oblivious to the existence of logback. |
| 121 |
</p> |
| 122 |
|
| 123 |
|
| 124 |
<p>You can launch the first |
| 125 |
sample application, <em>chapters.introduction.HelloWorld1</em> with the command: |
| 126 |
</p> |
| 127 |
<div class="source"><pre>java chapters.introduction.HelloWorld1</pre></div> |
| 128 |
|
| 129 |
<p>Launching the <code>HelloWorld1</code> application will output |
| 130 |
a single line on the console. By virtue of logback's default |
| 131 |
configuration policy, when no default configuration file is found, |
| 132 |
logback will add a <code>ConsoleAppender</code> to the root |
| 133 |
logger. |
| 134 |
</p> |
| 135 |
|
| 136 |
<p class="source">20:49:07.962 [main] DEBUG chapters.introduction.HelloWorld1 - Hello world.</p> |
| 137 |
|
| 138 |
<p>Logback can report information about its internal state using a |
| 139 |
built-in status system. Important events occurring during logback's |
| 140 |
lifetime can be accessed through a component called |
| 141 |
<code>StatusManager</code>. For the time being, let us instruct |
| 142 |
logback to print its internal state by invoking the static |
| 143 |
<code>print()</code> method of the <code>StatusPrinter</code> |
| 144 |
class. |
| 145 |
</p> |
| 146 |
|
| 147 |
<em>Example: Printing Logger Status (<a href="../xref/chapters/introduction/HelloWorld2.html">logback-examples/src/main/java/chapters/introduction/HelloWorld2.java</a>)</em> |
| 148 |
<pre class="prettyprint source">package chapters.introduction; |
| 149 |
|
| 150 |
import org.slf4j.Logger; |
| 151 |
import org.slf4j.LoggerFactory; |
| 152 |
<b>import ch.qos.logback.classic.LoggerContext; |
| 153 |
import ch.qos.logback.core.util.StatusPrinter;</b> |
| 154 |
|
| 155 |
public class HelloWorld2 { |
| 156 |
|
| 157 |
public static void main(String[] args) { |
| 158 |
Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2"); |
| 159 |
logger.debug("Hello world."); |
| 160 |
|
| 161 |
// print internal state |
| 162 |
<b>LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 163 |
StatusPrinter.print(lc);</b> |
| 164 |
} |
| 165 |
}</pre> |
| 166 |
|
| 167 |
|
| 168 |
<p>Running the <code>HelloWorld2</code> application will produce |
| 169 |
the following output:</p> |
| 170 |
|
| 171 |
<div class="source longline"><pre>12:49:22.203 [main] DEBUG chapters.introduction.HelloWorld2 - Hello world. |
| 172 |
12:49:22,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] |
| 173 |
12:49:22,078 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] |
| 174 |
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml] |
| 175 |
12:49:22,093 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration. |
| 176 |
</pre></div> |
| 177 |
|
| 178 |
|
| 179 |
<p>Logback explains that having failed to find the |
| 180 |
<em>logback-test.xml</em> and <em>logback.xml</em> configuration |
| 181 |
files (discussed later), it configured itself using its default |
| 182 |
policy, which is a basic <code>ConsoleAppender</code>. An |
| 183 |
<code>Appender</code> is a class that can be seen as an output |
| 184 |
destination. Appenders exist for many different destinations |
| 185 |
including the console, files, Syslog, TCP Sockets, JMS and many |
| 186 |
more. Users can also easily create their own Appenders as |
| 187 |
appropriate for their specific situation. |
| 188 |
</p> |
| 189 |
|
| 190 |
<p>Note that in case of errors, logback will automatically print its |
| 191 |
internal state on the console.</p> |
| 192 |
|
| 193 |
<p>The previous examples are rather simple. Actual logging in a |
| 194 |
larger application would not be that different. The general pattern |
| 195 |
for logging statements would not change. Only the configuration |
| 196 |
process would be different. However, you would probably want to |
| 197 |
customize or configure logback according to your needs. Logback |
| 198 |
configuration will be covered in subsequent chapters. |
| 199 |
</p> |
| 200 |
|
| 201 |
<p>Note that in the above example we have instructed logback to |
| 202 |
print its internal state by invoking the |
| 203 |
<code>StatusPrinter.print()</code> method. Logback's internal status |
| 204 |
information can be very useful in diagnosing logback-related |
| 205 |
problems. |
| 206 |
</p> |
| 207 |
|
| 208 |
<p>Here is a list of the three required steps in order to enable |
| 209 |
logging in your application. |
| 210 |
</p> |
| 211 |
|
| 212 |
<ol> |
| 213 |
<li>Configure the logback environment. You can do so in several |
| 214 |
more or less sophisticated ways. More on this later.</li> |
| 215 |
|
| 216 |
<li>In every class where you wish to perform logging, retrieve a |
| 217 |
<code>Logger</code> instance by invoking the |
| 218 |
<code>org.slf4j.LoggerFactory</code> class' |
| 219 |
<code>getLogger()</code> method, passing the current class name |
| 220 |
or the class itself as a parameter.</li> |
| 221 |
|
| 222 |
<li>Use this logger instance by invoking its printing methods, |
| 223 |
namely the debug(), info(), warn() and error() methods. This will |
| 224 |
produce logging output on the configured appenders.</li> |
| 225 |
</ol> |
| 226 |
|
| 227 |
|
| 228 |
<h2 class="doAnchor" name="building">Building logback</h2> |
| 229 |
|
| 230 |
<p>As its build tool, logback relies on <a |
| 231 |
href="http://maven.apache.org">Maven</a>, a widely-used open-source |
| 232 |
build tool. |
| 233 |
</p> |
| 234 |
|
| 235 |
<p>Once you have installed Maven, building the logback project, |
| 236 |
including all its modules, should be as easy as issuing a <code>mvn |
| 237 |
install</code> command from within the directory where you |
| 238 |
unarchived the logback distribution. Maven will automatically |
| 239 |
download the required external libraries. |
| 240 |
</p> |
| 241 |
|
| 242 |
<p>Logback distributions contain complete source code such that you |
| 243 |
can modify parts of logback library and build your own version of |
| 244 |
it. You may even redistribute the modified version, as long as you |
| 245 |
adhere to the conditions of the LGPL license or the EPL license. |
| 246 |
</p> |
| 247 |
|
| 248 |
<p>For building logback under an IDE, please see the <a |
| 249 |
href="../setup.html#ide">relevant section on the class path setup |
| 250 |
page</a>.</p> |
| 251 |
|
| 252 |
<script src="../templates/footer.js" type="text/javascript"></script> |
| 253 |
</div> |
| 254 |
</body> |
| 255 |
</html> |