2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 package java.util.logging;
29 import java.util.HashMap;
31 import org.apidesign.bck2brwsr.core.JavaScriptBody;
34 * A Logger object is used to log messages for a specific
35 * system or application component. Loggers are normally named,
36 * using a hierarchical dot-separated namespace. Logger names
37 * can be arbitrary strings, but they should normally be based on
38 * the package name or class name of the logged component, such
39 * as java.net or javax.swing. In addition it is possible to create
40 * "anonymous" Loggers that are not stored in the Logger namespace.
42 * Logger objects may be obtained by calls on one of the getLogger
43 * factory methods. These will either create a new Logger or
44 * return a suitable existing Logger. It is important to note that
45 * the Logger returned by one of the {@code getLogger} factory methods
46 * may be garbage collected at any time if a strong reference to the
49 * Logging messages will be forwarded to registered Handler
50 * objects, which can forward the messages to a variety of
51 * destinations, including consoles, files, OS logs, etc.
53 * Each Logger keeps track of a "parent" Logger, which is its
54 * nearest existing ancestor in the Logger namespace.
56 * Each Logger has a "Level" associated with it. This reflects
57 * a minimum Level that this logger cares about. If a Logger's
58 * level is set to <tt>null</tt>, then its effective level is inherited
59 * from its parent, which may in turn obtain it recursively from its
60 * parent, and so on up the tree.
62 * The log level can be configured based on the properties from the
63 * logging configuration file, as described in the description
64 * of the LogManager class. However it may also be dynamically changed
65 * by calls on the Logger.setLevel method. If a logger's level is
66 * changed the change may also affect child loggers, since any child
67 * logger that has <tt>null</tt> as its level will inherit its
68 * effective level from its parent.
70 * On each logging call the Logger initially performs a cheap
71 * check of the request level (e.g., SEVERE or FINE) against the
72 * effective log level of the logger. If the request level is
73 * lower than the log level, the logging call returns immediately.
75 * After passing this initial (cheap) test, the Logger will allocate
76 * a LogRecord to describe the logging message. It will then call a
77 * Filter (if present) to do a more detailed check on whether the
78 * record should be published. If that passes it will then publish
79 * the LogRecord to its output Handlers. By default, loggers also
80 * publish to their parent's Handlers, recursively up the tree.
82 * Each Logger may have a ResourceBundle name associated with it.
83 * The named bundle will be used for localizing logging messages.
84 * If a Logger does not have its own ResourceBundle name, then
85 * it will inherit the ResourceBundle name from its parent,
86 * recursively up the tree.
88 * Most of the logger output methods take a "msg" argument. This
89 * msg argument may be either a raw value or a localization key.
90 * During formatting, if the logger has (or inherits) a localization
91 * ResourceBundle and if the ResourceBundle has a mapping for the msg
92 * string, then the msg string is replaced by the localized value.
93 * Otherwise the original msg string is used. Typically, formatters use
94 * java.text.MessageFormat style formatting to format parameters, so
95 * for example a format string "{0} {1}" would format two parameters
98 * When mapping ResourceBundle names to ResourceBundles, the Logger
99 * will first try to use the Thread's ContextClassLoader. If that
100 * is null it will try the SystemClassLoader instead. As a temporary
101 * transition feature in the initial implementation, if the Logger is
102 * unable to locate a ResourceBundle from the ContextClassLoader or
103 * SystemClassLoader the Logger will also search up the class stack
104 * and use successive calling ClassLoaders to try to locate a ResourceBundle.
105 * (This call stack search is to allow containers to transition to
106 * using ContextClassLoaders and is likely to be removed in future
109 * Formatting (including localization) is the responsibility of
110 * the output Handler, which will typically call a Formatter.
112 * Note that formatting need not occur synchronously. It may be delayed
113 * until a LogRecord is actually written to an external sink.
115 * The logging methods are grouped in five main categories:
118 * There are a set of "log" methods that take a log level, a message
119 * string, and optionally some parameters to the message string.
121 * There are a set of "logp" methods (for "log precise") that are
122 * like the "log" methods, but also take an explicit source class name
125 * There are a set of "logrb" method (for "log with resource bundle")
126 * that are like the "logp" method, but also take an explicit resource
127 * bundle name for use in localizing the log message.
129 * There are convenience methods for tracing method entries (the
130 * "entering" methods), method returns (the "exiting" methods) and
131 * throwing exceptions (the "throwing" methods).
133 * Finally, there are a set of convenience methods for use in the
134 * very simplest cases, when a developer simply wants to log a
135 * simple string at a given log level. These methods are named
136 * after the standard Level names ("severe", "warning", "info", etc.)
137 * and take a single argument, a message string.
140 * For the methods that do not take an explicit source name and
141 * method name, the Logging framework will make a "best effort"
142 * to determine which class and method called into the logging method.
143 * However, it is important to realize that this automatically inferred
144 * information may only be approximate (or may even be quite wrong!).
145 * Virtual machines are allowed to do extensive optimizations when
146 * JITing and may entirely remove stack frames, making it impossible
147 * to reliably locate the calling class and method.
149 * All methods on Logger are multi-thread safe.
151 * <b>Subclassing Information:</b> Note that a LogManager class may
152 * provide its own implementation of named Loggers for any point in
153 * the namespace. Therefore, any subclasses of Logger (unless they
154 * are implemented in conjunction with a new LogManager class) should
155 * take care to obtain a Logger instance from the LogManager class and
156 * should delegate operations such as "isLoggable" and "log(LogRecord)"
157 * to that instance. Note that in order to intercept all logging
158 * output, subclasses need only override the log(LogRecord) method.
159 * All the other logging methods are implemented as calls on this
160 * log(LogRecord) method.
166 public class Logger {
167 private static int offValue = Level.OFF.intValue();
168 private static final Map<String,Logger> ALL = new HashMap<>();
171 private volatile int levelValue; // current effective level value
172 private Level levelObject;
175 * GLOBAL_LOGGER_NAME is a name for the global logger.
179 public static final String GLOBAL_LOGGER_NAME = "global";
182 * Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
184 * @return global logger object
187 public static final Logger getGlobal() {
192 * The "global" Logger object is provided as a convenience to developers
193 * who are making casual use of the Logging package. Developers
194 * who are making serious use of the logging package (for example
195 * in products) should create and use their own Logger objects,
196 * with appropriate names, so that logging can be controlled on a
197 * suitable per-Logger granularity. Developers also need to keep a
198 * strong reference to their Logger objects to prevent them from
199 * being garbage collected.
201 * @deprecated Initialization of this field is prone to deadlocks.
202 * The field must be initialized by the Logger class initialization
203 * which may cause deadlocks with the LogManager class initialization.
204 * In such cases two class initialization wait for each other to complete.
205 * The preferred way to get the global logger object is via the call
206 * <code>Logger.getGlobal()</code>.
207 * For compatibility with old JDK versions where the
208 * <code>Logger.getGlobal()</code> is not available use the call
209 * <code>Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)</code>
210 * or <code>Logger.getLogger("global")</code>.
213 public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
216 * Protected method to construct a logger for a named subsystem.
218 * The logger will be initially configured with a null Level
219 * and with useParentHandlers set to true.
221 * @param name A name for the logger. This should
222 * be a dot-separated name and should normally
223 * be based on the package name or class name
224 * of the subsystem, such as java.net
225 * or javax.swing. It may be null for anonymous Loggers.
226 * @param resourceBundleName name of ResourceBundle to be used for localizing
227 * messages for this logger. May be null if none
228 * of the messages require localization.
229 * @throws MissingResourceException if the resourceBundleName is non-null and
230 * no corresponding resource can be found.
232 protected Logger(String name, String resourceBundleName) {
234 levelValue = Level.INFO.intValue();
237 // This constructor is used only to create the global Logger.
238 // It is needed to break a cyclic dependence between the LogManager
239 // and Logger static initializers causing deadlocks.
240 private Logger(String name) {
241 // The manager field is not initialized here.
243 levelValue = Level.INFO.intValue();
246 private void checkAccess() throws SecurityException {
247 throw new SecurityException();
251 * Find or create a logger for a named subsystem. If a logger has
252 * already been created with the given name it is returned. Otherwise
253 * a new logger is created.
255 * If a new logger is created its log level will be configured
256 * based on the LogManager configuration and it will configured
257 * to also send logging output to its parent's Handlers. It will
258 * be registered in the LogManager global namespace.
260 * Note: The LogManager may only retain a weak reference to the newly
261 * created Logger. It is important to understand that a previously
262 * created Logger with the given name may be garbage collected at any
263 * time if there is no strong reference to the Logger. In particular,
264 * this means that two back-to-back calls like
265 * {@code getLogger("MyLogger").log(...)} may use different Logger
266 * objects named "MyLogger" if there is no strong reference to the
267 * Logger named "MyLogger" elsewhere in the program.
269 * @param name A name for the logger. This should
270 * be a dot-separated name and should normally
271 * be based on the package name or class name
272 * of the subsystem, such as java.net
274 * @return a suitable Logger
275 * @throws NullPointerException if the name is null.
278 // Synchronization is not required here. All synchronization for
279 // adding a new Logger object is handled by LogManager.addLogger().
280 public static Logger getLogger(String name) {
281 return getLogger(name, null);
285 * Find or create a logger for a named subsystem. If a logger has
286 * already been created with the given name it is returned. Otherwise
287 * a new logger is created.
289 * If a new logger is created its log level will be configured
290 * based on the LogManager and it will configured to also send logging
291 * output to its parent's Handlers. It will be registered in
292 * the LogManager global namespace.
294 * Note: The LogManager may only retain a weak reference to the newly
295 * created Logger. It is important to understand that a previously
296 * created Logger with the given name may be garbage collected at any
297 * time if there is no strong reference to the Logger. In particular,
298 * this means that two back-to-back calls like
299 * {@code getLogger("MyLogger", ...).log(...)} may use different Logger
300 * objects named "MyLogger" if there is no strong reference to the
301 * Logger named "MyLogger" elsewhere in the program.
303 * If the named Logger already exists and does not yet have a
304 * localization resource bundle then the given resource bundle
305 * name is used. If the named Logger already exists and has
306 * a different resource bundle name then an IllegalArgumentException
309 * @param name A name for the logger. This should
310 * be a dot-separated name and should normally
311 * be based on the package name or class name
312 * of the subsystem, such as java.net
314 * @param resourceBundleName name of ResourceBundle to be used for localizing
315 * messages for this logger. May be <CODE>null</CODE> if none of
316 * the messages require localization.
317 * @return a suitable Logger
318 * @throws MissingResourceException if the resourceBundleName is non-null and
319 * no corresponding resource can be found.
320 * @throws IllegalArgumentException if the Logger already exists and uses
321 * a different resource bundle name.
322 * @throws NullPointerException if the name is null.
325 // Synchronization is not required here. All synchronization for
326 // adding a new Logger object is handled by LogManager.addLogger().
327 public static Logger getLogger(String name, String resourceBundleName) {
328 Logger l = ALL.get(name);
330 l = new Logger(name, resourceBundleName);
338 * Create an anonymous Logger. The newly created Logger is not
339 * registered in the LogManager namespace. There will be no
340 * access checks on updates to the logger.
342 * This factory method is primarily intended for use from applets.
343 * Because the resulting Logger is anonymous it can be kept private
344 * by the creating class. This removes the need for normal security
345 * checks, which in turn allows untrusted applet code to update
346 * the control state of the Logger. For example an applet can do
347 * a setLevel or an addHandler on an anonymous Logger.
349 * Even although the new logger is anonymous, it is configured
350 * to have the root logger ("") as its parent. This means that
351 * by default it inherits its effective level and handlers
352 * from the root logger.
355 * @return a newly created private Logger
357 public static Logger getAnonymousLogger() {
358 return getAnonymousLogger(null);
362 * Create an anonymous Logger. The newly created Logger is not
363 * registered in the LogManager namespace. There will be no
364 * access checks on updates to the logger.
366 * This factory method is primarily intended for use from applets.
367 * Because the resulting Logger is anonymous it can be kept private
368 * by the creating class. This removes the need for normal security
369 * checks, which in turn allows untrusted applet code to update
370 * the control state of the Logger. For example an applet can do
371 * a setLevel or an addHandler on an anonymous Logger.
373 * Even although the new logger is anonymous, it is configured
374 * to have the root logger ("") as its parent. This means that
375 * by default it inherits its effective level and handlers
376 * from the root logger.
378 * @param resourceBundleName name of ResourceBundle to be used for localizing
379 * messages for this logger.
380 * May be null if none of the messages require localization.
381 * @return a newly created private Logger
382 * @throws MissingResourceException if the resourceBundleName is non-null and
383 * no corresponding resource can be found.
386 // Synchronization is not required here. All synchronization for
387 // adding a new anonymous Logger object is handled by doSetParent().
388 public static Logger getAnonymousLogger(String resourceBundleName) {
389 return new Logger(null, resourceBundleName);
393 * Retrieve the localization resource bundle for this
394 * logger for the current default locale. Note that if
395 * the result is null, then the Logger will use a resource
396 * bundle inherited from its parent.
398 * @return localization bundle (may be null)
400 // public ResourceBundle getResourceBundle() {
401 // return findResourceBundle(getResourceBundleName());
405 * Retrieve the localization resource bundle name for this
406 * logger. Note that if the result is null, then the Logger
407 * will use a resource bundle name inherited from its parent.
409 * @return localization bundle name (may be null)
411 public String getResourceBundleName() {
416 * Set a filter to control output on this Logger.
418 * After passing the initial "level" check, the Logger will
419 * call this Filter to check if a log record should really
422 * @param newFilter a filter object (may be null)
423 * @exception SecurityException if a security manager exists and if
424 * the caller does not have LoggingPermission("control").
426 // public void setFilter(Filter newFilter) throws SecurityException {
431 * Get the current filter for this Logger.
433 * @return a filter object (may be null)
435 // public Filter getFilter() {
442 * All the other logging methods in this class call through
443 * this method to actually perform any logging. Subclasses can
444 * override this single method to capture all log activity.
446 * @param record the LogRecord to be published
448 public void log(LogRecord record) {
449 if (record.getLevel().intValue() < levelValue) {
454 switch (record.getLevel().toString()) {
455 case "INFO": method = "info"; break;
456 case "SEVERE": method = "error"; break;
457 case "WARNING": method = "warn"; break;
458 default: method = "log"; break;
463 record.getLoggerName(),
468 @JavaScriptBody(args = { "method", "logger", "msg" }, body =
469 "window.console[method]('[' + logger + ']: ' + msg);"
471 private static native void consoleLog(
472 String method, String logger, String msg
475 // private support method for logging.
476 // We fill in the logger name, resource bundle name, and
477 // resource bundle and then call "void log(LogRecord)".
478 private void doLog(LogRecord lr) {
479 doLog(lr, lr.getResourceBundleName());
481 private void doLog(LogRecord lr, String bundleName) {
482 lr.setLoggerName(name);
487 //================================================================
488 // Start of convenience methods WITHOUT className and methodName
489 //================================================================
492 * Log a message, with no arguments.
494 * If the logger is currently enabled for the given message
495 * level then the given message is forwarded to all the
496 * registered output Handler objects.
498 * @param level One of the message level identifiers, e.g., SEVERE
499 * @param msg The string message (or a key in the message catalog)
501 public void log(Level level, String msg) {
502 if (level.intValue() < levelValue || levelValue == offValue) {
505 LogRecord lr = new LogRecord(level, msg);
510 * Log a message, with one object parameter.
512 * If the logger is currently enabled for the given message
513 * level then a corresponding LogRecord is created and forwarded
514 * to all the registered output Handler objects.
516 * @param level One of the message level identifiers, e.g., SEVERE
517 * @param msg The string message (or a key in the message catalog)
518 * @param param1 parameter to the message
520 public void log(Level level, String msg, Object param1) {
521 if (level.intValue() < levelValue || levelValue == offValue) {
524 LogRecord lr = new LogRecord(level, msg);
525 Object params[] = { param1 };
526 lr.setParameters(params);
531 * Log a message, with an array of object arguments.
533 * If the logger is currently enabled for the given message
534 * level then a corresponding LogRecord is created and forwarded
535 * to all the registered output Handler objects.
537 * @param level One of the message level identifiers, e.g., SEVERE
538 * @param msg The string message (or a key in the message catalog)
539 * @param params array of parameters to the message
541 public void log(Level level, String msg, Object params[]) {
542 if (level.intValue() < levelValue || levelValue == offValue) {
545 LogRecord lr = new LogRecord(level, msg);
546 lr.setParameters(params);
551 * Log a message, with associated Throwable information.
553 * If the logger is currently enabled for the given message
554 * level then the given arguments are stored in a LogRecord
555 * which is forwarded to all registered output handlers.
557 * Note that the thrown argument is stored in the LogRecord thrown
558 * property, rather than the LogRecord parameters property. Thus is it
559 * processed specially by output Formatters and is not treated
560 * as a formatting parameter to the LogRecord message property.
562 * @param level One of the message level identifiers, e.g., SEVERE
563 * @param msg The string message (or a key in the message catalog)
564 * @param thrown Throwable associated with log message.
566 public void log(Level level, String msg, Throwable thrown) {
567 if (level.intValue() < levelValue || levelValue == offValue) {
570 LogRecord lr = new LogRecord(level, msg);
571 lr.setThrown(thrown);
575 //================================================================
576 // Start of convenience methods WITH className and methodName
577 //================================================================
580 * Log a message, specifying source class and method,
583 * If the logger is currently enabled for the given message
584 * level then the given message is forwarded to all the
585 * registered output Handler objects.
587 * @param level One of the message level identifiers, e.g., SEVERE
588 * @param sourceClass name of class that issued the logging request
589 * @param sourceMethod name of method that issued the logging request
590 * @param msg The string message (or a key in the message catalog)
592 public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
593 if (level.intValue() < levelValue || levelValue == offValue) {
596 LogRecord lr = new LogRecord(level, msg);
597 lr.setSourceClassName(sourceClass);
598 lr.setSourceMethodName(sourceMethod);
603 * Log a message, specifying source class and method,
604 * with a single object parameter to the log message.
606 * If the logger is currently enabled for the given message
607 * level then a corresponding LogRecord is created and forwarded
608 * to all the registered output Handler objects.
610 * @param level One of the message level identifiers, e.g., SEVERE
611 * @param sourceClass name of class that issued the logging request
612 * @param sourceMethod name of method that issued the logging request
613 * @param msg The string message (or a key in the message catalog)
614 * @param param1 Parameter to the log message.
616 public void logp(Level level, String sourceClass, String sourceMethod,
617 String msg, Object param1) {
618 if (level.intValue() < levelValue || levelValue == offValue) {
621 LogRecord lr = new LogRecord(level, msg);
622 lr.setSourceClassName(sourceClass);
623 lr.setSourceMethodName(sourceMethod);
624 Object params[] = { param1 };
625 lr.setParameters(params);
630 * Log a message, specifying source class and method,
631 * with an array of object arguments.
633 * If the logger is currently enabled for the given message
634 * level then a corresponding LogRecord is created and forwarded
635 * to all the registered output Handler objects.
637 * @param level One of the message level identifiers, e.g., SEVERE
638 * @param sourceClass name of class that issued the logging request
639 * @param sourceMethod name of method that issued the logging request
640 * @param msg The string message (or a key in the message catalog)
641 * @param params Array of parameters to the message
643 public void logp(Level level, String sourceClass, String sourceMethod,
644 String msg, Object params[]) {
645 if (level.intValue() < levelValue || levelValue == offValue) {
648 LogRecord lr = new LogRecord(level, msg);
649 lr.setSourceClassName(sourceClass);
650 lr.setSourceMethodName(sourceMethod);
651 lr.setParameters(params);
656 * Log a message, specifying source class and method,
657 * with associated Throwable information.
659 * If the logger is currently enabled for the given message
660 * level then the given arguments are stored in a LogRecord
661 * which is forwarded to all registered output handlers.
663 * Note that the thrown argument is stored in the LogRecord thrown
664 * property, rather than the LogRecord parameters property. Thus is it
665 * processed specially by output Formatters and is not treated
666 * as a formatting parameter to the LogRecord message property.
668 * @param level One of the message level identifiers, e.g., SEVERE
669 * @param sourceClass name of class that issued the logging request
670 * @param sourceMethod name of method that issued the logging request
671 * @param msg The string message (or a key in the message catalog)
672 * @param thrown Throwable associated with log message.
674 public void logp(Level level, String sourceClass, String sourceMethod,
675 String msg, Throwable thrown) {
676 if (level.intValue() < levelValue || levelValue == offValue) {
679 LogRecord lr = new LogRecord(level, msg);
680 lr.setSourceClassName(sourceClass);
681 lr.setSourceMethodName(sourceMethod);
682 lr.setThrown(thrown);
687 //=========================================================================
688 // Start of convenience methods WITH className, methodName and bundle name.
689 //=========================================================================
693 * Log a message, specifying source class, method, and resource bundle name
696 * If the logger is currently enabled for the given message
697 * level then the given message is forwarded to all the
698 * registered output Handler objects.
700 * The msg string is localized using the named resource bundle. If the
701 * resource bundle name is null, or an empty String or invalid
702 * then the msg string is not localized.
704 * @param level One of the message level identifiers, e.g., SEVERE
705 * @param sourceClass name of class that issued the logging request
706 * @param sourceMethod name of method that issued the logging request
707 * @param bundleName name of resource bundle to localize msg,
709 * @param msg The string message (or a key in the message catalog)
712 public void logrb(Level level, String sourceClass, String sourceMethod,
713 String bundleName, String msg) {
714 if (level.intValue() < levelValue || levelValue == offValue) {
717 LogRecord lr = new LogRecord(level, msg);
718 lr.setSourceClassName(sourceClass);
719 lr.setSourceMethodName(sourceMethod);
720 doLog(lr, bundleName);
724 * Log a message, specifying source class, method, and resource bundle name,
725 * with a single object parameter to the log message.
727 * If the logger is currently enabled for the given message
728 * level then a corresponding LogRecord is created and forwarded
729 * to all the registered output Handler objects.
731 * The msg string is localized using the named resource bundle. If the
732 * resource bundle name is null, or an empty String or invalid
733 * then the msg string is not localized.
735 * @param level One of the message level identifiers, e.g., SEVERE
736 * @param sourceClass name of class that issued the logging request
737 * @param sourceMethod name of method that issued the logging request
738 * @param bundleName name of resource bundle to localize msg,
740 * @param msg The string message (or a key in the message catalog)
741 * @param param1 Parameter to the log message.
743 public void logrb(Level level, String sourceClass, String sourceMethod,
744 String bundleName, String msg, Object param1) {
745 if (level.intValue() < levelValue || levelValue == offValue) {
748 LogRecord lr = new LogRecord(level, msg);
749 lr.setSourceClassName(sourceClass);
750 lr.setSourceMethodName(sourceMethod);
751 Object params[] = { param1 };
752 lr.setParameters(params);
753 doLog(lr, bundleName);
757 * Log a message, specifying source class, method, and resource bundle name,
758 * with an array of object arguments.
760 * If the logger is currently enabled for the given message
761 * level then a corresponding LogRecord is created and forwarded
762 * to all the registered output Handler objects.
764 * The msg string is localized using the named resource bundle. If the
765 * resource bundle name is null, or an empty String or invalid
766 * then the msg string is not localized.
768 * @param level One of the message level identifiers, e.g., SEVERE
769 * @param sourceClass name of class that issued the logging request
770 * @param sourceMethod name of method that issued the logging request
771 * @param bundleName name of resource bundle to localize msg,
773 * @param msg The string message (or a key in the message catalog)
774 * @param params Array of parameters to the message
776 public void logrb(Level level, String sourceClass, String sourceMethod,
777 String bundleName, String msg, Object params[]) {
778 if (level.intValue() < levelValue || levelValue == offValue) {
781 LogRecord lr = new LogRecord(level, msg);
782 lr.setSourceClassName(sourceClass);
783 lr.setSourceMethodName(sourceMethod);
784 lr.setParameters(params);
785 doLog(lr, bundleName);
789 * Log a message, specifying source class, method, and resource bundle name,
790 * with associated Throwable information.
792 * If the logger is currently enabled for the given message
793 * level then the given arguments are stored in a LogRecord
794 * which is forwarded to all registered output handlers.
796 * The msg string is localized using the named resource bundle. If the
797 * resource bundle name is null, or an empty String or invalid
798 * then the msg string is not localized.
800 * Note that the thrown argument is stored in the LogRecord thrown
801 * property, rather than the LogRecord parameters property. Thus is it
802 * processed specially by output Formatters and is not treated
803 * as a formatting parameter to the LogRecord message property.
805 * @param level One of the message level identifiers, e.g., SEVERE
806 * @param sourceClass name of class that issued the logging request
807 * @param sourceMethod name of method that issued the logging request
808 * @param bundleName name of resource bundle to localize msg,
810 * @param msg The string message (or a key in the message catalog)
811 * @param thrown Throwable associated with log message.
813 public void logrb(Level level, String sourceClass, String sourceMethod,
814 String bundleName, String msg, Throwable thrown) {
815 if (level.intValue() < levelValue || levelValue == offValue) {
818 LogRecord lr = new LogRecord(level, msg);
819 lr.setSourceClassName(sourceClass);
820 lr.setSourceMethodName(sourceMethod);
821 lr.setThrown(thrown);
822 doLog(lr, bundleName);
826 //======================================================================
827 // Start of convenience methods for logging method entries and returns.
828 //======================================================================
831 * Log a method entry.
833 * This is a convenience method that can be used to log entry
834 * to a method. A LogRecord with message "ENTRY", log level
835 * FINER, and the given sourceMethod and sourceClass is logged.
837 * @param sourceClass name of class that issued the logging request
838 * @param sourceMethod name of method that is being entered
840 public void entering(String sourceClass, String sourceMethod) {
841 if (Level.FINER.intValue() < levelValue) {
844 logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
848 * Log a method entry, with one parameter.
850 * This is a convenience method that can be used to log entry
851 * to a method. A LogRecord with message "ENTRY {0}", log level
852 * FINER, and the given sourceMethod, sourceClass, and parameter
855 * @param sourceClass name of class that issued the logging request
856 * @param sourceMethod name of method that is being entered
857 * @param param1 parameter to the method being entered
859 public void entering(String sourceClass, String sourceMethod, Object param1) {
860 if (Level.FINER.intValue() < levelValue) {
863 Object params[] = { param1 };
864 logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
868 * Log a method entry, with an array of parameters.
870 * This is a convenience method that can be used to log entry
871 * to a method. A LogRecord with message "ENTRY" (followed by a
872 * format {N} indicator for each entry in the parameter array),
873 * log level FINER, and the given sourceMethod, sourceClass, and
874 * parameters is logged.
876 * @param sourceClass name of class that issued the logging request
877 * @param sourceMethod name of method that is being entered
878 * @param params array of parameters to the method being entered
880 public void entering(String sourceClass, String sourceMethod, Object params[]) {
881 if (Level.FINER.intValue() < levelValue) {
884 String msg = "ENTRY";
885 if (params == null ) {
886 logp(Level.FINER, sourceClass, sourceMethod, msg);
889 for (int i = 0; i < params.length; i++) {
890 msg = msg + " {" + i + "}";
892 logp(Level.FINER, sourceClass, sourceMethod, msg, params);
896 * Log a method return.
898 * This is a convenience method that can be used to log returning
899 * from a method. A LogRecord with message "RETURN", log level
900 * FINER, and the given sourceMethod and sourceClass is logged.
902 * @param sourceClass name of class that issued the logging request
903 * @param sourceMethod name of the method
905 public void exiting(String sourceClass, String sourceMethod) {
906 if (Level.FINER.intValue() < levelValue) {
909 logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
914 * Log a method return, with result object.
916 * This is a convenience method that can be used to log returning
917 * from a method. A LogRecord with message "RETURN {0}", log level
918 * FINER, and the gives sourceMethod, sourceClass, and result
921 * @param sourceClass name of class that issued the logging request
922 * @param sourceMethod name of the method
923 * @param result Object that is being returned
925 public void exiting(String sourceClass, String sourceMethod, Object result) {
926 if (Level.FINER.intValue() < levelValue) {
929 Object params[] = { result };
930 logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
934 * Log throwing an exception.
936 * This is a convenience method to log that a method is
937 * terminating by throwing an exception. The logging is done
938 * using the FINER level.
940 * If the logger is currently enabled for the given message
941 * level then the given arguments are stored in a LogRecord
942 * which is forwarded to all registered output handlers. The
943 * LogRecord's message is set to "THROW".
945 * Note that the thrown argument is stored in the LogRecord thrown
946 * property, rather than the LogRecord parameters property. Thus is it
947 * processed specially by output Formatters and is not treated
948 * as a formatting parameter to the LogRecord message property.
950 * @param sourceClass name of class that issued the logging request
951 * @param sourceMethod name of the method.
952 * @param thrown The Throwable that is being thrown.
954 public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
955 if (Level.FINER.intValue() < levelValue || levelValue == offValue ) {
958 LogRecord lr = new LogRecord(Level.FINER, "THROW");
959 lr.setSourceClassName(sourceClass);
960 lr.setSourceMethodName(sourceMethod);
961 lr.setThrown(thrown);
965 //=======================================================================
966 // Start of simple convenience methods using level names as method names
967 //=======================================================================
970 * Log a SEVERE message.
972 * If the logger is currently enabled for the SEVERE message
973 * level then the given message is forwarded to all the
974 * registered output Handler objects.
976 * @param msg The string message (or a key in the message catalog)
978 public void severe(String msg) {
979 if (Level.SEVERE.intValue() < levelValue) {
982 log(Level.SEVERE, msg);
986 * Log a WARNING message.
988 * If the logger is currently enabled for the WARNING message
989 * level then the given message is forwarded to all the
990 * registered output Handler objects.
992 * @param msg The string message (or a key in the message catalog)
994 public void warning(String msg) {
995 if (Level.WARNING.intValue() < levelValue) {
998 log(Level.WARNING, msg);
1002 * Log an INFO message.
1004 * If the logger is currently enabled for the INFO message
1005 * level then the given message is forwarded to all the
1006 * registered output Handler objects.
1008 * @param msg The string message (or a key in the message catalog)
1010 public void info(String msg) {
1011 if (Level.INFO.intValue() < levelValue) {
1014 log(Level.INFO, msg);
1018 * Log a CONFIG message.
1020 * If the logger is currently enabled for the CONFIG message
1021 * level then the given message is forwarded to all the
1022 * registered output Handler objects.
1024 * @param msg The string message (or a key in the message catalog)
1026 public void config(String msg) {
1027 if (Level.CONFIG.intValue() < levelValue) {
1030 log(Level.CONFIG, msg);
1034 * Log a FINE message.
1036 * If the logger is currently enabled for the FINE message
1037 * level then the given message is forwarded to all the
1038 * registered output Handler objects.
1040 * @param msg The string message (or a key in the message catalog)
1042 public void fine(String msg) {
1043 if (Level.FINE.intValue() < levelValue) {
1046 log(Level.FINE, msg);
1050 * Log a FINER message.
1052 * If the logger is currently enabled for the FINER message
1053 * level then the given message is forwarded to all the
1054 * registered output Handler objects.
1056 * @param msg The string message (or a key in the message catalog)
1058 public void finer(String msg) {
1059 if (Level.FINER.intValue() < levelValue) {
1062 log(Level.FINER, msg);
1066 * Log a FINEST message.
1068 * If the logger is currently enabled for the FINEST message
1069 * level then the given message is forwarded to all the
1070 * registered output Handler objects.
1072 * @param msg The string message (or a key in the message catalog)
1074 public void finest(String msg) {
1075 if (Level.FINEST.intValue() < levelValue) {
1078 log(Level.FINEST, msg);
1081 //================================================================
1082 // End of convenience methods
1083 //================================================================
1086 * Set the log level specifying which message levels will be
1087 * logged by this logger. Message levels lower than this
1088 * value will be discarded. The level value Level.OFF
1089 * can be used to turn off logging.
1091 * If the new level is null, it means that this node should
1092 * inherit its level from its nearest ancestor with a specific
1093 * (non-null) level value.
1095 * @param newLevel the new value for the log level (may be null)
1096 * @exception SecurityException if a security manager exists and if
1097 * the caller does not have LoggingPermission("control").
1099 public void setLevel(Level newLevel) throws SecurityException {
1100 levelValue = newLevel.intValue();
1101 levelObject = newLevel;
1105 * Get the log Level that has been specified for this Logger.
1106 * The result may be null, which means that this logger's
1107 * effective level will be inherited from its parent.
1109 * @return this Logger's level
1111 public Level getLevel() {
1116 * Check if a message of the given level would actually be logged
1117 * by this logger. This check is based on the Loggers effective level,
1118 * which may be inherited from its parent.
1120 * @param level a message logging level
1121 * @return true if the given message level is currently being logged.
1123 public boolean isLoggable(Level level) {
1124 if (level.intValue() < levelValue || levelValue == offValue) {
1131 * Get the name for this logger.
1132 * @return logger name. Will be null for anonymous Loggers.
1134 public String getName() {
1139 * Add a log Handler to receive logging messages.
1141 * By default, Loggers also send their output to their parent logger.
1142 * Typically the root Logger is configured with a set of Handlers
1143 * that essentially act as default handlers for all loggers.
1145 * @param handler a logging Handler
1146 * @exception SecurityException if a security manager exists and if
1147 * the caller does not have LoggingPermission("control").
1149 // public void addHandler(Handler handler) throws SecurityException {
1150 // // Check for null handler
1151 // handler.getClass();
1153 // handlers.add(handler);
1157 * Remove a log Handler.
1159 * Returns silently if the given Handler is not found or is null
1161 * @param handler a logging Handler
1162 * @exception SecurityException if a security manager exists and if
1163 * the caller does not have LoggingPermission("control").
1165 // public void removeHandler(Handler handler) throws SecurityException {
1167 // if (handler == null) {
1170 // handlers.remove(handler);
1174 * Get the Handlers associated with this logger.
1176 * @return an array of all registered Handlers
1178 // public Handler[] getHandlers() {
1179 // return handlers.toArray(emptyHandlers);
1183 * Specify whether or not this logger should send its output
1184 * to its parent Logger. This means that any LogRecords will
1185 * also be written to the parent's Handlers, and potentially
1186 * to its parent, recursively up the namespace.
1188 * @param useParentHandlers true if output is to be sent to the
1190 * @exception SecurityException if a security manager exists and if
1191 * the caller does not have LoggingPermission("control").
1193 public void setUseParentHandlers(boolean useParentHandlers) {
1198 * Discover whether or not this logger is sending its output
1199 * to its parent logger.
1201 * @return true if output is to be sent to the logger's parent
1203 public boolean getUseParentHandlers() {
1208 * Return the parent for this Logger.
1210 * This method returns the nearest extant parent in the namespace.
1211 * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
1212 * has been created but no logger "a.b.c" exists, then a call of
1213 * getParent on the Logger "a.b.c.d" will return the Logger "a.b".
1215 * The result will be null if it is called on the root Logger
1218 * @return nearest existing parent Logger
1220 public Logger getParent() {
1221 // Note: this used to be synchronized on treeLock. However, this only
1222 // provided memory semantics, as there was no guarantee that the caller
1223 // would synchronize on treeLock (in fact, there is no way for external
1224 // callers to so synchronize). Therefore, we have made parent volatile
1226 String n = getName();
1227 int at = n.length();
1229 int last = n.lastIndexOf('.', at - 1);
1233 Logger p = ALL.get(n.substring(0, last));
1242 * Set the parent for this Logger. This method is used by
1243 * the LogManager to update a Logger when the namespace changes.
1245 * It should not be called from application code.
1247 * @param parent the new parent logger
1248 * @exception SecurityException if a security manager exists and if
1249 * the caller does not have LoggingPermission("control").
1251 public void setParent(Logger parent) {
1252 if (parent == null) {
1253 throw new NullPointerException();