Class LogManager
- java.lang.Object
-
- java.util.logging.LogManager
-
public class LogManager extends Object
LogManageris used to maintain configuration properties of the logging framework, and to manage a hierarchical namespace of all namedLoggerobjects.There is only one global
LogManagerinstance in the application, which can be get by calling static methodgetLogManager(). This instance is created and initialized during class initialization and cannot be changed.The
LogManagerclass can be specified by java.util.logging.manager system property, if the property is unavailable or invalid, the default classLogManagerwill be used.On initialization,
LogManagerreads its configuration from a properties file, which by default is the "lib/logging.properties" in the JRE directory.However, two optional system properties can be used to customize the initial configuration process of
LogManager.- "java.util.logging.config.class"
- "java.util.logging.config.file"
These two properties can be set in three ways, by the Preferences API, by the "java" command line property definitions, or by system property definitions passed to JNI_CreateJavaVM.
The "java.util.logging.config.class" should specifies a class name. If it is set, this given class will be loaded and instantiated during
LogManagerinitialization, so that this object's default constructor can read the initial configuration and define properties forLogManager.If "java.util.logging.config.class" property is not set, or it is invalid, or some exception is thrown during the instantiation, then the "java.util.logging.config.file" system property can be used to specify a properties file. The
LogManagerwill read initial configuration from this file.If neither of these properties is defined, or some exception is thrown during these two properties using, the
LogManagerwill read its initial configuration from default properties file, as described above.The global logging properties may include:
- "handlers". This property's values should be a list of class names for
handler classes separated by whitespace, these classes must be subclasses of
Handlerand each must have a default constructor, these classes will be loaded, instantiated and registered as handlers on the rootLogger(theLoggernamed ""). TheseHandlers maybe initialized lazily. - "config". The property defines a list of class names separated by
whitespace. Each class must have a default constructor, in which it can
update the logging configuration, such as levels, handlers, or filters for
some logger, etc. These classes will be loaded and instantiated during
LogManagerconfiguration
This class, together with any handler and configuration classes associated with it, must be loaded from the system classpath when
LogManagerconfiguration occurs.Besides global properties, the properties for loggers and Handlers can be specified in the property files. The names of these properties will start with the complete dot separated names for the handlers or loggers.
In the
LogManager's hierarchical namespace,Loggersare organized based on their dot separated names. For example, "x.y.z" is child of "x.y".Levels for
Loggerscan be defined by properties whose name end with ".level". Thus "alogger.level" defines a level for the logger named as "alogger" and for all its children in the naming hierarchy. Log levels properties are read and applied in the same order as they are specified in the property file. The root logger's level can be defined by the property named as ".level".This class is thread safe. It is an error to synchronize on a
LogManagerwhile synchronized on aLogger.
-
-
Field Summary
Fields Modifier and Type Field Description static StringLOGGING_MXBEAN_NAMETheStringvalue of theLoggingMXBean's ObjectName.
-
Constructor Summary
Constructors Modifier Constructor Description protectedLogManager()Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddLogger(Logger logger)Add a given logger into the hierarchical namespace.voidaddPropertyChangeListener(PropertyChangeListener l)Add aPropertyChangeListener, which will be invoked when the properties are reread.voidcheckAccess()Does nothing.LoggergetLogger(String name)Get the logger with the given name.Enumeration<String>getLoggerNames()Get aEnumerationof all registered logger names.static LoggingMXBeangetLoggingMXBean()Get theLoggingMXBeaninstance. this implementation always throws an UnsupportedOperationException.static LogManagergetLogManager()Get the globalLogManagerinstance.StringgetProperty(String name)Get the value of property with given name.voidreadConfiguration()Re-initialize the properties and configuration.voidreadConfiguration(InputStream ins)Re-initialize the properties and configuration from the givenInputStreamvoidremovePropertyChangeListener(PropertyChangeListener l)Remove aPropertyChangeListener, do nothing if the given listener is not found.voidreset()Reset configuration.
-
-
-
Field Detail
-
LOGGING_MXBEAN_NAME
public static final String LOGGING_MXBEAN_NAME
TheStringvalue of theLoggingMXBean's ObjectName.- See Also:
- Constant Field Values
-
-
Method Detail
-
getLoggingMXBean
public static LoggingMXBean getLoggingMXBean()
Get theLoggingMXBeaninstance. this implementation always throws an UnsupportedOperationException.- Returns:
- the
LoggingMXBeaninstance
-
checkAccess
public void checkAccess()
Does nothing.
-
addLogger
public boolean addLogger(Logger logger)
Add a given logger into the hierarchical namespace. TheLogger.addLogger()factory methods call this method to add newly created Logger. This returns false if a logger with the given name has existed in the namespaceNote that the
LogManagermay only retain weak references to registered loggers. In order to preventLoggerobjects from being unexpectedly garbage collected it is necessary for applications to maintain references to them.- Parameters:
logger- the logger to be added.- Returns:
- true if the given logger is added into the namespace successfully, false if the given logger exists in the namespace.
-
getLogger
public Logger getLogger(String name)
Get the logger with the given name.- Parameters:
name- name of logger- Returns:
- logger with given name, or
nullif nothing is found.
-
getLoggerNames
public Enumeration<String> getLoggerNames()
Get aEnumerationof all registered logger names.- Returns:
- enumeration of registered logger names
-
getLogManager
public static LogManager getLogManager()
Get the globalLogManagerinstance.- Returns:
- the global
LogManagerinstance
-
getProperty
public String getProperty(String name)
Get the value of property with given name.- Parameters:
name- the name of property- Returns:
- the value of property
-
readConfiguration
public void readConfiguration() throws IOExceptionRe-initialize the properties and configuration. The initialization process is same as theLogManagerinstantiation.Notice : No
PropertyChangeEventare fired.- Throws:
IOException- if any IO related problems happened.
-
readConfiguration
public void readConfiguration(InputStream ins) throws IOException
Re-initialize the properties and configuration from the givenInputStreamNotice : No
PropertyChangeEventare fired.- Parameters:
ins- the input stream- Throws:
IOException- if any IO related problems happened.
-
reset
public void reset()
Reset configuration.All handlers are closed and removed from any named loggers. All loggers' level is set to null, except the root logger's level is set to
Level.INFO.
-
addPropertyChangeListener
public void addPropertyChangeListener(PropertyChangeListener l)
Add aPropertyChangeListener, which will be invoked when the properties are reread.- Parameters:
l- thePropertyChangeListenerto be added.
-
removePropertyChangeListener
public void removePropertyChangeListener(PropertyChangeListener l)
Remove aPropertyChangeListener, do nothing if the given listener is not found.- Parameters:
l- thePropertyChangeListenerto be removed.
-
-