Class Logger
public class Logger extends Object
Handler to actually do the destination-specific
operations.
Client applications can get named loggers by calling the getLogger
methods. They can also get anonymous loggers by calling the
getAnonymousLogger methods. Named loggers are organized in a
namespace hierarchy managed by a log manager. The naming convention is
usually the Java package naming convention. Anonymous loggers do not belong to any namespace.
Developers should use named loggers to enable logging to be controlled on a
per-Logger granularity. The recommended idiom is to create and assign the logger to
a static final field. This ensures that there's always a strong reference to the logger,
preventing it from being garbage collected. In particular, LogManager.addLogger(Logger)
will not keep your logger live.
Loggers "inherit" log level setting from their parent if their own level is
set to null. This is also true for the resource bundle. The logger's
resource bundle is used to localize the log messages if no resource bundle
name is given when a log method is called. If getUseParentHandlers()
returns true, loggers also inherit their parent's handlers. In this
context, "inherit" only means that "behavior" is inherited. The internal
field values will not change, for example, getLevel() still returns
null.
When loading a given resource bundle, the logger first tries to use the
context ClassLoader. If that fails, it tries the system ClassLoader. And if
that still fails, it searches up the class stack and uses each class's
ClassLoader to try to locate the resource bundle.
Some log methods accept log requests that do not specify the source class and source method. In these cases, the logging framework will automatically infer the calling class and method, but this is not guaranteed to be accurate.
Once a LogRecord object has been passed into the logging framework,
it is owned by the logging framework and the client applications should not
use it any longer.
All methods of this class are thread-safe.
- See Also:
LogManager
-
Field Summary
Fields Modifier and Type Field Description static LoggerglobalDeprecated.This is deadlock-prone.static StringGLOBAL_LOGGER_NAMEThe name of the global logger. -
Constructor Summary
-
Method Summary
Modifier and Type Method Description voidaddHandler(Handler handler)Adds a handler to this logger.voidconfig(String msg)Logs a message of levelLevel.CONFIG; the message is transmitted to all subscribed handlers.voidentering(String sourceClass, String sourceMethod)Logs a message indicating that a method has been entered.voidentering(String sourceClass, String sourceMethod, Object param)Logs a message indicating that a method has been entered.voidentering(String sourceClass, String sourceMethod, Object[] params)Logs a message indicating that a method has been entered.voidexiting(String sourceClass, String sourceMethod)Logs a message indicating that a method is exited.voidexiting(String sourceClass, String sourceMethod, Object result)Logs a message indicating that a method is exited.voidfine(String msg)Logs a message of levelLevel.FINE; the message is transmitted to all subscribed handlers.voidfiner(String msg)Logs a message of levelLevel.FINER; the message is transmitted to all subscribed handlers.voidfinest(String msg)Logs a message of levelLevel.FINEST; the message is transmitted to all subscribed handlers.static LoggergetAnonymousLogger()Gets an anonymous logger to use internally in a thread.static LoggergetAnonymousLogger(String resourceBundleName)Gets an anonymous logger to use internally in a thread.FiltergetFilter()Gets the filter used by this logger.static LoggergetGlobal()Returns the globalLogger.Handler[]getHandlers()Gets all the handlers associated with this logger.LevelgetLevel()Gets the logging level of this logger.static LoggergetLogger(String name)Gets a named logger.static LoggergetLogger(String name, String resourceBundleName)Gets a named logger associated with the supplied resource bundle.StringgetName()Gets the name of this logger,nullfor anonymous loggers.LoggergetParent()Gets the nearest parent of this logger in the namespace, anullvalue will be returned if called on the root logger.ResourceBundlegetResourceBundle()Gets the loaded resource bundle used by this logger to localize logging messages.StringgetResourceBundleName()Gets the name of the loaded resource bundle used by this logger to localize logging messages.booleangetUseParentHandlers()Gets the flag which indicates whether to use the handlers of this logger's parent to publish incoming log records, potentially recursively up the namespace.voidinfo(String msg)Logs a message of levelLevel.INFO; the message is transmitted to all subscribed handlers.booleanisLoggable(Level l)Determines whether this logger will actually log messages of the specified level.voidlog(Level logLevel, String msg)Logs a message of the specified level.voidlog(Level logLevel, String msg, Object param)Logs a message of the specified level with the supplied parameter.voidlog(Level logLevel, String msg, Object[] params)Logs a message of the specified level with the supplied parameter array.voidlog(Level logLevel, String msg, Throwable thrown)Logs a message of the specified level with the suppliedThrowableobject.voidlog(LogRecord record)Logs a given log record.voidlogp(Level logLevel, String sourceClass, String sourceMethod, String msg)Logs a message of the given level with the specified source class name and source method name.voidlogp(Level logLevel, String sourceClass, String sourceMethod, String msg, Object param)Logs a message of the given level with the specified source class name, source method name and parameter.voidlogp(Level logLevel, String sourceClass, String sourceMethod, String msg, Object[] params)Logs a message of the given level with the specified source class name, source method name and parameter array.voidlogp(Level logLevel, String sourceClass, String sourceMethod, String msg, Throwable thrown)Logs a message of the given level with the specified source class name, source method name andThrowableobject.voidlogrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg)Logs a message of the given level with the specified source class name and source method name, using the given resource bundle to localize the message.voidlogrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg, Object param)Logs a message of the given level with the specified source class name, source method name and parameter, using the given resource bundle to localize the message.voidlogrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg, Object[] params)Logs a message of the given level with the specified source class name, source method name and parameter array, using the given resource bundle to localize the message.voidlogrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg, Throwable thrown)Logs a message of the given level with the specified source class name, source method name andThrowableobject, using the given resource bundle to localize the message.voidremoveHandler(Handler handler)Removes a handler from this logger.voidsetFilter(Filter newFilter)Sets the filter used by this logger.voidsetLevel(Level newLevel)Sets the logging level for this logger.voidsetParent(Logger parent)Sets the parent of this logger in the namespace.voidsetUseParentHandlers(boolean notifyParentHandlers)Sets the flag which indicates whether to use the handlers of this logger's parent, potentially recursively up the namespace.voidsevere(String msg)Logs a message of levelLevel.SEVERE; the message is transmitted to all subscribed handlers.voidthrowing(String sourceClass, String sourceMethod, Throwable thrown)Logs a message indicating that an exception is thrown.voidwarning(String msg)Logs a message of levelLevel.WARNING; the message is transmitted to all subscribed handlers.
-
Field Details
-
GLOBAL_LOGGER_NAME
The name of the global logger. Before using this, see the discussion of how to useLoggerin the class documentation.- Since:
- 1.6
- See Also:
- Constant Field Values
-
global
Deprecated.This is deadlock-prone. UseLogger.getLogger(Logger.GLOBAL_LOGGER_NAME)as a direct replacement, but read the discussion of how to useLoggerin the class documentation.The global logger is provided as convenience for casual use.
-
-
Constructor Details
-
Logger
Constructs aLoggerobject with the supplied name and resource bundle name;notifiyParentHandlersis set totrue.Notice : Loggers use a naming hierarchy. Thus "z.x.y" is a child of "z.x".
- Parameters:
name- the name of this logger, may benullfor anonymous loggers.resourceBundleName- the name of the resource bundle used to localize logging messages, may benull.- Throws:
MissingResourceException- if the specified resource bundle can not be loaded.
-
-
Method Details
-
getAnonymousLogger
Gets an anonymous logger to use internally in a thread. Anonymous loggers are not registered in the log manager's namespace. No security checks will be performed when updating an anonymous logger's control settings.The anonymous loggers' parent is set to be the root logger. This way it inherits the default logging level and handlers from the root logger.
- Returns:
- a new instance of anonymous logger.
-
getAnonymousLogger
Gets an anonymous logger to use internally in a thread. Anonymous loggers are not registered in the log manager's namespace. No security checks will be performed when updating an anonymous logger's control settings.The anonymous loggers' parent is set to be the root logger. This way it inherits default logging level and handlers from the root logger.
- Parameters:
resourceBundleName- the name of the resource bundle used to localize log messages.- Returns:
- a new instance of anonymous logger.
- Throws:
MissingResourceException- if the specified resource bundle can not be loaded.
-
getLogger
Gets a named logger. The returned logger may already exist or may be newly created. In the latter case, its level will be set to the configured level according to theLogManager's properties.- Parameters:
name- the name of the logger to get, cannot benull.- Returns:
- a named logger.
- Throws:
MissingResourceException- If the specified resource bundle can not be loaded.
-
getLogger
Gets a named logger associated with the supplied resource bundle. The resource bundle will be used to localize logging messages.- Parameters:
name- the name of the logger to get, cannot benull.resourceBundleName- the name of the resource bundle, may benull.- Returns:
- a named logger.
- Throws:
IllegalArgumentException- if the logger identified bynameis associated with a resource bundle and its name is not equal toresourceBundleName.MissingResourceException- if the name of the resource bundle cannot be found.
-
getGlobal
Returns the globalLogger.- Since:
- 1.7
-
addHandler
Adds a handler to this logger. Thenamewill be fed with log records received by this logger.- Parameters:
handler- the handler object to add, cannot benull.
-
getHandlers
Gets all the handlers associated with this logger.- Returns:
- an array of all the handlers associated with this logger.
-
removeHandler
Removes a handler from this logger. If the specified handler does not exist then this method has no effect.- Parameters:
handler- the handler to be removed.
-
getFilter
Gets the filter used by this logger.- Returns:
- the filter used by this logger, may be
null.
-
setFilter
Sets the filter used by this logger.- Parameters:
newFilter- the filter to set, may benull.
-
getLevel
Gets the logging level of this logger. Anulllevel indicates that this logger inherits its parent's level.- Returns:
- the logging level of this logger.
-
setLevel
Sets the logging level for this logger. Anulllevel indicates that this logger will inherit its parent's level.- Parameters:
newLevel- the logging level to set.
-
getUseParentHandlers
public boolean getUseParentHandlers()Gets the flag which indicates whether to use the handlers of this logger's parent to publish incoming log records, potentially recursively up the namespace.- Returns:
trueif set to use parent's handlers,falseotherwise.
-
setUseParentHandlers
public void setUseParentHandlers(boolean notifyParentHandlers)Sets the flag which indicates whether to use the handlers of this logger's parent, potentially recursively up the namespace.- Parameters:
notifyParentHandlers- the new flag indicating whether to use the parent's handlers.
-
getParent
Gets the nearest parent of this logger in the namespace, anullvalue will be returned if called on the root logger.- Returns:
- the parent of this logger in the namespace.
-
setParent
Sets the parent of this logger in the namespace. This method should be used by theLogManagerobject only.- Parameters:
parent- the parent logger to set.
-
getName
Gets the name of this logger,nullfor anonymous loggers.- Returns:
- the name of this logger.
-
getResourceBundle
Gets the loaded resource bundle used by this logger to localize logging messages. If the value isnull, the parent's resource bundle will be inherited.- Returns:
- the loaded resource bundle used by this logger.
-
getResourceBundleName
Gets the name of the loaded resource bundle used by this logger to localize logging messages. If the value isnull, the parent's resource bundle name will be inherited.- Returns:
- the name of the loaded resource bundle used by this logger.
-
isLoggable
Determines whether this logger will actually log messages of the specified level. The effective level used to do the determination may be inherited from its parent. The default level isLevel.INFO.- Parameters:
l- the level to check.- Returns:
trueif this logger will actually log this level, otherwisefalse.
-
entering
Logs a message indicating that a method has been entered. A log record with log levelLevel.FINER, log message "ENTRY", the specified source class name and source method name is submitted for logging.- Parameters:
sourceClass- the calling class name.sourceMethod- the method name.
-
entering
Logs a message indicating that a method has been entered. A log record with log levelLevel.FINER, log message "ENTRY", the specified source class name, source method name and one parameter is submitted for logging.- Parameters:
sourceClass- the source class name.sourceMethod- the source method name.param- the parameter for the method call.
-
entering
Logs a message indicating that a method has been entered. A log record with log levelLevel.FINER, log message "ENTRY", the specified source class name, source method name and array of parameters is submitted for logging.- Parameters:
sourceClass- the source class name.sourceMethod- the source method name.params- an array of parameters for the method call.
-
exiting
Logs a message indicating that a method is exited. A log record with log levelLevel.FINER, log message "RETURN", the specified source class name and source method name is submitted for logging.- Parameters:
sourceClass- the calling class name.sourceMethod- the method name.
-
exiting
Logs a message indicating that a method is exited. A log record with log levelLevel.FINER, log message "RETURN", the specified source class name, source method name and return value is submitted for logging.- Parameters:
sourceClass- the source class name.sourceMethod- the source method name.result- the return value of the method call.
-
throwing
Logs a message indicating that an exception is thrown. A log record with log levelLevel.FINER, log message "THROW", the specified source class name, source method name and theThrowableobject is submitted for logging.- Parameters:
sourceClass- the source class name.sourceMethod- the source method name.thrown- theThrowableobject.
-
severe
Logs a message of levelLevel.SEVERE; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
warning
Logs a message of levelLevel.WARNING; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
info
Logs a message of levelLevel.INFO; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
config
Logs a message of levelLevel.CONFIG; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
fine
Logs a message of levelLevel.FINE; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
finer
Logs a message of levelLevel.FINER; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
finest
Logs a message of levelLevel.FINEST; the message is transmitted to all subscribed handlers.- Parameters:
msg- the message to log.
-
log
Logs a message of the specified level. The message is transmitted to all subscribed handlers.- Parameters:
logLevel- the level of the specified message.msg- the message to log.
-
log
Logs a message of the specified level with the supplied parameter. The message is then transmitted to all subscribed handlers.- Parameters:
logLevel- the level of the given message.msg- the message to log.param- the parameter associated with the event that is logged.
-
log
Logs a message of the specified level with the supplied parameter array. The message is then transmitted to all subscribed handlers.- Parameters:
logLevel- the level of the given messagemsg- the message to log.params- the parameter array associated with the event that is logged.
-
log
Logs a message of the specified level with the suppliedThrowableobject. The message is then transmitted to all subscribed handlers.- Parameters:
logLevel- the level of the given message.msg- the message to log.thrown- theThrowableobject associated with the event that is logged.
-
log
Logs a given log record. Only records with a logging level that is equal or greater than this logger's level will be submitted to this logger's handlers for logging. IfgetUseParentHandlers()returnstrue, the log record will also be submitted to the handlers of this logger's parent, potentially recursively up the namespace.Since all other log methods call this method to actually perform the logging action, subclasses of this class can override this method to catch all logging activities.
- Parameters:
record- the log record to be logged.
-
logp
Logs a message of the given level with the specified source class name and source method name.- Parameters:
logLevel- the level of the given message.sourceClass- the source class name.sourceMethod- the source method name.msg- the message to be logged.
-
logp
public void logp(Level logLevel, String sourceClass, String sourceMethod, String msg, Object param)Logs a message of the given level with the specified source class name, source method name and parameter.- Parameters:
logLevel- the level of the given messagesourceClass- the source class namesourceMethod- the source method namemsg- the message to be loggedparam- the parameter associated with the event that is logged.
-
logp
public void logp(Level logLevel, String sourceClass, String sourceMethod, String msg, Object[] params)Logs a message of the given level with the specified source class name, source method name and parameter array.- Parameters:
logLevel- the level of the given message.sourceClass- the source class name.sourceMethod- the source method name.msg- the message to be logged.params- the parameter array associated with the event that is logged.
-
logp
public void logp(Level logLevel, String sourceClass, String sourceMethod, String msg, Throwable thrown)Logs a message of the given level with the specified source class name, source method name andThrowableobject.- Parameters:
logLevel- the level of the given message.sourceClass- the source class name.sourceMethod- the source method name.msg- the message to be logged.thrown- theThrowableobject.
-
logrb
public void logrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg)Logs a message of the given level with the specified source class name and source method name, using the given resource bundle to localize the message. IfbundleNameis null, the empty string or not valid then the message is not localized.- Parameters:
logLevel- the level of the given message.sourceClass- the source class name.sourceMethod- the source method name.bundleName- the name of the resource bundle used to localize the message.msg- the message to be logged.
-
logrb
public void logrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg, Object param)Logs a message of the given level with the specified source class name, source method name and parameter, using the given resource bundle to localize the message. IfbundleNameis null, the empty string or not valid then the message is not localized.- Parameters:
logLevel- the level of the given message.sourceClass- the source class name.sourceMethod- the source method name.bundleName- the name of the resource bundle used to localize the message.msg- the message to be logged.param- the parameter associated with the event that is logged.
-
logrb
public void logrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg, Object[] params)Logs a message of the given level with the specified source class name, source method name and parameter array, using the given resource bundle to localize the message. IfbundleNameis null, the empty string or not valid then the message is not localized.- Parameters:
logLevel- the level of the given message.sourceClass- the source class name.sourceMethod- the source method name.bundleName- the name of the resource bundle used to localize the message.msg- the message to be logged.params- the parameter array associated with the event that is logged.
-
logrb
public void logrb(Level logLevel, String sourceClass, String sourceMethod, String bundleName, String msg, Throwable thrown)Logs a message of the given level with the specified source class name, source method name andThrowableobject, using the given resource bundle to localize the message. IfbundleNameis null, the empty string or not valid then the message is not localized.- Parameters:
logLevel- the level of the given messagesourceClass- the source class namesourceMethod- the source method namebundleName- the name of the resource bundle used to localize the message.msg- the message to be logged.thrown- theThrowableobject.
-