📄 logger.java
字号:
/* Logger.java -- a class for logging messages Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.util.logging;import java.util.List;import java.util.MissingResourceException;import java.util.ResourceBundle;/** * A Logger is used for logging information about events. Usually, there * is a seprate logger for each subsystem or component, although there * is a shared instance for components that make only occasional use of * the logging framework. * * <p>It is common to name a logger after the name of a corresponding * Java package. Loggers are organized into a hierarchical namespace; * for example, the logger <code>"org.gnu.foo"</code> is the * <em>parent</em> of logger <code>"org.gnu.foo.bar"</code>. * * <p>A logger for a named subsystem can be obtained through {@link * java.util.logging.Logger#getLogger(java.lang.String)}. However, * only code which has been granted the permission to control the * logging infrastructure will be allowed to customize that logger. * Untrusted code can obtain a private, anonymous logger through * {@link #getAnonymousLogger()} if it wants to perform any * modifications to the logger. * * <p>FIXME: Write more documentation. * * @author Sascha Brawer (brawer@acm.org) */public class Logger{ /** * A logger provided to applications that make only occasional use * of the logging framework, typically early prototypes. Serious * products are supposed to create and use their own Loggers, so * they can be controlled individually. */ public static final Logger global = getLogger("global"); /** * The name of the Logger, or <code>null</code> if the logger is * anonymous. * * <p>A previous version of the GNU Classpath implementation granted * untrusted code the permission to control any logger whose name * was null. However, test code revealed that the Sun J2SE 1.4 * reference implementation enforces the security control for any * logger that was not created through getAnonymousLogger, even if * it has a null name. Therefore, a separate flag {@link * Logger#anonymous} was introduced. */ private final String name; /** * The name of the resource bundle used for localization. * * <p>This variable cannot be declared as <code>final</code> * because its value can change as a result of calling * getLogger(String,String). */ private String resourceBundleName; /** * The resource bundle used for localization. * * <p>This variable cannot be declared as <code>final</code> * because its value can change as a result of calling * getLogger(String,String). */ private ResourceBundle resourceBundle; private Filter filter; private final List handlerList = new java.util.ArrayList(4); private Handler[] handlers = new Handler[0]; /** * Indicates whether or not this logger is anonymous. While * a LoggingPermission is required for any modifications to * a normal logger, untrusted code can obtain an anonymous logger * and modify it according to its needs. * * <p>A previous version of the GNU Classpath implementation * granted access to every logger whose name was null. * However, test code revealed that the Sun J2SE 1.4 reference * implementation enforces the security control for any logger * that was not created through getAnonymousLogger, even * if it has a null name. */ private boolean anonymous; private boolean useParentHandlers; private Level level; private Logger parent; /** * Constructs a Logger for a subsystem. Most applications do not * need to create new Loggers explicitly; instead, they should call * the static factory methods * {@link #getLogger(java.lang.String,java.lang.String) getLogger} * (with ResourceBundle for localization) or * {@link #getLogger(java.lang.String) getLogger} (without * ResourceBundle), respectively. * * @param name the name for the logger, for example "java.awt" * or "com.foo.bar". The name should be based on * the name of the package issuing log records * and consist of dot-separated Java identifiers. * * @param resourceBundleName the name of a resource bundle * for localizing messages, or <code>null</code> * to indicate that messages do not need to be localized. * * @throws java.util.MissingResourceException if * <code>resourceBundleName</code> is not <code>null</code> * and no such bundle could be located. */ protected Logger(String name, String resourceBundleName) throws MissingResourceException { this.name = name; this.resourceBundleName = resourceBundleName; if (resourceBundleName == null) resourceBundle = null; else resourceBundle = ResourceBundle.getBundle(resourceBundleName); level = null; /* This is null when the root logger is being constructed, * and the root logger afterwards. */ parent = LogManager.getLogManager().rootLogger; useParentHandlers = (parent != null); } /** * Finds a registered logger for a subsystem, or creates one in * case no logger has been registered yet. * * @param name the name for the logger, for example "java.awt" * or "com.foo.bar". The name should be based on * the name of the package issuing log records * and consist of dot-separated Java identifiers. * * @throws IllegalArgumentException if a logger for the subsystem * identified by <code>name</code> has already been created, * but uses a a resource bundle for localizing messages. * * @throws NullPointerException if <code>name</code> is * <code>null</code>. * * @return a logger for the subsystem specified by <code>name</code> * that does not localize messages. */ public static Logger getLogger(String name) { return getLogger(name, null); } /** * Finds a registered logger for a subsystem, or creates one in case * no logger has been registered yet. * * <p>If a logger with the specified name has already been * registered, the behavior depends on the resource bundle that is * currently associated with the existing logger. * * <ul><li>If the existing logger uses the same resource bundle as * specified by <code>resourceBundleName</code>, the existing logger * is returned.</li> * * <li>If the existing logger currently does not localize messages, * the existing logger is modified to use the bundle specified by * <code>resourceBundleName</code>. The existing logger is then * returned. Therefore, all subsystems currently using this logger * will produce localized messages from now on.</li> * * <li>If the existing logger already has an associated resource * bundle, but a different one than specified by * <code>resourceBundleName</code>, an * <code>IllegalArgumentException</code> is thrown.</li></ul> * * @param name the name for the logger, for example "java.awt" * or "org.gnu.foo". The name should be based on * the name of the package issuing log records * and consist of dot-separated Java identifiers. * * @param resourceBundleName the name of a resource bundle * for localizing messages, or <code>null</code> * to indicate that messages do not need to be localized. * * @return a logger for the subsystem specified by <code>name</code>. * * @throws java.util.MissingResourceException if * <code>resourceBundleName</code> is not <code>null</code> * and no such bundle could be located. * * @throws IllegalArgumentException if a logger for the subsystem * identified by <code>name</code> has already been created, * but uses a different resource bundle for localizing * messages. * * @throws NullPointerException if <code>name</code> is * <code>null</code>. */ public static Logger getLogger(String name, String resourceBundleName) { LogManager lm = LogManager.getLogManager(); Logger result; /* Throw NullPointerException if name is null. */ name.getClass(); /* Without synchronized(lm), it could happen that another thread * would create a logger between our calls to getLogger and * addLogger. While addLogger would indicate this by returning * false, we could not be sure that this other logger was still * existing when we called getLogger a second time in order * to retrieve it -- note that LogManager is only allowed to * keep weak references to registered loggers, so Loggers * can be garbage collected at any time in general, and between * our call to addLogger and our second call go getLogger * in particular. * * Of course, we assume here that LogManager.addLogger etc. * are synchronizing on the global LogManager object. There * is a comment in the implementation of LogManager.addLogger * referring to this comment here, so that any change in * the synchronization of LogManager will be reflected here. */ synchronized (lm) { result = lm.getLogger(name); if (result == null) { boolean couldBeAdded; result = new Logger(name, resourceBundleName); couldBeAdded = lm.addLogger(result); if (!couldBeAdded) throw new IllegalStateException("cannot register new logger"); } else { /* The logger already exists. Make sure it uses * the same resource bundle for localizing messages. */ String existingBundleName = result.getResourceBundleName(); /* The Sun J2SE 1.4 reference implementation will return the * registered logger object, even if it does not have a resource * bundle associated with it. However, it seems to change the * resourceBundle of the registered logger to the bundle * whose name was passed to getLogger. */ if ((existingBundleName == null) && (resourceBundleName != null)) { /* If ResourceBundle.getBundle throws an exception, the * existing logger will be unchanged. This would be * different if the assignment to resourceBundleName * came first. */ result.resourceBundle = ResourceBundle.getBundle(resourceBundleName); result.resourceBundleName = resourceBundleName; return result; } if ((existingBundleName != resourceBundleName) && ((existingBundleName == null) || !existingBundleName.equals(resourceBundleName))) { throw new IllegalArgumentException(); } } } return result; } /** * Creates a new, unnamed logger. Unnamed loggers are not * registered in the namespace of the LogManager, and no special * security permission is required for changing their state. * Therefore, untrusted applets are able to modify their private * logger instance obtained through this method. * * <p>The parent of the newly created logger will the the root * logger, from which the level threshold and the handlers are * inherited. */ public static Logger getAnonymousLogger() { return getAnonymousLogger(null); } /** * Creates a new, unnamed logger. Unnamed loggers are not * registered in the namespace of the LogManager, and no special * security permission is required for changing their state. * Therefore, untrusted applets are able to modify their private * logger instance obtained through this method. * * <p>The parent of the newly created logger will the the root * logger, from which the level threshold and the handlers are * inherited. * * @param resourceBundleName the name of a resource bundle * for localizing messages, or <code>null</code> * to indicate that messages do not need to be localized. * * @throws java.util.MissingResourceException if * <code>resourceBundleName</code> is not <code>null</code> * and no such bundle could be located. */ public static Logger getAnonymousLogger(String resourceBundleName) throws MissingResourceException { Logger result; result = new Logger(null, resourceBundleName); result.anonymous = true; return result; } /** * Returns the name of the resource bundle that is being used for * localizing messages. * * @return the name of the resource bundle used for localizing messages, * or <code>null</code> if the parent's resource bundle * is used for this purpose. */ public synchronized String getResourceBundleName() { return resourceBundleName; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -