forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLogging.html
More file actions
213 lines (190 loc) · 7.44 KB
/
Copy pathJavaLogging.html
File metadata and controls
213 lines (190 loc) · 7.44 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999.html">
<head>
<title>Java Logging</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>Java Logging</h2>
<h3>Contents</h3>
<a href="#Introduction">Introduction</a><br/>
<a href="#Installing">Installing</a><br/>
<a href="#Usage">Usage</a><br/>
<a href="#Configuration">Configuration</a><br/>
<a href="#Example">Example</a><br/>
<h3>Introduction</h3>
<p>
<a target="_blank" href="http://java.sun.com/javase/6/docs/technotes/guides/logging/">Java Logging</a>
is a logging implementation that is part of the JDK.
Other popular options include
<a target="_blank" href="http://logging.apache.org/log4j/docs/">Log4J</a> and
<a target="_blank" href="http://jakarta.apache.org/commons/logging/">Jakarta Commons Logging</a>.
An advantage of using this instead of Log4J is that it avoids
potential JAR version conflicts which can occur if you use
a different version of Log4J than a library you are using that
expects/includes a different version.
</p>
<a name="Installing"/>
<h3>Installing <a class="toplink" href="#top">top</a></h3>
<p>
There is nothing to be installed and nothing to put in the classpath
since this is included in the JDK.
</p>
<a name="Usage"/>
<h3>Usage <a class="toplink" href="#top">top</a></h3>
<p>
The basic steps to use Java Logging are:
<ol>
<li>Configure Java Logging.</li>
<li>In each Java class that needs logging,
<ol style="list-style-type: lower-alpha">
<li>Obtain a Logger object.</li>
<li>Invoking logging methods on it.</li>
</ol>
</li>
</ol>
</p>
<a name="Configuration"/>
<h3>Configuration <a class="toplink" href="#top">top</a></h3>
<p>
Java Logging can be configured in three ways.
<ol>
<li>from Java code</li>
<li>from $JAVA_HOME/lib/logging.properties</li>
<li>from a property file located elsewhere</li>
</ol>
The last option is typically preferred.
</p>
<p>
The configuration property file must be specified by
setting the system property
<code>java.util.logging.config.file</code>.
This can be done when an application is run by specifying
<code>-Djava.util.logging.config.file={file-path}</code>
on the <code>java</code> command.
When running from ant, add the following as a child of the
<code>java</code> task.<br/>
<code>
<sysproperty key="java.util.logging.config.file"
value="{file-path}"/>
</code>
</p>
<p>
Here's an example of a configuration file,
named <code>logging.properties</code>.
<pre><div class="code">
# Add handlers to the root logger.
# These are inherited by all other loggers.
handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
# Set the logging level of the root logger.
# Levels from lowest to highest are
# FINEST, FINER, FINE, CONFIG, INFO, WARNING and SEVERE.
# The default level for all loggers and handlers is INFO.
.level=SEVERE
# Specify logging levels for specific namespaces.
com.ociweb.level=FINEST
# Configure the ConsoleHandler.
# ConsoleHandler uses java.util.logging.SimpleFormatter by default.
# Even though the root logger has the same level as this,
# the next line is still needed because we're configuring a handler,
# not a logger, and handlers don't inherit properties from the root logger.
java.util.logging.ConsoleHandler.level=SEVERE
# Configure the FileHandler.
# FileHandler uses java.util.logging.XMLFormatter by default.
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=FINEST
# The following special tokens can be used in the pattern property
# which specifies the location and name of the log file.
# / - standard path separator
# %t - system temporary directory
# %h - value of the user.home system property
# %g - generation number for rotating logs
# %u - unique number to avoid conflicts
# FileHandler writes to %h/demo0.log by default.
java.util.logging.FileHandler.pattern=demo.log
</div></pre>
<a name="Example"/>
<h3>Example <a class="toplink" href="#top">top</a></h3>
<p>
Here's an example of using Java Logging in Java code.
Note how a <a target="_blank"
href="http://java.sun.com/javase/6/docs/api/java/util/logging/Logger.html">Logger</a>
instance is obtained.
</p>
<pre><div class="code">
package com.ociweb.demo;
import java.util.logging.Logger;
public class LoggingDemo {
private Logger logger = Logger.getLogger(getClass().getName());
public static void main(String[] args) {
LoggingDemo app = new LoggingDemo();
// To do something only if logging is set at or below a given level,
// such as construct a complicated log message ...
//if (logger.isLoggable(Level.{constant})) { ... }
app.doSomething();
}
public LoggingDemo() {
logger.finest("message 1");
}
public void doSomething() {
logger.info("message 2");
}
}
</div></pre>
<p>
In this example, using the configuration shown earlier,
the file <code>demo.log</code> is created in the current directory
with the following contents.
Note how the method name reported for logging from a constructor
is <code><init></code>.
</p>
<pre><div class="code">
Feb 20, 2007 9:14:24 AM com.ociweb.demo.LoggingDemo <init>
FINEST: message 1
Feb 20, 2007 9:14:24 AM com.ociweb.demo.LoggingDemo doSomething
INFO: message 2
</div></pre>
<p>
Methods in the <a target="_blank"
href="http://java.sun.com/javase/6/docs/api/java/util/logging/Logger.html">Logger</a>
class for writing log messages,
in order from lowest to highest severity, include the following.
Each of these has no return value and takes a String parameter
that is either a log message or a key in a message catalog.
What is a message catalog?
Perhaps it's a ResourceBundle described <a target="_blank"
href="http://java.sun.com/docs/books/tutorial/i18n/intro/after.html">here</a>.
<ul>
<li><code>finest</code></li>
<li><code>finer</code></li>
<li><code>fine</code></li>
<li><code>config</code></li>
<li><code>info</code></li>
<li><code>warning</code></li>
<li><code>severe</code></li>
</ul>
The <code>log</code> method can be used to write a log message
for a given level and it takes additional parameter for
constructing a log message using the <code>toString</code> value
of an arbitrary number of objects.
</p>
<p>
Methods in the <a target="_blank"
href="http://java.sun.com/javase/6/docs/api/java/util/logging/Logger.html">Logger</a>
class for obtaining information about the current level
of the Logger include the following.
These are commonly used to avoid construction of a
complicated log message when it will not be output.
<ul>
<li><code>Level getLevel()</code></li>
<li><code>boolean isLoggable(Level level)</code></li>
</ul>
</p>
<hr />
<p style="text-align:center">
Copyright © 2007 Object Computing, Inc. All rights reserved.
</p>
</body>
</html>