jaroslav@1258: /* jaroslav@1258: * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1258: * jaroslav@1258: * This code is free software; you can redistribute it and/or modify it jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as jaroslav@1258: * published by the Free Software Foundation. Oracle designates this jaroslav@1258: * particular file as subject to the "Classpath" exception as provided jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1258: * jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1258: * accompanied this code). jaroslav@1258: * jaroslav@1258: * You should have received a copy of the GNU General Public License version jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1258: * jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1258: * or visit www.oracle.com if you need additional information or have any jaroslav@1258: * questions. jaroslav@1258: */ jaroslav@1258: jaroslav@1258: jaroslav@1258: package java.util.logging; jaroslav@1258: jaroslav@1258: import java.util.*; jaroslav@1258: import java.util.concurrent.CopyOnWriteArrayList; jaroslav@1258: import java.security.*; jaroslav@1258: import java.lang.ref.WeakReference; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * A Logger object is used to log messages for a specific jaroslav@1258: * system or application component. Loggers are normally named, jaroslav@1258: * using a hierarchical dot-separated namespace. Logger names jaroslav@1258: * can be arbitrary strings, but they should normally be based on jaroslav@1258: * the package name or class name of the logged component, such jaroslav@1258: * as java.net or javax.swing. In addition it is possible to create jaroslav@1258: * "anonymous" Loggers that are not stored in the Logger namespace. jaroslav@1258: *
jaroslav@1258: * Logger objects may be obtained by calls on one of the getLogger jaroslav@1258: * factory methods. These will either create a new Logger or jaroslav@1258: * return a suitable existing Logger. It is important to note that jaroslav@1258: * the Logger returned by one of the {@code getLogger} factory methods jaroslav@1258: * may be garbage collected at any time if a strong reference to the jaroslav@1258: * Logger is not kept. jaroslav@1258: *
jaroslav@1258: * Logging messages will be forwarded to registered Handler jaroslav@1258: * objects, which can forward the messages to a variety of jaroslav@1258: * destinations, including consoles, files, OS logs, etc. jaroslav@1258: *
jaroslav@1258: * Each Logger keeps track of a "parent" Logger, which is its jaroslav@1258: * nearest existing ancestor in the Logger namespace. jaroslav@1258: *
jaroslav@1258: * Each Logger has a "Level" associated with it. This reflects jaroslav@1258: * a minimum Level that this logger cares about. If a Logger's jaroslav@1258: * level is set to null, then its effective level is inherited jaroslav@1258: * from its parent, which may in turn obtain it recursively from its jaroslav@1258: * parent, and so on up the tree. jaroslav@1258: *
jaroslav@1258: * The log level can be configured based on the properties from the jaroslav@1258: * logging configuration file, as described in the description jaroslav@1258: * of the LogManager class. However it may also be dynamically changed jaroslav@1258: * by calls on the Logger.setLevel method. If a logger's level is jaroslav@1258: * changed the change may also affect child loggers, since any child jaroslav@1258: * logger that has null as its level will inherit its jaroslav@1258: * effective level from its parent. jaroslav@1258: *
jaroslav@1258: * On each logging call the Logger initially performs a cheap jaroslav@1258: * check of the request level (e.g., SEVERE or FINE) against the jaroslav@1258: * effective log level of the logger. If the request level is jaroslav@1258: * lower than the log level, the logging call returns immediately. jaroslav@1258: *
jaroslav@1258: * After passing this initial (cheap) test, the Logger will allocate jaroslav@1258: * a LogRecord to describe the logging message. It will then call a jaroslav@1258: * Filter (if present) to do a more detailed check on whether the jaroslav@1258: * record should be published. If that passes it will then publish jaroslav@1258: * the LogRecord to its output Handlers. By default, loggers also jaroslav@1258: * publish to their parent's Handlers, recursively up the tree. jaroslav@1258: *
jaroslav@1258: * Each Logger may have a ResourceBundle name associated with it. jaroslav@1258: * The named bundle will be used for localizing logging messages. jaroslav@1258: * If a Logger does not have its own ResourceBundle name, then jaroslav@1258: * it will inherit the ResourceBundle name from its parent, jaroslav@1258: * recursively up the tree. jaroslav@1258: *
jaroslav@1258: * Most of the logger output methods take a "msg" argument. This jaroslav@1258: * msg argument may be either a raw value or a localization key. jaroslav@1258: * During formatting, if the logger has (or inherits) a localization jaroslav@1258: * ResourceBundle and if the ResourceBundle has a mapping for the msg jaroslav@1258: * string, then the msg string is replaced by the localized value. jaroslav@1258: * Otherwise the original msg string is used. Typically, formatters use jaroslav@1258: * java.text.MessageFormat style formatting to format parameters, so jaroslav@1258: * for example a format string "{0} {1}" would format two parameters jaroslav@1258: * as strings. jaroslav@1258: *
jaroslav@1258: * When mapping ResourceBundle names to ResourceBundles, the Logger jaroslav@1258: * will first try to use the Thread's ContextClassLoader. If that jaroslav@1258: * is null it will try the SystemClassLoader instead. As a temporary jaroslav@1258: * transition feature in the initial implementation, if the Logger is jaroslav@1258: * unable to locate a ResourceBundle from the ContextClassLoader or jaroslav@1258: * SystemClassLoader the Logger will also search up the class stack jaroslav@1258: * and use successive calling ClassLoaders to try to locate a ResourceBundle. jaroslav@1258: * (This call stack search is to allow containers to transition to jaroslav@1258: * using ContextClassLoaders and is likely to be removed in future jaroslav@1258: * versions.) jaroslav@1258: *
jaroslav@1258: * Formatting (including localization) is the responsibility of jaroslav@1258: * the output Handler, which will typically call a Formatter. jaroslav@1258: *
jaroslav@1258: * Note that formatting need not occur synchronously. It may be delayed jaroslav@1258: * until a LogRecord is actually written to an external sink. jaroslav@1258: *
jaroslav@1258: * The logging methods are grouped in five main categories: jaroslav@1258: *
jaroslav@1258: * There are a set of "log" methods that take a log level, a message jaroslav@1258: * string, and optionally some parameters to the message string. jaroslav@1258: *
jaroslav@1258: * There are a set of "logp" methods (for "log precise") that are jaroslav@1258: * like the "log" methods, but also take an explicit source class name jaroslav@1258: * and method name. jaroslav@1258: *
jaroslav@1258: * There are a set of "logrb" method (for "log with resource bundle") jaroslav@1258: * that are like the "logp" method, but also take an explicit resource jaroslav@1258: * bundle name for use in localizing the log message. jaroslav@1258: *
jaroslav@1258: * There are convenience methods for tracing method entries (the jaroslav@1258: * "entering" methods), method returns (the "exiting" methods) and jaroslav@1258: * throwing exceptions (the "throwing" methods). jaroslav@1258: *
jaroslav@1258: * Finally, there are a set of convenience methods for use in the jaroslav@1258: * very simplest cases, when a developer simply wants to log a jaroslav@1258: * simple string at a given log level. These methods are named jaroslav@1258: * after the standard Level names ("severe", "warning", "info", etc.) jaroslav@1258: * and take a single argument, a message string. jaroslav@1258: *
jaroslav@1258: * For the methods that do not take an explicit source name and jaroslav@1258: * method name, the Logging framework will make a "best effort" jaroslav@1258: * to determine which class and method called into the logging method. jaroslav@1258: * However, it is important to realize that this automatically inferred jaroslav@1258: * information may only be approximate (or may even be quite wrong!). jaroslav@1258: * Virtual machines are allowed to do extensive optimizations when jaroslav@1258: * JITing and may entirely remove stack frames, making it impossible jaroslav@1258: * to reliably locate the calling class and method. jaroslav@1258: *
jaroslav@1258: * All methods on Logger are multi-thread safe. jaroslav@1258: *
jaroslav@1258: * Subclassing Information: Note that a LogManager class may
jaroslav@1258: * provide its own implementation of named Loggers for any point in
jaroslav@1258: * the namespace. Therefore, any subclasses of Logger (unless they
jaroslav@1258: * are implemented in conjunction with a new LogManager class) should
jaroslav@1258: * take care to obtain a Logger instance from the LogManager class and
jaroslav@1258: * should delegate operations such as "isLoggable" and "log(LogRecord)"
jaroslav@1258: * to that instance. Note that in order to intercept all logging
jaroslav@1258: * output, subclasses need only override the log(LogRecord) method.
jaroslav@1258: * All the other logging methods are implemented as calls on this
jaroslav@1258: * log(LogRecord) method.
jaroslav@1258: *
jaroslav@1258: * @since 1.4
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: public class Logger {
jaroslav@1258: private static final Handler emptyHandlers[] = new Handler[0];
jaroslav@1258: private static final int offValue = Level.OFF.intValue();
jaroslav@1258: private LogManager manager;
jaroslav@1258: private String name;
jaroslav@1258: private final CopyOnWriteArrayList
jaroslav@1258: * @deprecated Initialization of this field is prone to deadlocks.
jaroslav@1258: * The field must be initialized by the Logger class initialization
jaroslav@1258: * which may cause deadlocks with the LogManager class initialization.
jaroslav@1258: * In such cases two class initialization wait for each other to complete.
jaroslav@1258: * The preferred way to get the global logger object is via the call
jaroslav@1258: *
jaroslav@1258: * The logger will be initially configured with a null Level
jaroslav@1258: * and with useParentHandlers set to true.
jaroslav@1258: *
jaroslav@1258: * @param name A name for the logger. This should
jaroslav@1258: * be a dot-separated name and should normally
jaroslav@1258: * be based on the package name or class name
jaroslav@1258: * of the subsystem, such as java.net
jaroslav@1258: * or javax.swing. It may be null for anonymous Loggers.
jaroslav@1258: * @param resourceBundleName name of ResourceBundle to be used for localizing
jaroslav@1258: * messages for this logger. May be null if none
jaroslav@1258: * of the messages require localization.
jaroslav@1258: * @throws MissingResourceException if the resourceBundleName is non-null and
jaroslav@1258: * no corresponding resource can be found.
jaroslav@1258: */
jaroslav@1258: protected Logger(String name, String resourceBundleName) {
jaroslav@1258: this.manager = LogManager.getLogManager();
jaroslav@1258: if (resourceBundleName != null) {
jaroslav@1258: // Note: we may get a MissingResourceException here.
jaroslav@1258: setupResourceInfo(resourceBundleName);
jaroslav@1258: }
jaroslav@1258: this.name = name;
jaroslav@1258: levelValue = Level.INFO.intValue();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // This constructor is used only to create the global Logger.
jaroslav@1258: // It is needed to break a cyclic dependence between the LogManager
jaroslav@1258: // and Logger static initializers causing deadlocks.
jaroslav@1258: private Logger(String name) {
jaroslav@1258: // The manager field is not initialized here.
jaroslav@1258: this.name = name;
jaroslav@1258: levelValue = Level.INFO.intValue();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // It is called from the LogManager.
jaroslav@1258: * If a new logger is created its log level will be configured
jaroslav@1258: * based on the LogManager configuration and it will configured
jaroslav@1258: * to also send logging output to its parent's Handlers. It will
jaroslav@1258: * be registered in the LogManager global namespace.
jaroslav@1258: *
jaroslav@1258: * Note: The LogManager may only retain a weak reference to the newly
jaroslav@1258: * created Logger. It is important to understand that a previously
jaroslav@1258: * created Logger with the given name may be garbage collected at any
jaroslav@1258: * time if there is no strong reference to the Logger. In particular,
jaroslav@1258: * this means that two back-to-back calls like
jaroslav@1258: * {@code getLogger("MyLogger").log(...)} may use different Logger
jaroslav@1258: * objects named "MyLogger" if there is no strong reference to the
jaroslav@1258: * Logger named "MyLogger" elsewhere in the program.
jaroslav@1258: *
jaroslav@1258: * @param name A name for the logger. This should
jaroslav@1258: * be a dot-separated name and should normally
jaroslav@1258: * be based on the package name or class name
jaroslav@1258: * of the subsystem, such as java.net
jaroslav@1258: * or javax.swing
jaroslav@1258: * @return a suitable Logger
jaroslav@1258: * @throws NullPointerException if the name is null.
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: // Synchronization is not required here. All synchronization for
jaroslav@1258: // adding a new Logger object is handled by LogManager.addLogger().
jaroslav@1258: public static Logger getLogger(String name) {
jaroslav@1258: // This method is intentionally not a wrapper around a call
jaroslav@1258: // to getLogger(name, resourceBundleName). If it were then
jaroslav@1258: // this sequence:
jaroslav@1258: //
jaroslav@1258: // getLogger("Foo", "resourceBundleForFoo");
jaroslav@1258: // getLogger("Foo");
jaroslav@1258: //
jaroslav@1258: // would throw an IllegalArgumentException in the second call
jaroslav@1258: // because the wrapper would result in an attempt to replace
jaroslav@1258: // the existing "resourceBundleForFoo" with null.
jaroslav@1258: LogManager manager = LogManager.getLogManager();
jaroslav@1258: return manager.demandLogger(name);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Find or create a logger for a named subsystem. If a logger has
jaroslav@1258: * already been created with the given name it is returned. Otherwise
jaroslav@1258: * a new logger is created.
jaroslav@1258: *
jaroslav@1258: * If a new logger is created its log level will be configured
jaroslav@1258: * based on the LogManager and it will configured to also send logging
jaroslav@1258: * output to its parent's Handlers. It will be registered in
jaroslav@1258: * the LogManager global namespace.
jaroslav@1258: *
jaroslav@1258: * Note: The LogManager may only retain a weak reference to the newly
jaroslav@1258: * created Logger. It is important to understand that a previously
jaroslav@1258: * created Logger with the given name may be garbage collected at any
jaroslav@1258: * time if there is no strong reference to the Logger. In particular,
jaroslav@1258: * this means that two back-to-back calls like
jaroslav@1258: * {@code getLogger("MyLogger", ...).log(...)} may use different Logger
jaroslav@1258: * objects named "MyLogger" if there is no strong reference to the
jaroslav@1258: * Logger named "MyLogger" elsewhere in the program.
jaroslav@1258: *
jaroslav@1258: * If the named Logger already exists and does not yet have a
jaroslav@1258: * localization resource bundle then the given resource bundle
jaroslav@1258: * name is used. If the named Logger already exists and has
jaroslav@1258: * a different resource bundle name then an IllegalArgumentException
jaroslav@1258: * is thrown.
jaroslav@1258: *
jaroslav@1258: * @param name A name for the logger. This should
jaroslav@1258: * be a dot-separated name and should normally
jaroslav@1258: * be based on the package name or class name
jaroslav@1258: * of the subsystem, such as java.net
jaroslav@1258: * or javax.swing
jaroslav@1258: * @param resourceBundleName name of ResourceBundle to be used for localizing
jaroslav@1258: * messages for this logger. May be
jaroslav@1258: * This factory method is primarily intended for use from applets.
jaroslav@1258: * Because the resulting Logger is anonymous it can be kept private
jaroslav@1258: * by the creating class. This removes the need for normal security
jaroslav@1258: * checks, which in turn allows untrusted applet code to update
jaroslav@1258: * the control state of the Logger. For example an applet can do
jaroslav@1258: * a setLevel or an addHandler on an anonymous Logger.
jaroslav@1258: *
jaroslav@1258: * Even although the new logger is anonymous, it is configured
jaroslav@1258: * to have the root logger ("") as its parent. This means that
jaroslav@1258: * by default it inherits its effective level and handlers
jaroslav@1258: * from the root logger.
jaroslav@1258: *
jaroslav@1258: *
jaroslav@1258: * @return a newly created private Logger
jaroslav@1258: */
jaroslav@1258: public static Logger getAnonymousLogger() {
jaroslav@1258: return getAnonymousLogger(null);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Create an anonymous Logger. The newly created Logger is not
jaroslav@1258: * registered in the LogManager namespace. There will be no
jaroslav@1258: * access checks on updates to the logger.
jaroslav@1258: *
jaroslav@1258: * This factory method is primarily intended for use from applets.
jaroslav@1258: * Because the resulting Logger is anonymous it can be kept private
jaroslav@1258: * by the creating class. This removes the need for normal security
jaroslav@1258: * checks, which in turn allows untrusted applet code to update
jaroslav@1258: * the control state of the Logger. For example an applet can do
jaroslav@1258: * a setLevel or an addHandler on an anonymous Logger.
jaroslav@1258: *
jaroslav@1258: * Even although the new logger is anonymous, it is configured
jaroslav@1258: * to have the root logger ("") as its parent. This means that
jaroslav@1258: * by default it inherits its effective level and handlers
jaroslav@1258: * from the root logger.
jaroslav@1258: *
jaroslav@1258: * @param resourceBundleName name of ResourceBundle to be used for localizing
jaroslav@1258: * messages for this logger.
jaroslav@1258: * May be null if none of the messages require localization.
jaroslav@1258: * @return a newly created private Logger
jaroslav@1258: * @throws MissingResourceException if the resourceBundleName is non-null and
jaroslav@1258: * no corresponding resource can be found.
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: // Synchronization is not required here. All synchronization for
jaroslav@1258: // adding a new anonymous Logger object is handled by doSetParent().
jaroslav@1258: public static Logger getAnonymousLogger(String resourceBundleName) {
jaroslav@1258: LogManager manager = LogManager.getLogManager();
jaroslav@1258: // cleanup some Loggers that have been GC'ed
jaroslav@1258: manager.drainLoggerRefQueueBounded();
jaroslav@1258: Logger result = new Logger(null, resourceBundleName);
jaroslav@1258: result.anonymous = true;
jaroslav@1258: Logger root = manager.getLogger("");
jaroslav@1258: result.doSetParent(root);
jaroslav@1258: return result;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Retrieve the localization resource bundle for this
jaroslav@1258: * logger for the current default locale. Note that if
jaroslav@1258: * the result is null, then the Logger will use a resource
jaroslav@1258: * bundle inherited from its parent.
jaroslav@1258: *
jaroslav@1258: * @return localization bundle (may be null)
jaroslav@1258: */
jaroslav@1258: public ResourceBundle getResourceBundle() {
jaroslav@1258: return findResourceBundle(getResourceBundleName());
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Retrieve the localization resource bundle name for this
jaroslav@1258: * logger. Note that if the result is null, then the Logger
jaroslav@1258: * will use a resource bundle name inherited from its parent.
jaroslav@1258: *
jaroslav@1258: * @return localization bundle name (may be null)
jaroslav@1258: */
jaroslav@1258: public String getResourceBundleName() {
jaroslav@1258: return resourceBundleName;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Set a filter to control output on this Logger.
jaroslav@1258: *
jaroslav@1258: * After passing the initial "level" check, the Logger will
jaroslav@1258: * call this Filter to check if a log record should really
jaroslav@1258: * be published.
jaroslav@1258: *
jaroslav@1258: * @param newFilter a filter object (may be null)
jaroslav@1258: * @exception SecurityException if a security manager exists and if
jaroslav@1258: * the caller does not have LoggingPermission("control").
jaroslav@1258: */
jaroslav@1258: public void setFilter(Filter newFilter) throws SecurityException {
jaroslav@1258: checkAccess();
jaroslav@1258: filter = newFilter;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Get the current filter for this Logger.
jaroslav@1258: *
jaroslav@1258: * @return a filter object (may be null)
jaroslav@1258: */
jaroslav@1258: public Filter getFilter() {
jaroslav@1258: return filter;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a LogRecord.
jaroslav@1258: *
jaroslav@1258: * All the other logging methods in this class call through
jaroslav@1258: * this method to actually perform any logging. Subclasses can
jaroslav@1258: * override this single method to capture all log activity.
jaroslav@1258: *
jaroslav@1258: * @param record the LogRecord to be published
jaroslav@1258: */
jaroslav@1258: public void log(LogRecord record) {
jaroslav@1258: if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: Filter theFilter = filter;
jaroslav@1258: if (theFilter != null && !theFilter.isLoggable(record)) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // Post the LogRecord to all our Handlers, and then to
jaroslav@1258: // our parents' handlers, all the way up the tree.
jaroslav@1258:
jaroslav@1258: Logger logger = this;
jaroslav@1258: while (logger != null) {
jaroslav@1258: for (Handler handler : logger.getHandlers()) {
jaroslav@1258: handler.publish(record);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: if (!logger.getUseParentHandlers()) {
jaroslav@1258: break;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: logger = logger.getParent();
jaroslav@1258: }
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // private support method for logging.
jaroslav@1258: // We fill in the logger name, resource bundle name, and
jaroslav@1258: // resource bundle and then call "void log(LogRecord)".
jaroslav@1258: private void doLog(LogRecord lr) {
jaroslav@1258: lr.setLoggerName(name);
jaroslav@1258: String ebname = getEffectiveResourceBundleName();
jaroslav@1258: if (ebname != null) {
jaroslav@1258: lr.setResourceBundleName(ebname);
jaroslav@1258: lr.setResourceBundle(findResourceBundle(ebname));
jaroslav@1258: }
jaroslav@1258: log(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: //================================================================
jaroslav@1258: // Start of convenience methods WITHOUT className and methodName
jaroslav@1258: //================================================================
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, with no arguments.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void log(Level level, String msg) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, with one object parameter.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then a corresponding LogRecord is created and forwarded
jaroslav@1258: * to all the registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param param1 parameter to the message
jaroslav@1258: */
jaroslav@1258: public void log(Level level, String msg, Object param1) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: Object params[] = { param1 };
jaroslav@1258: lr.setParameters(params);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, with an array of object arguments.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then a corresponding LogRecord is created and forwarded
jaroslav@1258: * to all the registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param params array of parameters to the message
jaroslav@1258: */
jaroslav@1258: public void log(Level level, String msg, Object params[]) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setParameters(params);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, with associated Throwable information.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given arguments are stored in a LogRecord
jaroslav@1258: * which is forwarded to all registered output handlers.
jaroslav@1258: *
jaroslav@1258: * Note that the thrown argument is stored in the LogRecord thrown
jaroslav@1258: * property, rather than the LogRecord parameters property. Thus is it
jaroslav@1258: * processed specially by output Formatters and is not treated
jaroslav@1258: * as a formatting parameter to the LogRecord message property.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param thrown Throwable associated with log message.
jaroslav@1258: */
jaroslav@1258: public void log(Level level, String msg, Throwable thrown) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setThrown(thrown);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: //================================================================
jaroslav@1258: // Start of convenience methods WITH className and methodName
jaroslav@1258: //================================================================
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class and method,
jaroslav@1258: * with no arguments.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class and method,
jaroslav@1258: * with a single object parameter to the log message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then a corresponding LogRecord is created and forwarded
jaroslav@1258: * to all the registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param param1 Parameter to the log message.
jaroslav@1258: */
jaroslav@1258: public void logp(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String msg, Object param1) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: Object params[] = { param1 };
jaroslav@1258: lr.setParameters(params);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class and method,
jaroslav@1258: * with an array of object arguments.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then a corresponding LogRecord is created and forwarded
jaroslav@1258: * to all the registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param params Array of parameters to the message
jaroslav@1258: */
jaroslav@1258: public void logp(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String msg, Object params[]) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: lr.setParameters(params);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class and method,
jaroslav@1258: * with associated Throwable information.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given arguments are stored in a LogRecord
jaroslav@1258: * which is forwarded to all registered output handlers.
jaroslav@1258: *
jaroslav@1258: * Note that the thrown argument is stored in the LogRecord thrown
jaroslav@1258: * property, rather than the LogRecord parameters property. Thus is it
jaroslav@1258: * processed specially by output Formatters and is not treated
jaroslav@1258: * as a formatting parameter to the LogRecord message property.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param thrown Throwable associated with log message.
jaroslav@1258: */
jaroslav@1258: public void logp(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String msg, Throwable thrown) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: lr.setThrown(thrown);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: //=========================================================================
jaroslav@1258: // Start of convenience methods WITH className, methodName and bundle name.
jaroslav@1258: //=========================================================================
jaroslav@1258:
jaroslav@1258: // Private support method for logging for "logrb" methods.
jaroslav@1258: // We fill in the logger name, resource bundle name, and
jaroslav@1258: // resource bundle and then call "void log(LogRecord)".
jaroslav@1258: private void doLog(LogRecord lr, String rbname) {
jaroslav@1258: lr.setLoggerName(name);
jaroslav@1258: if (rbname != null) {
jaroslav@1258: lr.setResourceBundleName(rbname);
jaroslav@1258: lr.setResourceBundle(findResourceBundle(rbname));
jaroslav@1258: }
jaroslav@1258: log(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class, method, and resource bundle name
jaroslav@1258: * with no arguments.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * The msg string is localized using the named resource bundle. If the
jaroslav@1258: * resource bundle name is null, or an empty String or invalid
jaroslav@1258: * then the msg string is not localized.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param bundleName name of resource bundle to localize msg,
jaroslav@1258: * can be null
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: public void logrb(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String bundleName, String msg) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: doLog(lr, bundleName);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class, method, and resource bundle name,
jaroslav@1258: * with a single object parameter to the log message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then a corresponding LogRecord is created and forwarded
jaroslav@1258: * to all the registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * The msg string is localized using the named resource bundle. If the
jaroslav@1258: * resource bundle name is null, or an empty String or invalid
jaroslav@1258: * then the msg string is not localized.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param bundleName name of resource bundle to localize msg,
jaroslav@1258: * can be null
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param param1 Parameter to the log message.
jaroslav@1258: */
jaroslav@1258: public void logrb(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String bundleName, String msg, Object param1) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: Object params[] = { param1 };
jaroslav@1258: lr.setParameters(params);
jaroslav@1258: doLog(lr, bundleName);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class, method, and resource bundle name,
jaroslav@1258: * with an array of object arguments.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then a corresponding LogRecord is created and forwarded
jaroslav@1258: * to all the registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * The msg string is localized using the named resource bundle. If the
jaroslav@1258: * resource bundle name is null, or an empty String or invalid
jaroslav@1258: * then the msg string is not localized.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param bundleName name of resource bundle to localize msg,
jaroslav@1258: * can be null.
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param params Array of parameters to the message
jaroslav@1258: */
jaroslav@1258: public void logrb(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String bundleName, String msg, Object params[]) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: lr.setParameters(params);
jaroslav@1258: doLog(lr, bundleName);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a message, specifying source class, method, and resource bundle name,
jaroslav@1258: * with associated Throwable information.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given arguments are stored in a LogRecord
jaroslav@1258: * which is forwarded to all registered output handlers.
jaroslav@1258: *
jaroslav@1258: * The msg string is localized using the named resource bundle. If the
jaroslav@1258: * resource bundle name is null, or an empty String or invalid
jaroslav@1258: * then the msg string is not localized.
jaroslav@1258: *
jaroslav@1258: * Note that the thrown argument is stored in the LogRecord thrown
jaroslav@1258: * property, rather than the LogRecord parameters property. Thus is it
jaroslav@1258: * processed specially by output Formatters and is not treated
jaroslav@1258: * as a formatting parameter to the LogRecord message property.
jaroslav@1258: *
jaroslav@1258: * @param level One of the message level identifiers, e.g., SEVERE
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that issued the logging request
jaroslav@1258: * @param bundleName name of resource bundle to localize msg,
jaroslav@1258: * can be null
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: * @param thrown Throwable associated with log message.
jaroslav@1258: */
jaroslav@1258: public void logrb(Level level, String sourceClass, String sourceMethod,
jaroslav@1258: String bundleName, String msg, Throwable thrown) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(level, msg);
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: lr.setThrown(thrown);
jaroslav@1258: doLog(lr, bundleName);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: //======================================================================
jaroslav@1258: // Start of convenience methods for logging method entries and returns.
jaroslav@1258: //======================================================================
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a method entry.
jaroslav@1258: *
jaroslav@1258: * This is a convenience method that can be used to log entry
jaroslav@1258: * to a method. A LogRecord with message "ENTRY", log level
jaroslav@1258: * FINER, and the given sourceMethod and sourceClass is logged.
jaroslav@1258: *
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that is being entered
jaroslav@1258: */
jaroslav@1258: public void entering(String sourceClass, String sourceMethod) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a method entry, with one parameter.
jaroslav@1258: *
jaroslav@1258: * This is a convenience method that can be used to log entry
jaroslav@1258: * to a method. A LogRecord with message "ENTRY {0}", log level
jaroslav@1258: * FINER, and the given sourceMethod, sourceClass, and parameter
jaroslav@1258: * is logged.
jaroslav@1258: *
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that is being entered
jaroslav@1258: * @param param1 parameter to the method being entered
jaroslav@1258: */
jaroslav@1258: public void entering(String sourceClass, String sourceMethod, Object param1) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: Object params[] = { param1 };
jaroslav@1258: logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a method entry, with an array of parameters.
jaroslav@1258: *
jaroslav@1258: * This is a convenience method that can be used to log entry
jaroslav@1258: * to a method. A LogRecord with message "ENTRY" (followed by a
jaroslav@1258: * format {N} indicator for each entry in the parameter array),
jaroslav@1258: * log level FINER, and the given sourceMethod, sourceClass, and
jaroslav@1258: * parameters is logged.
jaroslav@1258: *
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of method that is being entered
jaroslav@1258: * @param params array of parameters to the method being entered
jaroslav@1258: */
jaroslav@1258: public void entering(String sourceClass, String sourceMethod, Object params[]) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: String msg = "ENTRY";
jaroslav@1258: if (params == null ) {
jaroslav@1258: logp(Level.FINER, sourceClass, sourceMethod, msg);
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: for (int i = 0; i < params.length; i++) {
jaroslav@1258: msg = msg + " {" + i + "}";
jaroslav@1258: }
jaroslav@1258: logp(Level.FINER, sourceClass, sourceMethod, msg, params);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a method return.
jaroslav@1258: *
jaroslav@1258: * This is a convenience method that can be used to log returning
jaroslav@1258: * from a method. A LogRecord with message "RETURN", log level
jaroslav@1258: * FINER, and the given sourceMethod and sourceClass is logged.
jaroslav@1258: *
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of the method
jaroslav@1258: */
jaroslav@1258: public void exiting(String sourceClass, String sourceMethod) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a method return, with result object.
jaroslav@1258: *
jaroslav@1258: * This is a convenience method that can be used to log returning
jaroslav@1258: * from a method. A LogRecord with message "RETURN {0}", log level
jaroslav@1258: * FINER, and the gives sourceMethod, sourceClass, and result
jaroslav@1258: * object is logged.
jaroslav@1258: *
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of the method
jaroslav@1258: * @param result Object that is being returned
jaroslav@1258: */
jaroslav@1258: public void exiting(String sourceClass, String sourceMethod, Object result) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: Object params[] = { result };
jaroslav@1258: logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log throwing an exception.
jaroslav@1258: *
jaroslav@1258: * This is a convenience method to log that a method is
jaroslav@1258: * terminating by throwing an exception. The logging is done
jaroslav@1258: * using the FINER level.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the given message
jaroslav@1258: * level then the given arguments are stored in a LogRecord
jaroslav@1258: * which is forwarded to all registered output handlers. The
jaroslav@1258: * LogRecord's message is set to "THROW".
jaroslav@1258: *
jaroslav@1258: * Note that the thrown argument is stored in the LogRecord thrown
jaroslav@1258: * property, rather than the LogRecord parameters property. Thus is it
jaroslav@1258: * processed specially by output Formatters and is not treated
jaroslav@1258: * as a formatting parameter to the LogRecord message property.
jaroslav@1258: *
jaroslav@1258: * @param sourceClass name of class that issued the logging request
jaroslav@1258: * @param sourceMethod name of the method.
jaroslav@1258: * @param thrown The Throwable that is being thrown.
jaroslav@1258: */
jaroslav@1258: public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue || levelValue == offValue ) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: LogRecord lr = new LogRecord(Level.FINER, "THROW");
jaroslav@1258: lr.setSourceClassName(sourceClass);
jaroslav@1258: lr.setSourceMethodName(sourceMethod);
jaroslav@1258: lr.setThrown(thrown);
jaroslav@1258: doLog(lr);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: //=======================================================================
jaroslav@1258: // Start of simple convenience methods using level names as method names
jaroslav@1258: //=======================================================================
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a SEVERE message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the SEVERE message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void severe(String msg) {
jaroslav@1258: if (Level.SEVERE.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.SEVERE, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a WARNING message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the WARNING message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void warning(String msg) {
jaroslav@1258: if (Level.WARNING.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.WARNING, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log an INFO message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the INFO message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void info(String msg) {
jaroslav@1258: if (Level.INFO.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.INFO, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a CONFIG message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the CONFIG message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void config(String msg) {
jaroslav@1258: if (Level.CONFIG.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.CONFIG, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a FINE message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the FINE message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void fine(String msg) {
jaroslav@1258: if (Level.FINE.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.FINE, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a FINER message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the FINER message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void finer(String msg) {
jaroslav@1258: if (Level.FINER.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.FINER, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Log a FINEST message.
jaroslav@1258: *
jaroslav@1258: * If the logger is currently enabled for the FINEST message
jaroslav@1258: * level then the given message is forwarded to all the
jaroslav@1258: * registered output Handler objects.
jaroslav@1258: *
jaroslav@1258: * @param msg The string message (or a key in the message catalog)
jaroslav@1258: */
jaroslav@1258: public void finest(String msg) {
jaroslav@1258: if (Level.FINEST.intValue() < levelValue) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: log(Level.FINEST, msg);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: //================================================================
jaroslav@1258: // End of convenience methods
jaroslav@1258: //================================================================
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Set the log level specifying which message levels will be
jaroslav@1258: * logged by this logger. Message levels lower than this
jaroslav@1258: * value will be discarded. The level value Level.OFF
jaroslav@1258: * can be used to turn off logging.
jaroslav@1258: *
jaroslav@1258: * If the new level is null, it means that this node should
jaroslav@1258: * inherit its level from its nearest ancestor with a specific
jaroslav@1258: * (non-null) level value.
jaroslav@1258: *
jaroslav@1258: * @param newLevel the new value for the log level (may be null)
jaroslav@1258: * @exception SecurityException if a security manager exists and if
jaroslav@1258: * the caller does not have LoggingPermission("control").
jaroslav@1258: */
jaroslav@1258: public void setLevel(Level newLevel) throws SecurityException {
jaroslav@1258: checkAccess();
jaroslav@1258: synchronized (treeLock) {
jaroslav@1258: levelObject = newLevel;
jaroslav@1258: updateEffectiveLevel();
jaroslav@1258: }
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Get the log Level that has been specified for this Logger.
jaroslav@1258: * The result may be null, which means that this logger's
jaroslav@1258: * effective level will be inherited from its parent.
jaroslav@1258: *
jaroslav@1258: * @return this Logger's level
jaroslav@1258: */
jaroslav@1258: public Level getLevel() {
jaroslav@1258: return levelObject;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Check if a message of the given level would actually be logged
jaroslav@1258: * by this logger. This check is based on the Loggers effective level,
jaroslav@1258: * which may be inherited from its parent.
jaroslav@1258: *
jaroslav@1258: * @param level a message logging level
jaroslav@1258: * @return true if the given message level is currently being logged.
jaroslav@1258: */
jaroslav@1258: public boolean isLoggable(Level level) {
jaroslav@1258: if (level.intValue() < levelValue || levelValue == offValue) {
jaroslav@1258: return false;
jaroslav@1258: }
jaroslav@1258: return true;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Get the name for this logger.
jaroslav@1258: * @return logger name. Will be null for anonymous Loggers.
jaroslav@1258: */
jaroslav@1258: public String getName() {
jaroslav@1258: return name;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Add a log Handler to receive logging messages.
jaroslav@1258: *
jaroslav@1258: * By default, Loggers also send their output to their parent logger.
jaroslav@1258: * Typically the root Logger is configured with a set of Handlers
jaroslav@1258: * that essentially act as default handlers for all loggers.
jaroslav@1258: *
jaroslav@1258: * @param handler a logging Handler
jaroslav@1258: * @exception SecurityException if a security manager exists and if
jaroslav@1258: * the caller does not have LoggingPermission("control").
jaroslav@1258: */
jaroslav@1258: public void addHandler(Handler handler) throws SecurityException {
jaroslav@1258: // Check for null handler
jaroslav@1258: handler.getClass();
jaroslav@1258: checkAccess();
jaroslav@1258: handlers.add(handler);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Remove a log Handler.
jaroslav@1258: *
jaroslav@1258: * Returns silently if the given Handler is not found or is null
jaroslav@1258: *
jaroslav@1258: * @param handler a logging Handler
jaroslav@1258: * @exception SecurityException if a security manager exists and if
jaroslav@1258: * the caller does not have LoggingPermission("control").
jaroslav@1258: */
jaroslav@1258: public void removeHandler(Handler handler) throws SecurityException {
jaroslav@1258: checkAccess();
jaroslav@1258: if (handler == null) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: handlers.remove(handler);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Get the Handlers associated with this logger.
jaroslav@1258: *
jaroslav@1258: * @return an array of all registered Handlers
jaroslav@1258: */
jaroslav@1258: public Handler[] getHandlers() {
jaroslav@1258: return handlers.toArray(emptyHandlers);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Specify whether or not this logger should send its output
jaroslav@1258: * to its parent Logger. This means that any LogRecords will
jaroslav@1258: * also be written to the parent's Handlers, and potentially
jaroslav@1258: * to its parent, recursively up the namespace.
jaroslav@1258: *
jaroslav@1258: * @param useParentHandlers true if output is to be sent to the
jaroslav@1258: * logger's parent.
jaroslav@1258: * @exception SecurityException if a security manager exists and if
jaroslav@1258: * the caller does not have LoggingPermission("control").
jaroslav@1258: */
jaroslav@1258: public void setUseParentHandlers(boolean useParentHandlers) {
jaroslav@1258: checkAccess();
jaroslav@1258: this.useParentHandlers = useParentHandlers;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Discover whether or not this logger is sending its output
jaroslav@1258: * to its parent logger.
jaroslav@1258: *
jaroslav@1258: * @return true if output is to be sent to the logger's parent
jaroslav@1258: */
jaroslav@1258: public boolean getUseParentHandlers() {
jaroslav@1258: return useParentHandlers;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // Private utility method to map a resource bundle name to an
jaroslav@1258: // actual resource bundle, using a simple one-entry cache.
jaroslav@1258: // Returns null for a null name.
jaroslav@1258: // May also return null if we can't find the resource bundle and
jaroslav@1258: // there is no suitable previous cached value.
jaroslav@1258:
jaroslav@1258: private synchronized ResourceBundle findResourceBundle(String name) {
jaroslav@1258: // Return a null bundle for a null name.
jaroslav@1258: if (name == null) {
jaroslav@1258: return null;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: Locale currentLocale = Locale.getDefault();
jaroslav@1258:
jaroslav@1258: // Normally we should hit on our simple one entry cache.
jaroslav@1258: if (catalog != null && currentLocale == catalogLocale
jaroslav@1258: && name == catalogName) {
jaroslav@1258: return catalog;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // Use the thread's context ClassLoader. If there isn't one,
jaroslav@1258: // use the SystemClassloader.
jaroslav@1258: ClassLoader cl = Thread.currentThread().getContextClassLoader();
jaroslav@1258: if (cl == null) {
jaroslav@1258: cl = ClassLoader.getSystemClassLoader();
jaroslav@1258: }
jaroslav@1258: try {
jaroslav@1258: catalog = ResourceBundle.getBundle(name, currentLocale, cl);
jaroslav@1258: catalogName = name;
jaroslav@1258: catalogLocale = currentLocale;
jaroslav@1258: return catalog;
jaroslav@1258: } catch (MissingResourceException ex) {
jaroslav@1258: // Woops. We can't find the ResourceBundle in the default
jaroslav@1258: // ClassLoader. Drop through.
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: // Fall back to searching up the call stack and trying each
jaroslav@1258: // calling ClassLoader.
jaroslav@1258: for (int ix = 0; ; ix++) {
jaroslav@1258: Class clz = sun.reflect.Reflection.getCallerClass(ix);
jaroslav@1258: if (clz == null) {
jaroslav@1258: break;
jaroslav@1258: }
jaroslav@1258: ClassLoader cl2 = clz.getClassLoader();
jaroslav@1258: if (cl2 == null) {
jaroslav@1258: cl2 = ClassLoader.getSystemClassLoader();
jaroslav@1258: }
jaroslav@1258: if (cl == cl2) {
jaroslav@1258: // We've already checked this classloader.
jaroslav@1258: continue;
jaroslav@1258: }
jaroslav@1258: cl = cl2;
jaroslav@1258: try {
jaroslav@1258: catalog = ResourceBundle.getBundle(name, currentLocale, cl);
jaroslav@1258: catalogName = name;
jaroslav@1258: catalogLocale = currentLocale;
jaroslav@1258: return catalog;
jaroslav@1258: } catch (MissingResourceException ex) {
jaroslav@1258: // Ok, this one didn't work either.
jaroslav@1258: // Drop through, and try the next one.
jaroslav@1258: }
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: if (name.equals(catalogName)) {
jaroslav@1258: // Return the previous cached value for that name.
jaroslav@1258: // This may be null.
jaroslav@1258: return catalog;
jaroslav@1258: }
jaroslav@1258: // Sorry, we're out of luck.
jaroslav@1258: return null;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // Private utility method to initialize our one entry
jaroslav@1258: // resource bundle cache.
jaroslav@1258: // Note: for consistency reasons, we are careful to check
jaroslav@1258: // that a suitable ResourceBundle exists before setting the
jaroslav@1258: // ResourceBundleName.
jaroslav@1258: private synchronized void setupResourceInfo(String name) {
jaroslav@1258: if (name == null) {
jaroslav@1258: return;
jaroslav@1258: }
jaroslav@1258: ResourceBundle rb = findResourceBundle(name);
jaroslav@1258: if (rb == null) {
jaroslav@1258: // We've failed to find an expected ResourceBundle.
jaroslav@1258: throw new MissingResourceException("Can't find " + name + " bundle", name, "");
jaroslav@1258: }
jaroslav@1258: resourceBundleName = name;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Return the parent for this Logger.
jaroslav@1258: *
jaroslav@1258: * This method returns the nearest extant parent in the namespace.
jaroslav@1258: * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
jaroslav@1258: * has been created but no logger "a.b.c" exists, then a call of
jaroslav@1258: * getParent on the Logger "a.b.c.d" will return the Logger "a.b".
jaroslav@1258: *
jaroslav@1258: * The result will be null if it is called on the root Logger
jaroslav@1258: * in the namespace.
jaroslav@1258: *
jaroslav@1258: * @return nearest existing parent Logger
jaroslav@1258: */
jaroslav@1258: public Logger getParent() {
jaroslav@1258: // Note: this used to be synchronized on treeLock. However, this only
jaroslav@1258: // provided memory semantics, as there was no guarantee that the caller
jaroslav@1258: // would synchronize on treeLock (in fact, there is no way for external
jaroslav@1258: // callers to so synchronize). Therefore, we have made parent volatile
jaroslav@1258: // instead.
jaroslav@1258: return parent;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Set the parent for this Logger. This method is used by
jaroslav@1258: * the LogManager to update a Logger when the namespace changes.
jaroslav@1258: *
jaroslav@1258: * It should not be called from application code.
jaroslav@1258: *
jaroslav@1258: * @param parent the new parent logger
jaroslav@1258: * @exception SecurityException if a security manager exists and if
jaroslav@1258: * the caller does not have LoggingPermission("control").
jaroslav@1258: */
jaroslav@1258: public void setParent(Logger parent) {
jaroslav@1258: if (parent == null) {
jaroslav@1258: throw new NullPointerException();
jaroslav@1258: }
jaroslav@1258: manager.checkAccess();
jaroslav@1258: doSetParent(parent);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // Private method to do the work for parenting a child
jaroslav@1258: // Logger onto a parent logger.
jaroslav@1258: private void doSetParent(Logger newParent) {
jaroslav@1258:
jaroslav@1258: // System.err.println("doSetParent \"" + getName() + "\" \""
jaroslav@1258: // + newParent.getName() + "\"");
jaroslav@1258:
jaroslav@1258: synchronized (treeLock) {
jaroslav@1258:
jaroslav@1258: // Remove ourself from any previous parent.
jaroslav@1258: LogManager.LoggerWeakRef ref = null;
jaroslav@1258: if (parent != null) {
jaroslav@1258: // assert parent.kids != null;
jaroslav@1258: for (IteratorLogger.getGlobal()
.
jaroslav@1258: * For compatibility with old JDK versions where the
jaroslav@1258: * Logger.getGlobal()
is not available use the call
jaroslav@1258: * Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)
jaroslav@1258: * or Logger.getLogger("global")
.
jaroslav@1258: */
jaroslav@1258: @Deprecated
jaroslav@1258: public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Protected method to construct a logger for a named subsystem.
jaroslav@1258: * null
if none of
jaroslav@1258: * the messages require localization.
jaroslav@1258: * @return a suitable Logger
jaroslav@1258: * @throws MissingResourceException if the resourceBundleName is non-null and
jaroslav@1258: * no corresponding resource can be found.
jaroslav@1258: * @throws IllegalArgumentException if the Logger already exists and uses
jaroslav@1258: * a different resource bundle name.
jaroslav@1258: * @throws NullPointerException if the name is null.
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: // Synchronization is not required here. All synchronization for
jaroslav@1258: // adding a new Logger object is handled by LogManager.addLogger().
jaroslav@1258: public static Logger getLogger(String name, String resourceBundleName) {
jaroslav@1258: LogManager manager = LogManager.getLogManager();
jaroslav@1258: Logger result = manager.demandLogger(name);
jaroslav@1258: if (result.resourceBundleName == null) {
jaroslav@1258: // Note: we may get a MissingResourceException here.
jaroslav@1258: result.setupResourceInfo(resourceBundleName);
jaroslav@1258: } else if (!result.resourceBundleName.equals(resourceBundleName)) {
jaroslav@1258: throw new IllegalArgumentException(result.resourceBundleName +
jaroslav@1258: " != " + resourceBundleName);
jaroslav@1258: }
jaroslav@1258: return result;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Create an anonymous Logger. The newly created Logger is not
jaroslav@1258: * registered in the LogManager namespace. There will be no
jaroslav@1258: * access checks on updates to the logger.
jaroslav@1258: *