jaroslav@1258
|
1 |
/*
|
jaroslav@1258
|
2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@1258
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1258
|
4 |
*
|
jaroslav@1258
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1258
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1258
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1258
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1258
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1258
|
10 |
*
|
jaroslav@1258
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1258
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1258
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1258
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1258
|
15 |
* accompanied this code).
|
jaroslav@1258
|
16 |
*
|
jaroslav@1258
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1258
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1258
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1258
|
20 |
*
|
jaroslav@1258
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1258
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1258
|
23 |
* questions.
|
jaroslav@1258
|
24 |
*/
|
jaroslav@1258
|
25 |
|
jaroslav@1258
|
26 |
|
jaroslav@1258
|
27 |
package java.util.logging;
|
jaroslav@1258
|
28 |
|
jaroslav@1258
|
29 |
import java.util.*;
|
jaroslav@1258
|
30 |
import java.util.concurrent.CopyOnWriteArrayList;
|
jaroslav@1258
|
31 |
import java.security.*;
|
jaroslav@1258
|
32 |
import java.lang.ref.WeakReference;
|
jaroslav@1258
|
33 |
|
jaroslav@1258
|
34 |
/**
|
jaroslav@1258
|
35 |
* A Logger object is used to log messages for a specific
|
jaroslav@1258
|
36 |
* system or application component. Loggers are normally named,
|
jaroslav@1258
|
37 |
* using a hierarchical dot-separated namespace. Logger names
|
jaroslav@1258
|
38 |
* can be arbitrary strings, but they should normally be based on
|
jaroslav@1258
|
39 |
* the package name or class name of the logged component, such
|
jaroslav@1258
|
40 |
* as java.net or javax.swing. In addition it is possible to create
|
jaroslav@1258
|
41 |
* "anonymous" Loggers that are not stored in the Logger namespace.
|
jaroslav@1258
|
42 |
* <p>
|
jaroslav@1258
|
43 |
* Logger objects may be obtained by calls on one of the getLogger
|
jaroslav@1258
|
44 |
* factory methods. These will either create a new Logger or
|
jaroslav@1258
|
45 |
* return a suitable existing Logger. It is important to note that
|
jaroslav@1258
|
46 |
* the Logger returned by one of the {@code getLogger} factory methods
|
jaroslav@1258
|
47 |
* may be garbage collected at any time if a strong reference to the
|
jaroslav@1258
|
48 |
* Logger is not kept.
|
jaroslav@1258
|
49 |
* <p>
|
jaroslav@1258
|
50 |
* Logging messages will be forwarded to registered Handler
|
jaroslav@1258
|
51 |
* objects, which can forward the messages to a variety of
|
jaroslav@1258
|
52 |
* destinations, including consoles, files, OS logs, etc.
|
jaroslav@1258
|
53 |
* <p>
|
jaroslav@1258
|
54 |
* Each Logger keeps track of a "parent" Logger, which is its
|
jaroslav@1258
|
55 |
* nearest existing ancestor in the Logger namespace.
|
jaroslav@1258
|
56 |
* <p>
|
jaroslav@1258
|
57 |
* Each Logger has a "Level" associated with it. This reflects
|
jaroslav@1258
|
58 |
* a minimum Level that this logger cares about. If a Logger's
|
jaroslav@1258
|
59 |
* level is set to <tt>null</tt>, then its effective level is inherited
|
jaroslav@1258
|
60 |
* from its parent, which may in turn obtain it recursively from its
|
jaroslav@1258
|
61 |
* parent, and so on up the tree.
|
jaroslav@1258
|
62 |
* <p>
|
jaroslav@1258
|
63 |
* The log level can be configured based on the properties from the
|
jaroslav@1258
|
64 |
* logging configuration file, as described in the description
|
jaroslav@1258
|
65 |
* of the LogManager class. However it may also be dynamically changed
|
jaroslav@1258
|
66 |
* by calls on the Logger.setLevel method. If a logger's level is
|
jaroslav@1258
|
67 |
* changed the change may also affect child loggers, since any child
|
jaroslav@1258
|
68 |
* logger that has <tt>null</tt> as its level will inherit its
|
jaroslav@1258
|
69 |
* effective level from its parent.
|
jaroslav@1258
|
70 |
* <p>
|
jaroslav@1258
|
71 |
* On each logging call the Logger initially performs a cheap
|
jaroslav@1258
|
72 |
* check of the request level (e.g., SEVERE or FINE) against the
|
jaroslav@1258
|
73 |
* effective log level of the logger. If the request level is
|
jaroslav@1258
|
74 |
* lower than the log level, the logging call returns immediately.
|
jaroslav@1258
|
75 |
* <p>
|
jaroslav@1258
|
76 |
* After passing this initial (cheap) test, the Logger will allocate
|
jaroslav@1258
|
77 |
* a LogRecord to describe the logging message. It will then call a
|
jaroslav@1258
|
78 |
* Filter (if present) to do a more detailed check on whether the
|
jaroslav@1258
|
79 |
* record should be published. If that passes it will then publish
|
jaroslav@1258
|
80 |
* the LogRecord to its output Handlers. By default, loggers also
|
jaroslav@1258
|
81 |
* publish to their parent's Handlers, recursively up the tree.
|
jaroslav@1258
|
82 |
* <p>
|
jaroslav@1258
|
83 |
* Each Logger may have a ResourceBundle name associated with it.
|
jaroslav@1258
|
84 |
* The named bundle will be used for localizing logging messages.
|
jaroslav@1258
|
85 |
* If a Logger does not have its own ResourceBundle name, then
|
jaroslav@1258
|
86 |
* it will inherit the ResourceBundle name from its parent,
|
jaroslav@1258
|
87 |
* recursively up the tree.
|
jaroslav@1258
|
88 |
* <p>
|
jaroslav@1258
|
89 |
* Most of the logger output methods take a "msg" argument. This
|
jaroslav@1258
|
90 |
* msg argument may be either a raw value or a localization key.
|
jaroslav@1258
|
91 |
* During formatting, if the logger has (or inherits) a localization
|
jaroslav@1258
|
92 |
* ResourceBundle and if the ResourceBundle has a mapping for the msg
|
jaroslav@1258
|
93 |
* string, then the msg string is replaced by the localized value.
|
jaroslav@1258
|
94 |
* Otherwise the original msg string is used. Typically, formatters use
|
jaroslav@1258
|
95 |
* java.text.MessageFormat style formatting to format parameters, so
|
jaroslav@1258
|
96 |
* for example a format string "{0} {1}" would format two parameters
|
jaroslav@1258
|
97 |
* as strings.
|
jaroslav@1258
|
98 |
* <p>
|
jaroslav@1258
|
99 |
* When mapping ResourceBundle names to ResourceBundles, the Logger
|
jaroslav@1258
|
100 |
* will first try to use the Thread's ContextClassLoader. If that
|
jaroslav@1258
|
101 |
* is null it will try the SystemClassLoader instead. As a temporary
|
jaroslav@1258
|
102 |
* transition feature in the initial implementation, if the Logger is
|
jaroslav@1258
|
103 |
* unable to locate a ResourceBundle from the ContextClassLoader or
|
jaroslav@1258
|
104 |
* SystemClassLoader the Logger will also search up the class stack
|
jaroslav@1258
|
105 |
* and use successive calling ClassLoaders to try to locate a ResourceBundle.
|
jaroslav@1258
|
106 |
* (This call stack search is to allow containers to transition to
|
jaroslav@1258
|
107 |
* using ContextClassLoaders and is likely to be removed in future
|
jaroslav@1258
|
108 |
* versions.)
|
jaroslav@1258
|
109 |
* <p>
|
jaroslav@1258
|
110 |
* Formatting (including localization) is the responsibility of
|
jaroslav@1258
|
111 |
* the output Handler, which will typically call a Formatter.
|
jaroslav@1258
|
112 |
* <p>
|
jaroslav@1258
|
113 |
* Note that formatting need not occur synchronously. It may be delayed
|
jaroslav@1258
|
114 |
* until a LogRecord is actually written to an external sink.
|
jaroslav@1258
|
115 |
* <p>
|
jaroslav@1258
|
116 |
* The logging methods are grouped in five main categories:
|
jaroslav@1258
|
117 |
* <ul>
|
jaroslav@1258
|
118 |
* <li><p>
|
jaroslav@1258
|
119 |
* There are a set of "log" methods that take a log level, a message
|
jaroslav@1258
|
120 |
* string, and optionally some parameters to the message string.
|
jaroslav@1258
|
121 |
* <li><p>
|
jaroslav@1258
|
122 |
* There are a set of "logp" methods (for "log precise") that are
|
jaroslav@1258
|
123 |
* like the "log" methods, but also take an explicit source class name
|
jaroslav@1258
|
124 |
* and method name.
|
jaroslav@1258
|
125 |
* <li><p>
|
jaroslav@1258
|
126 |
* There are a set of "logrb" method (for "log with resource bundle")
|
jaroslav@1258
|
127 |
* that are like the "logp" method, but also take an explicit resource
|
jaroslav@1258
|
128 |
* bundle name for use in localizing the log message.
|
jaroslav@1258
|
129 |
* <li><p>
|
jaroslav@1258
|
130 |
* There are convenience methods for tracing method entries (the
|
jaroslav@1258
|
131 |
* "entering" methods), method returns (the "exiting" methods) and
|
jaroslav@1258
|
132 |
* throwing exceptions (the "throwing" methods).
|
jaroslav@1258
|
133 |
* <li><p>
|
jaroslav@1258
|
134 |
* Finally, there are a set of convenience methods for use in the
|
jaroslav@1258
|
135 |
* very simplest cases, when a developer simply wants to log a
|
jaroslav@1258
|
136 |
* simple string at a given log level. These methods are named
|
jaroslav@1258
|
137 |
* after the standard Level names ("severe", "warning", "info", etc.)
|
jaroslav@1258
|
138 |
* and take a single argument, a message string.
|
jaroslav@1258
|
139 |
* </ul>
|
jaroslav@1258
|
140 |
* <p>
|
jaroslav@1258
|
141 |
* For the methods that do not take an explicit source name and
|
jaroslav@1258
|
142 |
* method name, the Logging framework will make a "best effort"
|
jaroslav@1258
|
143 |
* to determine which class and method called into the logging method.
|
jaroslav@1258
|
144 |
* However, it is important to realize that this automatically inferred
|
jaroslav@1258
|
145 |
* information may only be approximate (or may even be quite wrong!).
|
jaroslav@1258
|
146 |
* Virtual machines are allowed to do extensive optimizations when
|
jaroslav@1258
|
147 |
* JITing and may entirely remove stack frames, making it impossible
|
jaroslav@1258
|
148 |
* to reliably locate the calling class and method.
|
jaroslav@1258
|
149 |
* <P>
|
jaroslav@1258
|
150 |
* All methods on Logger are multi-thread safe.
|
jaroslav@1258
|
151 |
* <p>
|
jaroslav@1258
|
152 |
* <b>Subclassing Information:</b> Note that a LogManager class may
|
jaroslav@1258
|
153 |
* provide its own implementation of named Loggers for any point in
|
jaroslav@1258
|
154 |
* the namespace. Therefore, any subclasses of Logger (unless they
|
jaroslav@1258
|
155 |
* are implemented in conjunction with a new LogManager class) should
|
jaroslav@1258
|
156 |
* take care to obtain a Logger instance from the LogManager class and
|
jaroslav@1258
|
157 |
* should delegate operations such as "isLoggable" and "log(LogRecord)"
|
jaroslav@1258
|
158 |
* to that instance. Note that in order to intercept all logging
|
jaroslav@1258
|
159 |
* output, subclasses need only override the log(LogRecord) method.
|
jaroslav@1258
|
160 |
* All the other logging methods are implemented as calls on this
|
jaroslav@1258
|
161 |
* log(LogRecord) method.
|
jaroslav@1258
|
162 |
*
|
jaroslav@1258
|
163 |
* @since 1.4
|
jaroslav@1258
|
164 |
*/
|
jaroslav@1258
|
165 |
|
jaroslav@1258
|
166 |
|
jaroslav@1258
|
167 |
public class Logger {
|
jaroslav@1258
|
168 |
private static final Handler emptyHandlers[] = new Handler[0];
|
jaroslav@1258
|
169 |
private static final int offValue = Level.OFF.intValue();
|
jaroslav@1258
|
170 |
private LogManager manager;
|
jaroslav@1258
|
171 |
private String name;
|
jaroslav@1258
|
172 |
private final CopyOnWriteArrayList<Handler> handlers =
|
jaroslav@1258
|
173 |
new CopyOnWriteArrayList<>();
|
jaroslav@1258
|
174 |
private String resourceBundleName;
|
jaroslav@1258
|
175 |
private volatile boolean useParentHandlers = true;
|
jaroslav@1258
|
176 |
private volatile Filter filter;
|
jaroslav@1258
|
177 |
private boolean anonymous;
|
jaroslav@1258
|
178 |
|
jaroslav@1258
|
179 |
private ResourceBundle catalog; // Cached resource bundle
|
jaroslav@1258
|
180 |
private String catalogName; // name associated with catalog
|
jaroslav@1258
|
181 |
private Locale catalogLocale; // locale associated with catalog
|
jaroslav@1258
|
182 |
|
jaroslav@1258
|
183 |
// The fields relating to parent-child relationships and levels
|
jaroslav@1258
|
184 |
// are managed under a separate lock, the treeLock.
|
jaroslav@1258
|
185 |
private static Object treeLock = new Object();
|
jaroslav@1258
|
186 |
// We keep weak references from parents to children, but strong
|
jaroslav@1258
|
187 |
// references from children to parents.
|
jaroslav@1258
|
188 |
private volatile Logger parent; // our nearest parent.
|
jaroslav@1258
|
189 |
private ArrayList<LogManager.LoggerWeakRef> kids; // WeakReferences to loggers that have us as parent
|
jaroslav@1258
|
190 |
private volatile Level levelObject;
|
jaroslav@1258
|
191 |
private volatile int levelValue; // current effective level value
|
jaroslav@1258
|
192 |
|
jaroslav@1258
|
193 |
/**
|
jaroslav@1258
|
194 |
* GLOBAL_LOGGER_NAME is a name for the global logger.
|
jaroslav@1258
|
195 |
*
|
jaroslav@1258
|
196 |
* @since 1.6
|
jaroslav@1258
|
197 |
*/
|
jaroslav@1258
|
198 |
public static final String GLOBAL_LOGGER_NAME = "global";
|
jaroslav@1258
|
199 |
|
jaroslav@1258
|
200 |
/**
|
jaroslav@1258
|
201 |
* Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
|
jaroslav@1258
|
202 |
*
|
jaroslav@1258
|
203 |
* @return global logger object
|
jaroslav@1258
|
204 |
* @since 1.7
|
jaroslav@1258
|
205 |
*/
|
jaroslav@1258
|
206 |
public static final Logger getGlobal() {
|
jaroslav@1258
|
207 |
return global;
|
jaroslav@1258
|
208 |
}
|
jaroslav@1258
|
209 |
|
jaroslav@1258
|
210 |
/**
|
jaroslav@1258
|
211 |
* The "global" Logger object is provided as a convenience to developers
|
jaroslav@1258
|
212 |
* who are making casual use of the Logging package. Developers
|
jaroslav@1258
|
213 |
* who are making serious use of the logging package (for example
|
jaroslav@1258
|
214 |
* in products) should create and use their own Logger objects,
|
jaroslav@1258
|
215 |
* with appropriate names, so that logging can be controlled on a
|
jaroslav@1258
|
216 |
* suitable per-Logger granularity. Developers also need to keep a
|
jaroslav@1258
|
217 |
* strong reference to their Logger objects to prevent them from
|
jaroslav@1258
|
218 |
* being garbage collected.
|
jaroslav@1258
|
219 |
* <p>
|
jaroslav@1258
|
220 |
* @deprecated Initialization of this field is prone to deadlocks.
|
jaroslav@1258
|
221 |
* The field must be initialized by the Logger class initialization
|
jaroslav@1258
|
222 |
* which may cause deadlocks with the LogManager class initialization.
|
jaroslav@1258
|
223 |
* In such cases two class initialization wait for each other to complete.
|
jaroslav@1258
|
224 |
* The preferred way to get the global logger object is via the call
|
jaroslav@1258
|
225 |
* <code>Logger.getGlobal()</code>.
|
jaroslav@1258
|
226 |
* For compatibility with old JDK versions where the
|
jaroslav@1258
|
227 |
* <code>Logger.getGlobal()</code> is not available use the call
|
jaroslav@1258
|
228 |
* <code>Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)</code>
|
jaroslav@1258
|
229 |
* or <code>Logger.getLogger("global")</code>.
|
jaroslav@1258
|
230 |
*/
|
jaroslav@1258
|
231 |
@Deprecated
|
jaroslav@1258
|
232 |
public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
|
jaroslav@1258
|
233 |
|
jaroslav@1258
|
234 |
/**
|
jaroslav@1258
|
235 |
* Protected method to construct a logger for a named subsystem.
|
jaroslav@1258
|
236 |
* <p>
|
jaroslav@1258
|
237 |
* The logger will be initially configured with a null Level
|
jaroslav@1258
|
238 |
* and with useParentHandlers set to true.
|
jaroslav@1258
|
239 |
*
|
jaroslav@1258
|
240 |
* @param name A name for the logger. This should
|
jaroslav@1258
|
241 |
* be a dot-separated name and should normally
|
jaroslav@1258
|
242 |
* be based on the package name or class name
|
jaroslav@1258
|
243 |
* of the subsystem, such as java.net
|
jaroslav@1258
|
244 |
* or javax.swing. It may be null for anonymous Loggers.
|
jaroslav@1258
|
245 |
* @param resourceBundleName name of ResourceBundle to be used for localizing
|
jaroslav@1258
|
246 |
* messages for this logger. May be null if none
|
jaroslav@1258
|
247 |
* of the messages require localization.
|
jaroslav@1258
|
248 |
* @throws MissingResourceException if the resourceBundleName is non-null and
|
jaroslav@1258
|
249 |
* no corresponding resource can be found.
|
jaroslav@1258
|
250 |
*/
|
jaroslav@1258
|
251 |
protected Logger(String name, String resourceBundleName) {
|
jaroslav@1258
|
252 |
this.manager = LogManager.getLogManager();
|
jaroslav@1258
|
253 |
if (resourceBundleName != null) {
|
jaroslav@1258
|
254 |
// Note: we may get a MissingResourceException here.
|
jaroslav@1258
|
255 |
setupResourceInfo(resourceBundleName);
|
jaroslav@1258
|
256 |
}
|
jaroslav@1258
|
257 |
this.name = name;
|
jaroslav@1258
|
258 |
levelValue = Level.INFO.intValue();
|
jaroslav@1258
|
259 |
}
|
jaroslav@1258
|
260 |
|
jaroslav@1258
|
261 |
// This constructor is used only to create the global Logger.
|
jaroslav@1258
|
262 |
// It is needed to break a cyclic dependence between the LogManager
|
jaroslav@1258
|
263 |
// and Logger static initializers causing deadlocks.
|
jaroslav@1258
|
264 |
private Logger(String name) {
|
jaroslav@1258
|
265 |
// The manager field is not initialized here.
|
jaroslav@1258
|
266 |
this.name = name;
|
jaroslav@1258
|
267 |
levelValue = Level.INFO.intValue();
|
jaroslav@1258
|
268 |
}
|
jaroslav@1258
|
269 |
|
jaroslav@1258
|
270 |
// It is called from the LogManager.<clinit> to complete
|
jaroslav@1258
|
271 |
// initialization of the global Logger.
|
jaroslav@1258
|
272 |
void setLogManager(LogManager manager) {
|
jaroslav@1258
|
273 |
this.manager = manager;
|
jaroslav@1258
|
274 |
}
|
jaroslav@1258
|
275 |
|
jaroslav@1258
|
276 |
private void checkAccess() throws SecurityException {
|
jaroslav@1258
|
277 |
if (!anonymous) {
|
jaroslav@1258
|
278 |
if (manager == null) {
|
jaroslav@1258
|
279 |
// Complete initialization of the global Logger.
|
jaroslav@1258
|
280 |
manager = LogManager.getLogManager();
|
jaroslav@1258
|
281 |
}
|
jaroslav@1258
|
282 |
manager.checkAccess();
|
jaroslav@1258
|
283 |
}
|
jaroslav@1258
|
284 |
}
|
jaroslav@1258
|
285 |
|
jaroslav@1258
|
286 |
/**
|
jaroslav@1258
|
287 |
* Find or create a logger for a named subsystem. If a logger has
|
jaroslav@1258
|
288 |
* already been created with the given name it is returned. Otherwise
|
jaroslav@1258
|
289 |
* a new logger is created.
|
jaroslav@1258
|
290 |
* <p>
|
jaroslav@1258
|
291 |
* If a new logger is created its log level will be configured
|
jaroslav@1258
|
292 |
* based on the LogManager configuration and it will configured
|
jaroslav@1258
|
293 |
* to also send logging output to its parent's Handlers. It will
|
jaroslav@1258
|
294 |
* be registered in the LogManager global namespace.
|
jaroslav@1258
|
295 |
* <p>
|
jaroslav@1258
|
296 |
* Note: The LogManager may only retain a weak reference to the newly
|
jaroslav@1258
|
297 |
* created Logger. It is important to understand that a previously
|
jaroslav@1258
|
298 |
* created Logger with the given name may be garbage collected at any
|
jaroslav@1258
|
299 |
* time if there is no strong reference to the Logger. In particular,
|
jaroslav@1258
|
300 |
* this means that two back-to-back calls like
|
jaroslav@1258
|
301 |
* {@code getLogger("MyLogger").log(...)} may use different Logger
|
jaroslav@1258
|
302 |
* objects named "MyLogger" if there is no strong reference to the
|
jaroslav@1258
|
303 |
* Logger named "MyLogger" elsewhere in the program.
|
jaroslav@1258
|
304 |
*
|
jaroslav@1258
|
305 |
* @param name A name for the logger. This should
|
jaroslav@1258
|
306 |
* be a dot-separated name and should normally
|
jaroslav@1258
|
307 |
* be based on the package name or class name
|
jaroslav@1258
|
308 |
* of the subsystem, such as java.net
|
jaroslav@1258
|
309 |
* or javax.swing
|
jaroslav@1258
|
310 |
* @return a suitable Logger
|
jaroslav@1258
|
311 |
* @throws NullPointerException if the name is null.
|
jaroslav@1258
|
312 |
*/
|
jaroslav@1258
|
313 |
|
jaroslav@1258
|
314 |
// Synchronization is not required here. All synchronization for
|
jaroslav@1258
|
315 |
// adding a new Logger object is handled by LogManager.addLogger().
|
jaroslav@1258
|
316 |
public static Logger getLogger(String name) {
|
jaroslav@1258
|
317 |
// This method is intentionally not a wrapper around a call
|
jaroslav@1258
|
318 |
// to getLogger(name, resourceBundleName). If it were then
|
jaroslav@1258
|
319 |
// this sequence:
|
jaroslav@1258
|
320 |
//
|
jaroslav@1258
|
321 |
// getLogger("Foo", "resourceBundleForFoo");
|
jaroslav@1258
|
322 |
// getLogger("Foo");
|
jaroslav@1258
|
323 |
//
|
jaroslav@1258
|
324 |
// would throw an IllegalArgumentException in the second call
|
jaroslav@1258
|
325 |
// because the wrapper would result in an attempt to replace
|
jaroslav@1258
|
326 |
// the existing "resourceBundleForFoo" with null.
|
jaroslav@1258
|
327 |
LogManager manager = LogManager.getLogManager();
|
jaroslav@1258
|
328 |
return manager.demandLogger(name);
|
jaroslav@1258
|
329 |
}
|
jaroslav@1258
|
330 |
|
jaroslav@1258
|
331 |
/**
|
jaroslav@1258
|
332 |
* Find or create a logger for a named subsystem. If a logger has
|
jaroslav@1258
|
333 |
* already been created with the given name it is returned. Otherwise
|
jaroslav@1258
|
334 |
* a new logger is created.
|
jaroslav@1258
|
335 |
* <p>
|
jaroslav@1258
|
336 |
* If a new logger is created its log level will be configured
|
jaroslav@1258
|
337 |
* based on the LogManager and it will configured to also send logging
|
jaroslav@1258
|
338 |
* output to its parent's Handlers. It will be registered in
|
jaroslav@1258
|
339 |
* the LogManager global namespace.
|
jaroslav@1258
|
340 |
* <p>
|
jaroslav@1258
|
341 |
* Note: The LogManager may only retain a weak reference to the newly
|
jaroslav@1258
|
342 |
* created Logger. It is important to understand that a previously
|
jaroslav@1258
|
343 |
* created Logger with the given name may be garbage collected at any
|
jaroslav@1258
|
344 |
* time if there is no strong reference to the Logger. In particular,
|
jaroslav@1258
|
345 |
* this means that two back-to-back calls like
|
jaroslav@1258
|
346 |
* {@code getLogger("MyLogger", ...).log(...)} may use different Logger
|
jaroslav@1258
|
347 |
* objects named "MyLogger" if there is no strong reference to the
|
jaroslav@1258
|
348 |
* Logger named "MyLogger" elsewhere in the program.
|
jaroslav@1258
|
349 |
* <p>
|
jaroslav@1258
|
350 |
* If the named Logger already exists and does not yet have a
|
jaroslav@1258
|
351 |
* localization resource bundle then the given resource bundle
|
jaroslav@1258
|
352 |
* name is used. If the named Logger already exists and has
|
jaroslav@1258
|
353 |
* a different resource bundle name then an IllegalArgumentException
|
jaroslav@1258
|
354 |
* is thrown.
|
jaroslav@1258
|
355 |
* <p>
|
jaroslav@1258
|
356 |
* @param name A name for the logger. This should
|
jaroslav@1258
|
357 |
* be a dot-separated name and should normally
|
jaroslav@1258
|
358 |
* be based on the package name or class name
|
jaroslav@1258
|
359 |
* of the subsystem, such as java.net
|
jaroslav@1258
|
360 |
* or javax.swing
|
jaroslav@1258
|
361 |
* @param resourceBundleName name of ResourceBundle to be used for localizing
|
jaroslav@1258
|
362 |
* messages for this logger. May be <CODE>null</CODE> if none of
|
jaroslav@1258
|
363 |
* the messages require localization.
|
jaroslav@1258
|
364 |
* @return a suitable Logger
|
jaroslav@1258
|
365 |
* @throws MissingResourceException if the resourceBundleName is non-null and
|
jaroslav@1258
|
366 |
* no corresponding resource can be found.
|
jaroslav@1258
|
367 |
* @throws IllegalArgumentException if the Logger already exists and uses
|
jaroslav@1258
|
368 |
* a different resource bundle name.
|
jaroslav@1258
|
369 |
* @throws NullPointerException if the name is null.
|
jaroslav@1258
|
370 |
*/
|
jaroslav@1258
|
371 |
|
jaroslav@1258
|
372 |
// Synchronization is not required here. All synchronization for
|
jaroslav@1258
|
373 |
// adding a new Logger object is handled by LogManager.addLogger().
|
jaroslav@1258
|
374 |
public static Logger getLogger(String name, String resourceBundleName) {
|
jaroslav@1258
|
375 |
LogManager manager = LogManager.getLogManager();
|
jaroslav@1258
|
376 |
Logger result = manager.demandLogger(name);
|
jaroslav@1258
|
377 |
if (result.resourceBundleName == null) {
|
jaroslav@1258
|
378 |
// Note: we may get a MissingResourceException here.
|
jaroslav@1258
|
379 |
result.setupResourceInfo(resourceBundleName);
|
jaroslav@1258
|
380 |
} else if (!result.resourceBundleName.equals(resourceBundleName)) {
|
jaroslav@1258
|
381 |
throw new IllegalArgumentException(result.resourceBundleName +
|
jaroslav@1258
|
382 |
" != " + resourceBundleName);
|
jaroslav@1258
|
383 |
}
|
jaroslav@1258
|
384 |
return result;
|
jaroslav@1258
|
385 |
}
|
jaroslav@1258
|
386 |
|
jaroslav@1258
|
387 |
|
jaroslav@1258
|
388 |
/**
|
jaroslav@1258
|
389 |
* Create an anonymous Logger. The newly created Logger is not
|
jaroslav@1258
|
390 |
* registered in the LogManager namespace. There will be no
|
jaroslav@1258
|
391 |
* access checks on updates to the logger.
|
jaroslav@1258
|
392 |
* <p>
|
jaroslav@1258
|
393 |
* This factory method is primarily intended for use from applets.
|
jaroslav@1258
|
394 |
* Because the resulting Logger is anonymous it can be kept private
|
jaroslav@1258
|
395 |
* by the creating class. This removes the need for normal security
|
jaroslav@1258
|
396 |
* checks, which in turn allows untrusted applet code to update
|
jaroslav@1258
|
397 |
* the control state of the Logger. For example an applet can do
|
jaroslav@1258
|
398 |
* a setLevel or an addHandler on an anonymous Logger.
|
jaroslav@1258
|
399 |
* <p>
|
jaroslav@1258
|
400 |
* Even although the new logger is anonymous, it is configured
|
jaroslav@1258
|
401 |
* to have the root logger ("") as its parent. This means that
|
jaroslav@1258
|
402 |
* by default it inherits its effective level and handlers
|
jaroslav@1258
|
403 |
* from the root logger.
|
jaroslav@1258
|
404 |
* <p>
|
jaroslav@1258
|
405 |
*
|
jaroslav@1258
|
406 |
* @return a newly created private Logger
|
jaroslav@1258
|
407 |
*/
|
jaroslav@1258
|
408 |
public static Logger getAnonymousLogger() {
|
jaroslav@1258
|
409 |
return getAnonymousLogger(null);
|
jaroslav@1258
|
410 |
}
|
jaroslav@1258
|
411 |
|
jaroslav@1258
|
412 |
/**
|
jaroslav@1258
|
413 |
* Create an anonymous Logger. The newly created Logger is not
|
jaroslav@1258
|
414 |
* registered in the LogManager namespace. There will be no
|
jaroslav@1258
|
415 |
* access checks on updates to the logger.
|
jaroslav@1258
|
416 |
* <p>
|
jaroslav@1258
|
417 |
* This factory method is primarily intended for use from applets.
|
jaroslav@1258
|
418 |
* Because the resulting Logger is anonymous it can be kept private
|
jaroslav@1258
|
419 |
* by the creating class. This removes the need for normal security
|
jaroslav@1258
|
420 |
* checks, which in turn allows untrusted applet code to update
|
jaroslav@1258
|
421 |
* the control state of the Logger. For example an applet can do
|
jaroslav@1258
|
422 |
* a setLevel or an addHandler on an anonymous Logger.
|
jaroslav@1258
|
423 |
* <p>
|
jaroslav@1258
|
424 |
* Even although the new logger is anonymous, it is configured
|
jaroslav@1258
|
425 |
* to have the root logger ("") as its parent. This means that
|
jaroslav@1258
|
426 |
* by default it inherits its effective level and handlers
|
jaroslav@1258
|
427 |
* from the root logger.
|
jaroslav@1258
|
428 |
* <p>
|
jaroslav@1258
|
429 |
* @param resourceBundleName name of ResourceBundle to be used for localizing
|
jaroslav@1258
|
430 |
* messages for this logger.
|
jaroslav@1258
|
431 |
* May be null if none of the messages require localization.
|
jaroslav@1258
|
432 |
* @return a newly created private Logger
|
jaroslav@1258
|
433 |
* @throws MissingResourceException if the resourceBundleName is non-null and
|
jaroslav@1258
|
434 |
* no corresponding resource can be found.
|
jaroslav@1258
|
435 |
*/
|
jaroslav@1258
|
436 |
|
jaroslav@1258
|
437 |
// Synchronization is not required here. All synchronization for
|
jaroslav@1258
|
438 |
// adding a new anonymous Logger object is handled by doSetParent().
|
jaroslav@1258
|
439 |
public static Logger getAnonymousLogger(String resourceBundleName) {
|
jaroslav@1258
|
440 |
LogManager manager = LogManager.getLogManager();
|
jaroslav@1258
|
441 |
// cleanup some Loggers that have been GC'ed
|
jaroslav@1258
|
442 |
manager.drainLoggerRefQueueBounded();
|
jaroslav@1258
|
443 |
Logger result = new Logger(null, resourceBundleName);
|
jaroslav@1258
|
444 |
result.anonymous = true;
|
jaroslav@1258
|
445 |
Logger root = manager.getLogger("");
|
jaroslav@1258
|
446 |
result.doSetParent(root);
|
jaroslav@1258
|
447 |
return result;
|
jaroslav@1258
|
448 |
}
|
jaroslav@1258
|
449 |
|
jaroslav@1258
|
450 |
/**
|
jaroslav@1258
|
451 |
* Retrieve the localization resource bundle for this
|
jaroslav@1258
|
452 |
* logger for the current default locale. Note that if
|
jaroslav@1258
|
453 |
* the result is null, then the Logger will use a resource
|
jaroslav@1258
|
454 |
* bundle inherited from its parent.
|
jaroslav@1258
|
455 |
*
|
jaroslav@1258
|
456 |
* @return localization bundle (may be null)
|
jaroslav@1258
|
457 |
*/
|
jaroslav@1258
|
458 |
public ResourceBundle getResourceBundle() {
|
jaroslav@1258
|
459 |
return findResourceBundle(getResourceBundleName());
|
jaroslav@1258
|
460 |
}
|
jaroslav@1258
|
461 |
|
jaroslav@1258
|
462 |
/**
|
jaroslav@1258
|
463 |
* Retrieve the localization resource bundle name for this
|
jaroslav@1258
|
464 |
* logger. Note that if the result is null, then the Logger
|
jaroslav@1258
|
465 |
* will use a resource bundle name inherited from its parent.
|
jaroslav@1258
|
466 |
*
|
jaroslav@1258
|
467 |
* @return localization bundle name (may be null)
|
jaroslav@1258
|
468 |
*/
|
jaroslav@1258
|
469 |
public String getResourceBundleName() {
|
jaroslav@1258
|
470 |
return resourceBundleName;
|
jaroslav@1258
|
471 |
}
|
jaroslav@1258
|
472 |
|
jaroslav@1258
|
473 |
/**
|
jaroslav@1258
|
474 |
* Set a filter to control output on this Logger.
|
jaroslav@1258
|
475 |
* <P>
|
jaroslav@1258
|
476 |
* After passing the initial "level" check, the Logger will
|
jaroslav@1258
|
477 |
* call this Filter to check if a log record should really
|
jaroslav@1258
|
478 |
* be published.
|
jaroslav@1258
|
479 |
*
|
jaroslav@1258
|
480 |
* @param newFilter a filter object (may be null)
|
jaroslav@1258
|
481 |
* @exception SecurityException if a security manager exists and if
|
jaroslav@1258
|
482 |
* the caller does not have LoggingPermission("control").
|
jaroslav@1258
|
483 |
*/
|
jaroslav@1258
|
484 |
public void setFilter(Filter newFilter) throws SecurityException {
|
jaroslav@1258
|
485 |
checkAccess();
|
jaroslav@1258
|
486 |
filter = newFilter;
|
jaroslav@1258
|
487 |
}
|
jaroslav@1258
|
488 |
|
jaroslav@1258
|
489 |
/**
|
jaroslav@1258
|
490 |
* Get the current filter for this Logger.
|
jaroslav@1258
|
491 |
*
|
jaroslav@1258
|
492 |
* @return a filter object (may be null)
|
jaroslav@1258
|
493 |
*/
|
jaroslav@1258
|
494 |
public Filter getFilter() {
|
jaroslav@1258
|
495 |
return filter;
|
jaroslav@1258
|
496 |
}
|
jaroslav@1258
|
497 |
|
jaroslav@1258
|
498 |
/**
|
jaroslav@1258
|
499 |
* Log a LogRecord.
|
jaroslav@1258
|
500 |
* <p>
|
jaroslav@1258
|
501 |
* All the other logging methods in this class call through
|
jaroslav@1258
|
502 |
* this method to actually perform any logging. Subclasses can
|
jaroslav@1258
|
503 |
* override this single method to capture all log activity.
|
jaroslav@1258
|
504 |
*
|
jaroslav@1258
|
505 |
* @param record the LogRecord to be published
|
jaroslav@1258
|
506 |
*/
|
jaroslav@1258
|
507 |
public void log(LogRecord record) {
|
jaroslav@1258
|
508 |
if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
509 |
return;
|
jaroslav@1258
|
510 |
}
|
jaroslav@1258
|
511 |
Filter theFilter = filter;
|
jaroslav@1258
|
512 |
if (theFilter != null && !theFilter.isLoggable(record)) {
|
jaroslav@1258
|
513 |
return;
|
jaroslav@1258
|
514 |
}
|
jaroslav@1258
|
515 |
|
jaroslav@1258
|
516 |
// Post the LogRecord to all our Handlers, and then to
|
jaroslav@1258
|
517 |
// our parents' handlers, all the way up the tree.
|
jaroslav@1258
|
518 |
|
jaroslav@1258
|
519 |
Logger logger = this;
|
jaroslav@1258
|
520 |
while (logger != null) {
|
jaroslav@1258
|
521 |
for (Handler handler : logger.getHandlers()) {
|
jaroslav@1258
|
522 |
handler.publish(record);
|
jaroslav@1258
|
523 |
}
|
jaroslav@1258
|
524 |
|
jaroslav@1258
|
525 |
if (!logger.getUseParentHandlers()) {
|
jaroslav@1258
|
526 |
break;
|
jaroslav@1258
|
527 |
}
|
jaroslav@1258
|
528 |
|
jaroslav@1258
|
529 |
logger = logger.getParent();
|
jaroslav@1258
|
530 |
}
|
jaroslav@1258
|
531 |
}
|
jaroslav@1258
|
532 |
|
jaroslav@1258
|
533 |
// private support method for logging.
|
jaroslav@1258
|
534 |
// We fill in the logger name, resource bundle name, and
|
jaroslav@1258
|
535 |
// resource bundle and then call "void log(LogRecord)".
|
jaroslav@1258
|
536 |
private void doLog(LogRecord lr) {
|
jaroslav@1258
|
537 |
lr.setLoggerName(name);
|
jaroslav@1258
|
538 |
String ebname = getEffectiveResourceBundleName();
|
jaroslav@1258
|
539 |
if (ebname != null) {
|
jaroslav@1258
|
540 |
lr.setResourceBundleName(ebname);
|
jaroslav@1258
|
541 |
lr.setResourceBundle(findResourceBundle(ebname));
|
jaroslav@1258
|
542 |
}
|
jaroslav@1258
|
543 |
log(lr);
|
jaroslav@1258
|
544 |
}
|
jaroslav@1258
|
545 |
|
jaroslav@1258
|
546 |
|
jaroslav@1258
|
547 |
//================================================================
|
jaroslav@1258
|
548 |
// Start of convenience methods WITHOUT className and methodName
|
jaroslav@1258
|
549 |
//================================================================
|
jaroslav@1258
|
550 |
|
jaroslav@1258
|
551 |
/**
|
jaroslav@1258
|
552 |
* Log a message, with no arguments.
|
jaroslav@1258
|
553 |
* <p>
|
jaroslav@1258
|
554 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
555 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
556 |
* registered output Handler objects.
|
jaroslav@1258
|
557 |
* <p>
|
jaroslav@1258
|
558 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
559 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
560 |
*/
|
jaroslav@1258
|
561 |
public void log(Level level, String msg) {
|
jaroslav@1258
|
562 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
563 |
return;
|
jaroslav@1258
|
564 |
}
|
jaroslav@1258
|
565 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
566 |
doLog(lr);
|
jaroslav@1258
|
567 |
}
|
jaroslav@1258
|
568 |
|
jaroslav@1258
|
569 |
/**
|
jaroslav@1258
|
570 |
* Log a message, with one object parameter.
|
jaroslav@1258
|
571 |
* <p>
|
jaroslav@1258
|
572 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
573 |
* level then a corresponding LogRecord is created and forwarded
|
jaroslav@1258
|
574 |
* to all the registered output Handler objects.
|
jaroslav@1258
|
575 |
* <p>
|
jaroslav@1258
|
576 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
577 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
578 |
* @param param1 parameter to the message
|
jaroslav@1258
|
579 |
*/
|
jaroslav@1258
|
580 |
public void log(Level level, String msg, Object param1) {
|
jaroslav@1258
|
581 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
582 |
return;
|
jaroslav@1258
|
583 |
}
|
jaroslav@1258
|
584 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
585 |
Object params[] = { param1 };
|
jaroslav@1258
|
586 |
lr.setParameters(params);
|
jaroslav@1258
|
587 |
doLog(lr);
|
jaroslav@1258
|
588 |
}
|
jaroslav@1258
|
589 |
|
jaroslav@1258
|
590 |
/**
|
jaroslav@1258
|
591 |
* Log a message, with an array of object arguments.
|
jaroslav@1258
|
592 |
* <p>
|
jaroslav@1258
|
593 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
594 |
* level then a corresponding LogRecord is created and forwarded
|
jaroslav@1258
|
595 |
* to all the registered output Handler objects.
|
jaroslav@1258
|
596 |
* <p>
|
jaroslav@1258
|
597 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
598 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
599 |
* @param params array of parameters to the message
|
jaroslav@1258
|
600 |
*/
|
jaroslav@1258
|
601 |
public void log(Level level, String msg, Object params[]) {
|
jaroslav@1258
|
602 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
603 |
return;
|
jaroslav@1258
|
604 |
}
|
jaroslav@1258
|
605 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
606 |
lr.setParameters(params);
|
jaroslav@1258
|
607 |
doLog(lr);
|
jaroslav@1258
|
608 |
}
|
jaroslav@1258
|
609 |
|
jaroslav@1258
|
610 |
/**
|
jaroslav@1258
|
611 |
* Log a message, with associated Throwable information.
|
jaroslav@1258
|
612 |
* <p>
|
jaroslav@1258
|
613 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
614 |
* level then the given arguments are stored in a LogRecord
|
jaroslav@1258
|
615 |
* which is forwarded to all registered output handlers.
|
jaroslav@1258
|
616 |
* <p>
|
jaroslav@1258
|
617 |
* Note that the thrown argument is stored in the LogRecord thrown
|
jaroslav@1258
|
618 |
* property, rather than the LogRecord parameters property. Thus is it
|
jaroslav@1258
|
619 |
* processed specially by output Formatters and is not treated
|
jaroslav@1258
|
620 |
* as a formatting parameter to the LogRecord message property.
|
jaroslav@1258
|
621 |
* <p>
|
jaroslav@1258
|
622 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
623 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
624 |
* @param thrown Throwable associated with log message.
|
jaroslav@1258
|
625 |
*/
|
jaroslav@1258
|
626 |
public void log(Level level, String msg, Throwable thrown) {
|
jaroslav@1258
|
627 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
628 |
return;
|
jaroslav@1258
|
629 |
}
|
jaroslav@1258
|
630 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
631 |
lr.setThrown(thrown);
|
jaroslav@1258
|
632 |
doLog(lr);
|
jaroslav@1258
|
633 |
}
|
jaroslav@1258
|
634 |
|
jaroslav@1258
|
635 |
//================================================================
|
jaroslav@1258
|
636 |
// Start of convenience methods WITH className and methodName
|
jaroslav@1258
|
637 |
//================================================================
|
jaroslav@1258
|
638 |
|
jaroslav@1258
|
639 |
/**
|
jaroslav@1258
|
640 |
* Log a message, specifying source class and method,
|
jaroslav@1258
|
641 |
* with no arguments.
|
jaroslav@1258
|
642 |
* <p>
|
jaroslav@1258
|
643 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
644 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
645 |
* registered output Handler objects.
|
jaroslav@1258
|
646 |
* <p>
|
jaroslav@1258
|
647 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
648 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
649 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
650 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
651 |
*/
|
jaroslav@1258
|
652 |
public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
|
jaroslav@1258
|
653 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
654 |
return;
|
jaroslav@1258
|
655 |
}
|
jaroslav@1258
|
656 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
657 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
658 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
659 |
doLog(lr);
|
jaroslav@1258
|
660 |
}
|
jaroslav@1258
|
661 |
|
jaroslav@1258
|
662 |
/**
|
jaroslav@1258
|
663 |
* Log a message, specifying source class and method,
|
jaroslav@1258
|
664 |
* with a single object parameter to the log message.
|
jaroslav@1258
|
665 |
* <p>
|
jaroslav@1258
|
666 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
667 |
* level then a corresponding LogRecord is created and forwarded
|
jaroslav@1258
|
668 |
* to all the registered output Handler objects.
|
jaroslav@1258
|
669 |
* <p>
|
jaroslav@1258
|
670 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
671 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
672 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
673 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
674 |
* @param param1 Parameter to the log message.
|
jaroslav@1258
|
675 |
*/
|
jaroslav@1258
|
676 |
public void logp(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
677 |
String msg, Object param1) {
|
jaroslav@1258
|
678 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
679 |
return;
|
jaroslav@1258
|
680 |
}
|
jaroslav@1258
|
681 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
682 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
683 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
684 |
Object params[] = { param1 };
|
jaroslav@1258
|
685 |
lr.setParameters(params);
|
jaroslav@1258
|
686 |
doLog(lr);
|
jaroslav@1258
|
687 |
}
|
jaroslav@1258
|
688 |
|
jaroslav@1258
|
689 |
/**
|
jaroslav@1258
|
690 |
* Log a message, specifying source class and method,
|
jaroslav@1258
|
691 |
* with an array of object arguments.
|
jaroslav@1258
|
692 |
* <p>
|
jaroslav@1258
|
693 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
694 |
* level then a corresponding LogRecord is created and forwarded
|
jaroslav@1258
|
695 |
* to all the registered output Handler objects.
|
jaroslav@1258
|
696 |
* <p>
|
jaroslav@1258
|
697 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
698 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
699 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
700 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
701 |
* @param params Array of parameters to the message
|
jaroslav@1258
|
702 |
*/
|
jaroslav@1258
|
703 |
public void logp(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
704 |
String msg, Object params[]) {
|
jaroslav@1258
|
705 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
706 |
return;
|
jaroslav@1258
|
707 |
}
|
jaroslav@1258
|
708 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
709 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
710 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
711 |
lr.setParameters(params);
|
jaroslav@1258
|
712 |
doLog(lr);
|
jaroslav@1258
|
713 |
}
|
jaroslav@1258
|
714 |
|
jaroslav@1258
|
715 |
/**
|
jaroslav@1258
|
716 |
* Log a message, specifying source class and method,
|
jaroslav@1258
|
717 |
* with associated Throwable information.
|
jaroslav@1258
|
718 |
* <p>
|
jaroslav@1258
|
719 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
720 |
* level then the given arguments are stored in a LogRecord
|
jaroslav@1258
|
721 |
* which is forwarded to all registered output handlers.
|
jaroslav@1258
|
722 |
* <p>
|
jaroslav@1258
|
723 |
* Note that the thrown argument is stored in the LogRecord thrown
|
jaroslav@1258
|
724 |
* property, rather than the LogRecord parameters property. Thus is it
|
jaroslav@1258
|
725 |
* processed specially by output Formatters and is not treated
|
jaroslav@1258
|
726 |
* as a formatting parameter to the LogRecord message property.
|
jaroslav@1258
|
727 |
* <p>
|
jaroslav@1258
|
728 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
729 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
730 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
731 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
732 |
* @param thrown Throwable associated with log message.
|
jaroslav@1258
|
733 |
*/
|
jaroslav@1258
|
734 |
public void logp(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
735 |
String msg, Throwable thrown) {
|
jaroslav@1258
|
736 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
737 |
return;
|
jaroslav@1258
|
738 |
}
|
jaroslav@1258
|
739 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
740 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
741 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
742 |
lr.setThrown(thrown);
|
jaroslav@1258
|
743 |
doLog(lr);
|
jaroslav@1258
|
744 |
}
|
jaroslav@1258
|
745 |
|
jaroslav@1258
|
746 |
|
jaroslav@1258
|
747 |
//=========================================================================
|
jaroslav@1258
|
748 |
// Start of convenience methods WITH className, methodName and bundle name.
|
jaroslav@1258
|
749 |
//=========================================================================
|
jaroslav@1258
|
750 |
|
jaroslav@1258
|
751 |
// Private support method for logging for "logrb" methods.
|
jaroslav@1258
|
752 |
// We fill in the logger name, resource bundle name, and
|
jaroslav@1258
|
753 |
// resource bundle and then call "void log(LogRecord)".
|
jaroslav@1258
|
754 |
private void doLog(LogRecord lr, String rbname) {
|
jaroslav@1258
|
755 |
lr.setLoggerName(name);
|
jaroslav@1258
|
756 |
if (rbname != null) {
|
jaroslav@1258
|
757 |
lr.setResourceBundleName(rbname);
|
jaroslav@1258
|
758 |
lr.setResourceBundle(findResourceBundle(rbname));
|
jaroslav@1258
|
759 |
}
|
jaroslav@1258
|
760 |
log(lr);
|
jaroslav@1258
|
761 |
}
|
jaroslav@1258
|
762 |
|
jaroslav@1258
|
763 |
/**
|
jaroslav@1258
|
764 |
* Log a message, specifying source class, method, and resource bundle name
|
jaroslav@1258
|
765 |
* with no arguments.
|
jaroslav@1258
|
766 |
* <p>
|
jaroslav@1258
|
767 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
768 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
769 |
* registered output Handler objects.
|
jaroslav@1258
|
770 |
* <p>
|
jaroslav@1258
|
771 |
* The msg string is localized using the named resource bundle. If the
|
jaroslav@1258
|
772 |
* resource bundle name is null, or an empty String or invalid
|
jaroslav@1258
|
773 |
* then the msg string is not localized.
|
jaroslav@1258
|
774 |
* <p>
|
jaroslav@1258
|
775 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
776 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
777 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
778 |
* @param bundleName name of resource bundle to localize msg,
|
jaroslav@1258
|
779 |
* can be null
|
jaroslav@1258
|
780 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
781 |
*/
|
jaroslav@1258
|
782 |
|
jaroslav@1258
|
783 |
public void logrb(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
784 |
String bundleName, String msg) {
|
jaroslav@1258
|
785 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
786 |
return;
|
jaroslav@1258
|
787 |
}
|
jaroslav@1258
|
788 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
789 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
790 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
791 |
doLog(lr, bundleName);
|
jaroslav@1258
|
792 |
}
|
jaroslav@1258
|
793 |
|
jaroslav@1258
|
794 |
/**
|
jaroslav@1258
|
795 |
* Log a message, specifying source class, method, and resource bundle name,
|
jaroslav@1258
|
796 |
* with a single object parameter to the log message.
|
jaroslav@1258
|
797 |
* <p>
|
jaroslav@1258
|
798 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
799 |
* level then a corresponding LogRecord is created and forwarded
|
jaroslav@1258
|
800 |
* to all the registered output Handler objects.
|
jaroslav@1258
|
801 |
* <p>
|
jaroslav@1258
|
802 |
* The msg string is localized using the named resource bundle. If the
|
jaroslav@1258
|
803 |
* resource bundle name is null, or an empty String or invalid
|
jaroslav@1258
|
804 |
* then the msg string is not localized.
|
jaroslav@1258
|
805 |
* <p>
|
jaroslav@1258
|
806 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
807 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
808 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
809 |
* @param bundleName name of resource bundle to localize msg,
|
jaroslav@1258
|
810 |
* can be null
|
jaroslav@1258
|
811 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
812 |
* @param param1 Parameter to the log message.
|
jaroslav@1258
|
813 |
*/
|
jaroslav@1258
|
814 |
public void logrb(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
815 |
String bundleName, String msg, Object param1) {
|
jaroslav@1258
|
816 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
817 |
return;
|
jaroslav@1258
|
818 |
}
|
jaroslav@1258
|
819 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
820 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
821 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
822 |
Object params[] = { param1 };
|
jaroslav@1258
|
823 |
lr.setParameters(params);
|
jaroslav@1258
|
824 |
doLog(lr, bundleName);
|
jaroslav@1258
|
825 |
}
|
jaroslav@1258
|
826 |
|
jaroslav@1258
|
827 |
/**
|
jaroslav@1258
|
828 |
* Log a message, specifying source class, method, and resource bundle name,
|
jaroslav@1258
|
829 |
* with an array of object arguments.
|
jaroslav@1258
|
830 |
* <p>
|
jaroslav@1258
|
831 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
832 |
* level then a corresponding LogRecord is created and forwarded
|
jaroslav@1258
|
833 |
* to all the registered output Handler objects.
|
jaroslav@1258
|
834 |
* <p>
|
jaroslav@1258
|
835 |
* The msg string is localized using the named resource bundle. If the
|
jaroslav@1258
|
836 |
* resource bundle name is null, or an empty String or invalid
|
jaroslav@1258
|
837 |
* then the msg string is not localized.
|
jaroslav@1258
|
838 |
* <p>
|
jaroslav@1258
|
839 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
840 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
841 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
842 |
* @param bundleName name of resource bundle to localize msg,
|
jaroslav@1258
|
843 |
* can be null.
|
jaroslav@1258
|
844 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
845 |
* @param params Array of parameters to the message
|
jaroslav@1258
|
846 |
*/
|
jaroslav@1258
|
847 |
public void logrb(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
848 |
String bundleName, String msg, Object params[]) {
|
jaroslav@1258
|
849 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
850 |
return;
|
jaroslav@1258
|
851 |
}
|
jaroslav@1258
|
852 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
853 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
854 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
855 |
lr.setParameters(params);
|
jaroslav@1258
|
856 |
doLog(lr, bundleName);
|
jaroslav@1258
|
857 |
}
|
jaroslav@1258
|
858 |
|
jaroslav@1258
|
859 |
/**
|
jaroslav@1258
|
860 |
* Log a message, specifying source class, method, and resource bundle name,
|
jaroslav@1258
|
861 |
* with associated Throwable information.
|
jaroslav@1258
|
862 |
* <p>
|
jaroslav@1258
|
863 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
864 |
* level then the given arguments are stored in a LogRecord
|
jaroslav@1258
|
865 |
* which is forwarded to all registered output handlers.
|
jaroslav@1258
|
866 |
* <p>
|
jaroslav@1258
|
867 |
* The msg string is localized using the named resource bundle. If the
|
jaroslav@1258
|
868 |
* resource bundle name is null, or an empty String or invalid
|
jaroslav@1258
|
869 |
* then the msg string is not localized.
|
jaroslav@1258
|
870 |
* <p>
|
jaroslav@1258
|
871 |
* Note that the thrown argument is stored in the LogRecord thrown
|
jaroslav@1258
|
872 |
* property, rather than the LogRecord parameters property. Thus is it
|
jaroslav@1258
|
873 |
* processed specially by output Formatters and is not treated
|
jaroslav@1258
|
874 |
* as a formatting parameter to the LogRecord message property.
|
jaroslav@1258
|
875 |
* <p>
|
jaroslav@1258
|
876 |
* @param level One of the message level identifiers, e.g., SEVERE
|
jaroslav@1258
|
877 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
878 |
* @param sourceMethod name of method that issued the logging request
|
jaroslav@1258
|
879 |
* @param bundleName name of resource bundle to localize msg,
|
jaroslav@1258
|
880 |
* can be null
|
jaroslav@1258
|
881 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
882 |
* @param thrown Throwable associated with log message.
|
jaroslav@1258
|
883 |
*/
|
jaroslav@1258
|
884 |
public void logrb(Level level, String sourceClass, String sourceMethod,
|
jaroslav@1258
|
885 |
String bundleName, String msg, Throwable thrown) {
|
jaroslav@1258
|
886 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
887 |
return;
|
jaroslav@1258
|
888 |
}
|
jaroslav@1258
|
889 |
LogRecord lr = new LogRecord(level, msg);
|
jaroslav@1258
|
890 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
891 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
892 |
lr.setThrown(thrown);
|
jaroslav@1258
|
893 |
doLog(lr, bundleName);
|
jaroslav@1258
|
894 |
}
|
jaroslav@1258
|
895 |
|
jaroslav@1258
|
896 |
|
jaroslav@1258
|
897 |
//======================================================================
|
jaroslav@1258
|
898 |
// Start of convenience methods for logging method entries and returns.
|
jaroslav@1258
|
899 |
//======================================================================
|
jaroslav@1258
|
900 |
|
jaroslav@1258
|
901 |
/**
|
jaroslav@1258
|
902 |
* Log a method entry.
|
jaroslav@1258
|
903 |
* <p>
|
jaroslav@1258
|
904 |
* This is a convenience method that can be used to log entry
|
jaroslav@1258
|
905 |
* to a method. A LogRecord with message "ENTRY", log level
|
jaroslav@1258
|
906 |
* FINER, and the given sourceMethod and sourceClass is logged.
|
jaroslav@1258
|
907 |
* <p>
|
jaroslav@1258
|
908 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
909 |
* @param sourceMethod name of method that is being entered
|
jaroslav@1258
|
910 |
*/
|
jaroslav@1258
|
911 |
public void entering(String sourceClass, String sourceMethod) {
|
jaroslav@1258
|
912 |
if (Level.FINER.intValue() < levelValue) {
|
jaroslav@1258
|
913 |
return;
|
jaroslav@1258
|
914 |
}
|
jaroslav@1258
|
915 |
logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
|
jaroslav@1258
|
916 |
}
|
jaroslav@1258
|
917 |
|
jaroslav@1258
|
918 |
/**
|
jaroslav@1258
|
919 |
* Log a method entry, with one parameter.
|
jaroslav@1258
|
920 |
* <p>
|
jaroslav@1258
|
921 |
* This is a convenience method that can be used to log entry
|
jaroslav@1258
|
922 |
* to a method. A LogRecord with message "ENTRY {0}", log level
|
jaroslav@1258
|
923 |
* FINER, and the given sourceMethod, sourceClass, and parameter
|
jaroslav@1258
|
924 |
* is logged.
|
jaroslav@1258
|
925 |
* <p>
|
jaroslav@1258
|
926 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
927 |
* @param sourceMethod name of method that is being entered
|
jaroslav@1258
|
928 |
* @param param1 parameter to the method being entered
|
jaroslav@1258
|
929 |
*/
|
jaroslav@1258
|
930 |
public void entering(String sourceClass, String sourceMethod, Object param1) {
|
jaroslav@1258
|
931 |
if (Level.FINER.intValue() < levelValue) {
|
jaroslav@1258
|
932 |
return;
|
jaroslav@1258
|
933 |
}
|
jaroslav@1258
|
934 |
Object params[] = { param1 };
|
jaroslav@1258
|
935 |
logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
|
jaroslav@1258
|
936 |
}
|
jaroslav@1258
|
937 |
|
jaroslav@1258
|
938 |
/**
|
jaroslav@1258
|
939 |
* Log a method entry, with an array of parameters.
|
jaroslav@1258
|
940 |
* <p>
|
jaroslav@1258
|
941 |
* This is a convenience method that can be used to log entry
|
jaroslav@1258
|
942 |
* to a method. A LogRecord with message "ENTRY" (followed by a
|
jaroslav@1258
|
943 |
* format {N} indicator for each entry in the parameter array),
|
jaroslav@1258
|
944 |
* log level FINER, and the given sourceMethod, sourceClass, and
|
jaroslav@1258
|
945 |
* parameters is logged.
|
jaroslav@1258
|
946 |
* <p>
|
jaroslav@1258
|
947 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
948 |
* @param sourceMethod name of method that is being entered
|
jaroslav@1258
|
949 |
* @param params array of parameters to the method being entered
|
jaroslav@1258
|
950 |
*/
|
jaroslav@1258
|
951 |
public void entering(String sourceClass, String sourceMethod, Object params[]) {
|
jaroslav@1258
|
952 |
if (Level.FINER.intValue() < levelValue) {
|
jaroslav@1258
|
953 |
return;
|
jaroslav@1258
|
954 |
}
|
jaroslav@1258
|
955 |
String msg = "ENTRY";
|
jaroslav@1258
|
956 |
if (params == null ) {
|
jaroslav@1258
|
957 |
logp(Level.FINER, sourceClass, sourceMethod, msg);
|
jaroslav@1258
|
958 |
return;
|
jaroslav@1258
|
959 |
}
|
jaroslav@1258
|
960 |
for (int i = 0; i < params.length; i++) {
|
jaroslav@1258
|
961 |
msg = msg + " {" + i + "}";
|
jaroslav@1258
|
962 |
}
|
jaroslav@1258
|
963 |
logp(Level.FINER, sourceClass, sourceMethod, msg, params);
|
jaroslav@1258
|
964 |
}
|
jaroslav@1258
|
965 |
|
jaroslav@1258
|
966 |
/**
|
jaroslav@1258
|
967 |
* Log a method return.
|
jaroslav@1258
|
968 |
* <p>
|
jaroslav@1258
|
969 |
* This is a convenience method that can be used to log returning
|
jaroslav@1258
|
970 |
* from a method. A LogRecord with message "RETURN", log level
|
jaroslav@1258
|
971 |
* FINER, and the given sourceMethod and sourceClass is logged.
|
jaroslav@1258
|
972 |
* <p>
|
jaroslav@1258
|
973 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
974 |
* @param sourceMethod name of the method
|
jaroslav@1258
|
975 |
*/
|
jaroslav@1258
|
976 |
public void exiting(String sourceClass, String sourceMethod) {
|
jaroslav@1258
|
977 |
if (Level.FINER.intValue() < levelValue) {
|
jaroslav@1258
|
978 |
return;
|
jaroslav@1258
|
979 |
}
|
jaroslav@1258
|
980 |
logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
|
jaroslav@1258
|
981 |
}
|
jaroslav@1258
|
982 |
|
jaroslav@1258
|
983 |
|
jaroslav@1258
|
984 |
/**
|
jaroslav@1258
|
985 |
* Log a method return, with result object.
|
jaroslav@1258
|
986 |
* <p>
|
jaroslav@1258
|
987 |
* This is a convenience method that can be used to log returning
|
jaroslav@1258
|
988 |
* from a method. A LogRecord with message "RETURN {0}", log level
|
jaroslav@1258
|
989 |
* FINER, and the gives sourceMethod, sourceClass, and result
|
jaroslav@1258
|
990 |
* object is logged.
|
jaroslav@1258
|
991 |
* <p>
|
jaroslav@1258
|
992 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
993 |
* @param sourceMethod name of the method
|
jaroslav@1258
|
994 |
* @param result Object that is being returned
|
jaroslav@1258
|
995 |
*/
|
jaroslav@1258
|
996 |
public void exiting(String sourceClass, String sourceMethod, Object result) {
|
jaroslav@1258
|
997 |
if (Level.FINER.intValue() < levelValue) {
|
jaroslav@1258
|
998 |
return;
|
jaroslav@1258
|
999 |
}
|
jaroslav@1258
|
1000 |
Object params[] = { result };
|
jaroslav@1258
|
1001 |
logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
|
jaroslav@1258
|
1002 |
}
|
jaroslav@1258
|
1003 |
|
jaroslav@1258
|
1004 |
/**
|
jaroslav@1258
|
1005 |
* Log throwing an exception.
|
jaroslav@1258
|
1006 |
* <p>
|
jaroslav@1258
|
1007 |
* This is a convenience method to log that a method is
|
jaroslav@1258
|
1008 |
* terminating by throwing an exception. The logging is done
|
jaroslav@1258
|
1009 |
* using the FINER level.
|
jaroslav@1258
|
1010 |
* <p>
|
jaroslav@1258
|
1011 |
* If the logger is currently enabled for the given message
|
jaroslav@1258
|
1012 |
* level then the given arguments are stored in a LogRecord
|
jaroslav@1258
|
1013 |
* which is forwarded to all registered output handlers. The
|
jaroslav@1258
|
1014 |
* LogRecord's message is set to "THROW".
|
jaroslav@1258
|
1015 |
* <p>
|
jaroslav@1258
|
1016 |
* Note that the thrown argument is stored in the LogRecord thrown
|
jaroslav@1258
|
1017 |
* property, rather than the LogRecord parameters property. Thus is it
|
jaroslav@1258
|
1018 |
* processed specially by output Formatters and is not treated
|
jaroslav@1258
|
1019 |
* as a formatting parameter to the LogRecord message property.
|
jaroslav@1258
|
1020 |
* <p>
|
jaroslav@1258
|
1021 |
* @param sourceClass name of class that issued the logging request
|
jaroslav@1258
|
1022 |
* @param sourceMethod name of the method.
|
jaroslav@1258
|
1023 |
* @param thrown The Throwable that is being thrown.
|
jaroslav@1258
|
1024 |
*/
|
jaroslav@1258
|
1025 |
public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
|
jaroslav@1258
|
1026 |
if (Level.FINER.intValue() < levelValue || levelValue == offValue ) {
|
jaroslav@1258
|
1027 |
return;
|
jaroslav@1258
|
1028 |
}
|
jaroslav@1258
|
1029 |
LogRecord lr = new LogRecord(Level.FINER, "THROW");
|
jaroslav@1258
|
1030 |
lr.setSourceClassName(sourceClass);
|
jaroslav@1258
|
1031 |
lr.setSourceMethodName(sourceMethod);
|
jaroslav@1258
|
1032 |
lr.setThrown(thrown);
|
jaroslav@1258
|
1033 |
doLog(lr);
|
jaroslav@1258
|
1034 |
}
|
jaroslav@1258
|
1035 |
|
jaroslav@1258
|
1036 |
//=======================================================================
|
jaroslav@1258
|
1037 |
// Start of simple convenience methods using level names as method names
|
jaroslav@1258
|
1038 |
//=======================================================================
|
jaroslav@1258
|
1039 |
|
jaroslav@1258
|
1040 |
/**
|
jaroslav@1258
|
1041 |
* Log a SEVERE message.
|
jaroslav@1258
|
1042 |
* <p>
|
jaroslav@1258
|
1043 |
* If the logger is currently enabled for the SEVERE message
|
jaroslav@1258
|
1044 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1045 |
* registered output Handler objects.
|
jaroslav@1258
|
1046 |
* <p>
|
jaroslav@1258
|
1047 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1048 |
*/
|
jaroslav@1258
|
1049 |
public void severe(String msg) {
|
jaroslav@1258
|
1050 |
if (Level.SEVERE.intValue() < levelValue) {
|
jaroslav@1258
|
1051 |
return;
|
jaroslav@1258
|
1052 |
}
|
jaroslav@1258
|
1053 |
log(Level.SEVERE, msg);
|
jaroslav@1258
|
1054 |
}
|
jaroslav@1258
|
1055 |
|
jaroslav@1258
|
1056 |
/**
|
jaroslav@1258
|
1057 |
* Log a WARNING message.
|
jaroslav@1258
|
1058 |
* <p>
|
jaroslav@1258
|
1059 |
* If the logger is currently enabled for the WARNING message
|
jaroslav@1258
|
1060 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1061 |
* registered output Handler objects.
|
jaroslav@1258
|
1062 |
* <p>
|
jaroslav@1258
|
1063 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1064 |
*/
|
jaroslav@1258
|
1065 |
public void warning(String msg) {
|
jaroslav@1258
|
1066 |
if (Level.WARNING.intValue() < levelValue) {
|
jaroslav@1258
|
1067 |
return;
|
jaroslav@1258
|
1068 |
}
|
jaroslav@1258
|
1069 |
log(Level.WARNING, msg);
|
jaroslav@1258
|
1070 |
}
|
jaroslav@1258
|
1071 |
|
jaroslav@1258
|
1072 |
/**
|
jaroslav@1258
|
1073 |
* Log an INFO message.
|
jaroslav@1258
|
1074 |
* <p>
|
jaroslav@1258
|
1075 |
* If the logger is currently enabled for the INFO message
|
jaroslav@1258
|
1076 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1077 |
* registered output Handler objects.
|
jaroslav@1258
|
1078 |
* <p>
|
jaroslav@1258
|
1079 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1080 |
*/
|
jaroslav@1258
|
1081 |
public void info(String msg) {
|
jaroslav@1258
|
1082 |
if (Level.INFO.intValue() < levelValue) {
|
jaroslav@1258
|
1083 |
return;
|
jaroslav@1258
|
1084 |
}
|
jaroslav@1258
|
1085 |
log(Level.INFO, msg);
|
jaroslav@1258
|
1086 |
}
|
jaroslav@1258
|
1087 |
|
jaroslav@1258
|
1088 |
/**
|
jaroslav@1258
|
1089 |
* Log a CONFIG message.
|
jaroslav@1258
|
1090 |
* <p>
|
jaroslav@1258
|
1091 |
* If the logger is currently enabled for the CONFIG message
|
jaroslav@1258
|
1092 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1093 |
* registered output Handler objects.
|
jaroslav@1258
|
1094 |
* <p>
|
jaroslav@1258
|
1095 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1096 |
*/
|
jaroslav@1258
|
1097 |
public void config(String msg) {
|
jaroslav@1258
|
1098 |
if (Level.CONFIG.intValue() < levelValue) {
|
jaroslav@1258
|
1099 |
return;
|
jaroslav@1258
|
1100 |
}
|
jaroslav@1258
|
1101 |
log(Level.CONFIG, msg);
|
jaroslav@1258
|
1102 |
}
|
jaroslav@1258
|
1103 |
|
jaroslav@1258
|
1104 |
/**
|
jaroslav@1258
|
1105 |
* Log a FINE message.
|
jaroslav@1258
|
1106 |
* <p>
|
jaroslav@1258
|
1107 |
* If the logger is currently enabled for the FINE message
|
jaroslav@1258
|
1108 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1109 |
* registered output Handler objects.
|
jaroslav@1258
|
1110 |
* <p>
|
jaroslav@1258
|
1111 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1112 |
*/
|
jaroslav@1258
|
1113 |
public void fine(String msg) {
|
jaroslav@1258
|
1114 |
if (Level.FINE.intValue() < levelValue) {
|
jaroslav@1258
|
1115 |
return;
|
jaroslav@1258
|
1116 |
}
|
jaroslav@1258
|
1117 |
log(Level.FINE, msg);
|
jaroslav@1258
|
1118 |
}
|
jaroslav@1258
|
1119 |
|
jaroslav@1258
|
1120 |
/**
|
jaroslav@1258
|
1121 |
* Log a FINER message.
|
jaroslav@1258
|
1122 |
* <p>
|
jaroslav@1258
|
1123 |
* If the logger is currently enabled for the FINER message
|
jaroslav@1258
|
1124 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1125 |
* registered output Handler objects.
|
jaroslav@1258
|
1126 |
* <p>
|
jaroslav@1258
|
1127 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1128 |
*/
|
jaroslav@1258
|
1129 |
public void finer(String msg) {
|
jaroslav@1258
|
1130 |
if (Level.FINER.intValue() < levelValue) {
|
jaroslav@1258
|
1131 |
return;
|
jaroslav@1258
|
1132 |
}
|
jaroslav@1258
|
1133 |
log(Level.FINER, msg);
|
jaroslav@1258
|
1134 |
}
|
jaroslav@1258
|
1135 |
|
jaroslav@1258
|
1136 |
/**
|
jaroslav@1258
|
1137 |
* Log a FINEST message.
|
jaroslav@1258
|
1138 |
* <p>
|
jaroslav@1258
|
1139 |
* If the logger is currently enabled for the FINEST message
|
jaroslav@1258
|
1140 |
* level then the given message is forwarded to all the
|
jaroslav@1258
|
1141 |
* registered output Handler objects.
|
jaroslav@1258
|
1142 |
* <p>
|
jaroslav@1258
|
1143 |
* @param msg The string message (or a key in the message catalog)
|
jaroslav@1258
|
1144 |
*/
|
jaroslav@1258
|
1145 |
public void finest(String msg) {
|
jaroslav@1258
|
1146 |
if (Level.FINEST.intValue() < levelValue) {
|
jaroslav@1258
|
1147 |
return;
|
jaroslav@1258
|
1148 |
}
|
jaroslav@1258
|
1149 |
log(Level.FINEST, msg);
|
jaroslav@1258
|
1150 |
}
|
jaroslav@1258
|
1151 |
|
jaroslav@1258
|
1152 |
//================================================================
|
jaroslav@1258
|
1153 |
// End of convenience methods
|
jaroslav@1258
|
1154 |
//================================================================
|
jaroslav@1258
|
1155 |
|
jaroslav@1258
|
1156 |
/**
|
jaroslav@1258
|
1157 |
* Set the log level specifying which message levels will be
|
jaroslav@1258
|
1158 |
* logged by this logger. Message levels lower than this
|
jaroslav@1258
|
1159 |
* value will be discarded. The level value Level.OFF
|
jaroslav@1258
|
1160 |
* can be used to turn off logging.
|
jaroslav@1258
|
1161 |
* <p>
|
jaroslav@1258
|
1162 |
* If the new level is null, it means that this node should
|
jaroslav@1258
|
1163 |
* inherit its level from its nearest ancestor with a specific
|
jaroslav@1258
|
1164 |
* (non-null) level value.
|
jaroslav@1258
|
1165 |
*
|
jaroslav@1258
|
1166 |
* @param newLevel the new value for the log level (may be null)
|
jaroslav@1258
|
1167 |
* @exception SecurityException if a security manager exists and if
|
jaroslav@1258
|
1168 |
* the caller does not have LoggingPermission("control").
|
jaroslav@1258
|
1169 |
*/
|
jaroslav@1258
|
1170 |
public void setLevel(Level newLevel) throws SecurityException {
|
jaroslav@1258
|
1171 |
checkAccess();
|
jaroslav@1258
|
1172 |
synchronized (treeLock) {
|
jaroslav@1258
|
1173 |
levelObject = newLevel;
|
jaroslav@1258
|
1174 |
updateEffectiveLevel();
|
jaroslav@1258
|
1175 |
}
|
jaroslav@1258
|
1176 |
}
|
jaroslav@1258
|
1177 |
|
jaroslav@1258
|
1178 |
/**
|
jaroslav@1258
|
1179 |
* Get the log Level that has been specified for this Logger.
|
jaroslav@1258
|
1180 |
* The result may be null, which means that this logger's
|
jaroslav@1258
|
1181 |
* effective level will be inherited from its parent.
|
jaroslav@1258
|
1182 |
*
|
jaroslav@1258
|
1183 |
* @return this Logger's level
|
jaroslav@1258
|
1184 |
*/
|
jaroslav@1258
|
1185 |
public Level getLevel() {
|
jaroslav@1258
|
1186 |
return levelObject;
|
jaroslav@1258
|
1187 |
}
|
jaroslav@1258
|
1188 |
|
jaroslav@1258
|
1189 |
/**
|
jaroslav@1258
|
1190 |
* Check if a message of the given level would actually be logged
|
jaroslav@1258
|
1191 |
* by this logger. This check is based on the Loggers effective level,
|
jaroslav@1258
|
1192 |
* which may be inherited from its parent.
|
jaroslav@1258
|
1193 |
*
|
jaroslav@1258
|
1194 |
* @param level a message logging level
|
jaroslav@1258
|
1195 |
* @return true if the given message level is currently being logged.
|
jaroslav@1258
|
1196 |
*/
|
jaroslav@1258
|
1197 |
public boolean isLoggable(Level level) {
|
jaroslav@1258
|
1198 |
if (level.intValue() < levelValue || levelValue == offValue) {
|
jaroslav@1258
|
1199 |
return false;
|
jaroslav@1258
|
1200 |
}
|
jaroslav@1258
|
1201 |
return true;
|
jaroslav@1258
|
1202 |
}
|
jaroslav@1258
|
1203 |
|
jaroslav@1258
|
1204 |
/**
|
jaroslav@1258
|
1205 |
* Get the name for this logger.
|
jaroslav@1258
|
1206 |
* @return logger name. Will be null for anonymous Loggers.
|
jaroslav@1258
|
1207 |
*/
|
jaroslav@1258
|
1208 |
public String getName() {
|
jaroslav@1258
|
1209 |
return name;
|
jaroslav@1258
|
1210 |
}
|
jaroslav@1258
|
1211 |
|
jaroslav@1258
|
1212 |
/**
|
jaroslav@1258
|
1213 |
* Add a log Handler to receive logging messages.
|
jaroslav@1258
|
1214 |
* <p>
|
jaroslav@1258
|
1215 |
* By default, Loggers also send their output to their parent logger.
|
jaroslav@1258
|
1216 |
* Typically the root Logger is configured with a set of Handlers
|
jaroslav@1258
|
1217 |
* that essentially act as default handlers for all loggers.
|
jaroslav@1258
|
1218 |
*
|
jaroslav@1258
|
1219 |
* @param handler a logging Handler
|
jaroslav@1258
|
1220 |
* @exception SecurityException if a security manager exists and if
|
jaroslav@1258
|
1221 |
* the caller does not have LoggingPermission("control").
|
jaroslav@1258
|
1222 |
*/
|
jaroslav@1258
|
1223 |
public void addHandler(Handler handler) throws SecurityException {
|
jaroslav@1258
|
1224 |
// Check for null handler
|
jaroslav@1258
|
1225 |
handler.getClass();
|
jaroslav@1258
|
1226 |
checkAccess();
|
jaroslav@1258
|
1227 |
handlers.add(handler);
|
jaroslav@1258
|
1228 |
}
|
jaroslav@1258
|
1229 |
|
jaroslav@1258
|
1230 |
/**
|
jaroslav@1258
|
1231 |
* Remove a log Handler.
|
jaroslav@1258
|
1232 |
* <P>
|
jaroslav@1258
|
1233 |
* Returns silently if the given Handler is not found or is null
|
jaroslav@1258
|
1234 |
*
|
jaroslav@1258
|
1235 |
* @param handler a logging Handler
|
jaroslav@1258
|
1236 |
* @exception SecurityException if a security manager exists and if
|
jaroslav@1258
|
1237 |
* the caller does not have LoggingPermission("control").
|
jaroslav@1258
|
1238 |
*/
|
jaroslav@1258
|
1239 |
public void removeHandler(Handler handler) throws SecurityException {
|
jaroslav@1258
|
1240 |
checkAccess();
|
jaroslav@1258
|
1241 |
if (handler == null) {
|
jaroslav@1258
|
1242 |
return;
|
jaroslav@1258
|
1243 |
}
|
jaroslav@1258
|
1244 |
handlers.remove(handler);
|
jaroslav@1258
|
1245 |
}
|
jaroslav@1258
|
1246 |
|
jaroslav@1258
|
1247 |
/**
|
jaroslav@1258
|
1248 |
* Get the Handlers associated with this logger.
|
jaroslav@1258
|
1249 |
* <p>
|
jaroslav@1258
|
1250 |
* @return an array of all registered Handlers
|
jaroslav@1258
|
1251 |
*/
|
jaroslav@1258
|
1252 |
public Handler[] getHandlers() {
|
jaroslav@1258
|
1253 |
return handlers.toArray(emptyHandlers);
|
jaroslav@1258
|
1254 |
}
|
jaroslav@1258
|
1255 |
|
jaroslav@1258
|
1256 |
/**
|
jaroslav@1258
|
1257 |
* Specify whether or not this logger should send its output
|
jaroslav@1258
|
1258 |
* to its parent Logger. This means that any LogRecords will
|
jaroslav@1258
|
1259 |
* also be written to the parent's Handlers, and potentially
|
jaroslav@1258
|
1260 |
* to its parent, recursively up the namespace.
|
jaroslav@1258
|
1261 |
*
|
jaroslav@1258
|
1262 |
* @param useParentHandlers true if output is to be sent to the
|
jaroslav@1258
|
1263 |
* logger's parent.
|
jaroslav@1258
|
1264 |
* @exception SecurityException if a security manager exists and if
|
jaroslav@1258
|
1265 |
* the caller does not have LoggingPermission("control").
|
jaroslav@1258
|
1266 |
*/
|
jaroslav@1258
|
1267 |
public void setUseParentHandlers(boolean useParentHandlers) {
|
jaroslav@1258
|
1268 |
checkAccess();
|
jaroslav@1258
|
1269 |
this.useParentHandlers = useParentHandlers;
|
jaroslav@1258
|
1270 |
}
|
jaroslav@1258
|
1271 |
|
jaroslav@1258
|
1272 |
/**
|
jaroslav@1258
|
1273 |
* Discover whether or not this logger is sending its output
|
jaroslav@1258
|
1274 |
* to its parent logger.
|
jaroslav@1258
|
1275 |
*
|
jaroslav@1258
|
1276 |
* @return true if output is to be sent to the logger's parent
|
jaroslav@1258
|
1277 |
*/
|
jaroslav@1258
|
1278 |
public boolean getUseParentHandlers() {
|
jaroslav@1258
|
1279 |
return useParentHandlers;
|
jaroslav@1258
|
1280 |
}
|
jaroslav@1258
|
1281 |
|
jaroslav@1258
|
1282 |
// Private utility method to map a resource bundle name to an
|
jaroslav@1258
|
1283 |
// actual resource bundle, using a simple one-entry cache.
|
jaroslav@1258
|
1284 |
// Returns null for a null name.
|
jaroslav@1258
|
1285 |
// May also return null if we can't find the resource bundle and
|
jaroslav@1258
|
1286 |
// there is no suitable previous cached value.
|
jaroslav@1258
|
1287 |
|
jaroslav@1258
|
1288 |
private synchronized ResourceBundle findResourceBundle(String name) {
|
jaroslav@1258
|
1289 |
// Return a null bundle for a null name.
|
jaroslav@1258
|
1290 |
if (name == null) {
|
jaroslav@1258
|
1291 |
return null;
|
jaroslav@1258
|
1292 |
}
|
jaroslav@1258
|
1293 |
|
jaroslav@1258
|
1294 |
Locale currentLocale = Locale.getDefault();
|
jaroslav@1258
|
1295 |
|
jaroslav@1258
|
1296 |
// Normally we should hit on our simple one entry cache.
|
jaroslav@1258
|
1297 |
if (catalog != null && currentLocale == catalogLocale
|
jaroslav@1258
|
1298 |
&& name == catalogName) {
|
jaroslav@1258
|
1299 |
return catalog;
|
jaroslav@1258
|
1300 |
}
|
jaroslav@1258
|
1301 |
|
jaroslav@1258
|
1302 |
// Use the thread's context ClassLoader. If there isn't one,
|
jaroslav@1258
|
1303 |
// use the SystemClassloader.
|
jaroslav@1258
|
1304 |
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
jaroslav@1258
|
1305 |
if (cl == null) {
|
jaroslav@1258
|
1306 |
cl = ClassLoader.getSystemClassLoader();
|
jaroslav@1258
|
1307 |
}
|
jaroslav@1258
|
1308 |
try {
|
jaroslav@1258
|
1309 |
catalog = ResourceBundle.getBundle(name, currentLocale, cl);
|
jaroslav@1258
|
1310 |
catalogName = name;
|
jaroslav@1258
|
1311 |
catalogLocale = currentLocale;
|
jaroslav@1258
|
1312 |
return catalog;
|
jaroslav@1258
|
1313 |
} catch (MissingResourceException ex) {
|
jaroslav@1258
|
1314 |
// Woops. We can't find the ResourceBundle in the default
|
jaroslav@1258
|
1315 |
// ClassLoader. Drop through.
|
jaroslav@1258
|
1316 |
}
|
jaroslav@1258
|
1317 |
|
jaroslav@1258
|
1318 |
|
jaroslav@1258
|
1319 |
// Fall back to searching up the call stack and trying each
|
jaroslav@1258
|
1320 |
// calling ClassLoader.
|
jaroslav@1258
|
1321 |
for (int ix = 0; ; ix++) {
|
jaroslav@1258
|
1322 |
Class clz = sun.reflect.Reflection.getCallerClass(ix);
|
jaroslav@1258
|
1323 |
if (clz == null) {
|
jaroslav@1258
|
1324 |
break;
|
jaroslav@1258
|
1325 |
}
|
jaroslav@1258
|
1326 |
ClassLoader cl2 = clz.getClassLoader();
|
jaroslav@1258
|
1327 |
if (cl2 == null) {
|
jaroslav@1258
|
1328 |
cl2 = ClassLoader.getSystemClassLoader();
|
jaroslav@1258
|
1329 |
}
|
jaroslav@1258
|
1330 |
if (cl == cl2) {
|
jaroslav@1258
|
1331 |
// We've already checked this classloader.
|
jaroslav@1258
|
1332 |
continue;
|
jaroslav@1258
|
1333 |
}
|
jaroslav@1258
|
1334 |
cl = cl2;
|
jaroslav@1258
|
1335 |
try {
|
jaroslav@1258
|
1336 |
catalog = ResourceBundle.getBundle(name, currentLocale, cl);
|
jaroslav@1258
|
1337 |
catalogName = name;
|
jaroslav@1258
|
1338 |
catalogLocale = currentLocale;
|
jaroslav@1258
|
1339 |
return catalog;
|
jaroslav@1258
|
1340 |
} catch (MissingResourceException ex) {
|
jaroslav@1258
|
1341 |
// Ok, this one didn't work either.
|
jaroslav@1258
|
1342 |
// Drop through, and try the next one.
|
jaroslav@1258
|
1343 |
}
|
jaroslav@1258
|
1344 |
}
|
jaroslav@1258
|
1345 |
|
jaroslav@1258
|
1346 |
if (name.equals(catalogName)) {
|
jaroslav@1258
|
1347 |
// Return the previous cached value for that name.
|
jaroslav@1258
|
1348 |
// This may be null.
|
jaroslav@1258
|
1349 |
return catalog;
|
jaroslav@1258
|
1350 |
}
|
jaroslav@1258
|
1351 |
// Sorry, we're out of luck.
|
jaroslav@1258
|
1352 |
return null;
|
jaroslav@1258
|
1353 |
}
|
jaroslav@1258
|
1354 |
|
jaroslav@1258
|
1355 |
// Private utility method to initialize our one entry
|
jaroslav@1258
|
1356 |
// resource bundle cache.
|
jaroslav@1258
|
1357 |
// Note: for consistency reasons, we are careful to check
|
jaroslav@1258
|
1358 |
// that a suitable ResourceBundle exists before setting the
|
jaroslav@1258
|
1359 |
// ResourceBundleName.
|
jaroslav@1258
|
1360 |
private synchronized void setupResourceInfo(String name) {
|
jaroslav@1258
|
1361 |
if (name == null) {
|
jaroslav@1258
|
1362 |
return;
|
jaroslav@1258
|
1363 |
}
|
jaroslav@1258
|
1364 |
ResourceBundle rb = findResourceBundle(name);
|
jaroslav@1258
|
1365 |
if (rb == null) {
|
jaroslav@1258
|
1366 |
// We've failed to find an expected ResourceBundle.
|
jaroslav@1258
|
1367 |
throw new MissingResourceException("Can't find " + name + " bundle", name, "");
|
jaroslav@1258
|
1368 |
}
|
jaroslav@1258
|
1369 |
resourceBundleName = name;
|
jaroslav@1258
|
1370 |
}
|
jaroslav@1258
|
1371 |
|
jaroslav@1258
|
1372 |
/**
|
jaroslav@1258
|
1373 |
* Return the parent for this Logger.
|
jaroslav@1258
|
1374 |
* <p>
|
jaroslav@1258
|
1375 |
* This method returns the nearest extant parent in the namespace.
|
jaroslav@1258
|
1376 |
* Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
|
jaroslav@1258
|
1377 |
* has been created but no logger "a.b.c" exists, then a call of
|
jaroslav@1258
|
1378 |
* getParent on the Logger "a.b.c.d" will return the Logger "a.b".
|
jaroslav@1258
|
1379 |
* <p>
|
jaroslav@1258
|
1380 |
* The result will be null if it is called on the root Logger
|
jaroslav@1258
|
1381 |
* in the namespace.
|
jaroslav@1258
|
1382 |
*
|
jaroslav@1258
|
1383 |
* @return nearest existing parent Logger
|
jaroslav@1258
|
1384 |
*/
|
jaroslav@1258
|
1385 |
public Logger getParent() {
|
jaroslav@1258
|
1386 |
// Note: this used to be synchronized on treeLock. However, this only
|
jaroslav@1258
|
1387 |
// provided memory semantics, as there was no guarantee that the caller
|
jaroslav@1258
|
1388 |
// would synchronize on treeLock (in fact, there is no way for external
|
jaroslav@1258
|
1389 |
// callers to so synchronize). Therefore, we have made parent volatile
|
jaroslav@1258
|
1390 |
// instead.
|
jaroslav@1258
|
1391 |
return parent;
|
jaroslav@1258
|
1392 |
}
|
jaroslav@1258
|
1393 |
|
jaroslav@1258
|
1394 |
/**
|
jaroslav@1258
|
1395 |
* Set the parent for this Logger. This method is used by
|
jaroslav@1258
|
1396 |
* the LogManager to update a Logger when the namespace changes.
|
jaroslav@1258
|
1397 |
* <p>
|
jaroslav@1258
|
1398 |
* It should not be called from application code.
|
jaroslav@1258
|
1399 |
* <p>
|
jaroslav@1258
|
1400 |
* @param parent the new parent logger
|
jaroslav@1258
|
1401 |
* @exception SecurityException if a security manager exists and if
|
jaroslav@1258
|
1402 |
* the caller does not have LoggingPermission("control").
|
jaroslav@1258
|
1403 |
*/
|
jaroslav@1258
|
1404 |
public void setParent(Logger parent) {
|
jaroslav@1258
|
1405 |
if (parent == null) {
|
jaroslav@1258
|
1406 |
throw new NullPointerException();
|
jaroslav@1258
|
1407 |
}
|
jaroslav@1258
|
1408 |
manager.checkAccess();
|
jaroslav@1258
|
1409 |
doSetParent(parent);
|
jaroslav@1258
|
1410 |
}
|
jaroslav@1258
|
1411 |
|
jaroslav@1258
|
1412 |
// Private method to do the work for parenting a child
|
jaroslav@1258
|
1413 |
// Logger onto a parent logger.
|
jaroslav@1258
|
1414 |
private void doSetParent(Logger newParent) {
|
jaroslav@1258
|
1415 |
|
jaroslav@1258
|
1416 |
// System.err.println("doSetParent \"" + getName() + "\" \""
|
jaroslav@1258
|
1417 |
// + newParent.getName() + "\"");
|
jaroslav@1258
|
1418 |
|
jaroslav@1258
|
1419 |
synchronized (treeLock) {
|
jaroslav@1258
|
1420 |
|
jaroslav@1258
|
1421 |
// Remove ourself from any previous parent.
|
jaroslav@1258
|
1422 |
LogManager.LoggerWeakRef ref = null;
|
jaroslav@1258
|
1423 |
if (parent != null) {
|
jaroslav@1258
|
1424 |
// assert parent.kids != null;
|
jaroslav@1258
|
1425 |
for (Iterator<LogManager.LoggerWeakRef> iter = parent.kids.iterator(); iter.hasNext(); ) {
|
jaroslav@1258
|
1426 |
ref = iter.next();
|
jaroslav@1258
|
1427 |
Logger kid = ref.get();
|
jaroslav@1258
|
1428 |
if (kid == this) {
|
jaroslav@1258
|
1429 |
// ref is used down below to complete the reparenting
|
jaroslav@1258
|
1430 |
iter.remove();
|
jaroslav@1258
|
1431 |
break;
|
jaroslav@1258
|
1432 |
} else {
|
jaroslav@1258
|
1433 |
ref = null;
|
jaroslav@1258
|
1434 |
}
|
jaroslav@1258
|
1435 |
}
|
jaroslav@1258
|
1436 |
// We have now removed ourself from our parents' kids.
|
jaroslav@1258
|
1437 |
}
|
jaroslav@1258
|
1438 |
|
jaroslav@1258
|
1439 |
// Set our new parent.
|
jaroslav@1258
|
1440 |
parent = newParent;
|
jaroslav@1258
|
1441 |
if (parent.kids == null) {
|
jaroslav@1258
|
1442 |
parent.kids = new ArrayList<>(2);
|
jaroslav@1258
|
1443 |
}
|
jaroslav@1258
|
1444 |
if (ref == null) {
|
jaroslav@1258
|
1445 |
// we didn't have a previous parent
|
jaroslav@1258
|
1446 |
ref = manager.new LoggerWeakRef(this);
|
jaroslav@1258
|
1447 |
}
|
jaroslav@1258
|
1448 |
ref.setParentRef(new WeakReference<Logger>(parent));
|
jaroslav@1258
|
1449 |
parent.kids.add(ref);
|
jaroslav@1258
|
1450 |
|
jaroslav@1258
|
1451 |
// As a result of the reparenting, the effective level
|
jaroslav@1258
|
1452 |
// may have changed for us and our children.
|
jaroslav@1258
|
1453 |
updateEffectiveLevel();
|
jaroslav@1258
|
1454 |
|
jaroslav@1258
|
1455 |
}
|
jaroslav@1258
|
1456 |
}
|
jaroslav@1258
|
1457 |
|
jaroslav@1258
|
1458 |
// Package-level method.
|
jaroslav@1258
|
1459 |
// Remove the weak reference for the specified child Logger from the
|
jaroslav@1258
|
1460 |
// kid list. We should only be called from LoggerWeakRef.dispose().
|
jaroslav@1258
|
1461 |
final void removeChildLogger(LogManager.LoggerWeakRef child) {
|
jaroslav@1258
|
1462 |
synchronized (treeLock) {
|
jaroslav@1258
|
1463 |
for (Iterator<LogManager.LoggerWeakRef> iter = kids.iterator(); iter.hasNext(); ) {
|
jaroslav@1258
|
1464 |
LogManager.LoggerWeakRef ref = iter.next();
|
jaroslav@1258
|
1465 |
if (ref == child) {
|
jaroslav@1258
|
1466 |
iter.remove();
|
jaroslav@1258
|
1467 |
return;
|
jaroslav@1258
|
1468 |
}
|
jaroslav@1258
|
1469 |
}
|
jaroslav@1258
|
1470 |
}
|
jaroslav@1258
|
1471 |
}
|
jaroslav@1258
|
1472 |
|
jaroslav@1258
|
1473 |
// Recalculate the effective level for this node and
|
jaroslav@1258
|
1474 |
// recursively for our children.
|
jaroslav@1258
|
1475 |
|
jaroslav@1258
|
1476 |
private void updateEffectiveLevel() {
|
jaroslav@1258
|
1477 |
// assert Thread.holdsLock(treeLock);
|
jaroslav@1258
|
1478 |
|
jaroslav@1258
|
1479 |
// Figure out our current effective level.
|
jaroslav@1258
|
1480 |
int newLevelValue;
|
jaroslav@1258
|
1481 |
if (levelObject != null) {
|
jaroslav@1258
|
1482 |
newLevelValue = levelObject.intValue();
|
jaroslav@1258
|
1483 |
} else {
|
jaroslav@1258
|
1484 |
if (parent != null) {
|
jaroslav@1258
|
1485 |
newLevelValue = parent.levelValue;
|
jaroslav@1258
|
1486 |
} else {
|
jaroslav@1258
|
1487 |
// This may happen during initialization.
|
jaroslav@1258
|
1488 |
newLevelValue = Level.INFO.intValue();
|
jaroslav@1258
|
1489 |
}
|
jaroslav@1258
|
1490 |
}
|
jaroslav@1258
|
1491 |
|
jaroslav@1258
|
1492 |
// If our effective value hasn't changed, we're done.
|
jaroslav@1258
|
1493 |
if (levelValue == newLevelValue) {
|
jaroslav@1258
|
1494 |
return;
|
jaroslav@1258
|
1495 |
}
|
jaroslav@1258
|
1496 |
|
jaroslav@1258
|
1497 |
levelValue = newLevelValue;
|
jaroslav@1258
|
1498 |
|
jaroslav@1258
|
1499 |
// System.err.println("effective level: \"" + getName() + "\" := " + level);
|
jaroslav@1258
|
1500 |
|
jaroslav@1258
|
1501 |
// Recursively update the level on each of our kids.
|
jaroslav@1258
|
1502 |
if (kids != null) {
|
jaroslav@1258
|
1503 |
for (int i = 0; i < kids.size(); i++) {
|
jaroslav@1258
|
1504 |
LogManager.LoggerWeakRef ref = kids.get(i);
|
jaroslav@1258
|
1505 |
Logger kid = ref.get();
|
jaroslav@1258
|
1506 |
if (kid != null) {
|
jaroslav@1258
|
1507 |
kid.updateEffectiveLevel();
|
jaroslav@1258
|
1508 |
}
|
jaroslav@1258
|
1509 |
}
|
jaroslav@1258
|
1510 |
}
|
jaroslav@1258
|
1511 |
}
|
jaroslav@1258
|
1512 |
|
jaroslav@1258
|
1513 |
|
jaroslav@1258
|
1514 |
// Private method to get the potentially inherited
|
jaroslav@1258
|
1515 |
// resource bundle name for this Logger.
|
jaroslav@1258
|
1516 |
// May return null
|
jaroslav@1258
|
1517 |
private String getEffectiveResourceBundleName() {
|
jaroslav@1258
|
1518 |
Logger target = this;
|
jaroslav@1258
|
1519 |
while (target != null) {
|
jaroslav@1258
|
1520 |
String rbn = target.getResourceBundleName();
|
jaroslav@1258
|
1521 |
if (rbn != null) {
|
jaroslav@1258
|
1522 |
return rbn;
|
jaroslav@1258
|
1523 |
}
|
jaroslav@1258
|
1524 |
target = target.getParent();
|
jaroslav@1258
|
1525 |
}
|
jaroslav@1258
|
1526 |
return null;
|
jaroslav@1258
|
1527 |
}
|
jaroslav@1258
|
1528 |
|
jaroslav@1258
|
1529 |
|
jaroslav@1258
|
1530 |
}
|