public class Logger extends Object
Logger objects may be obtained by calls on one of the getLogger
factory methods. These will either create a new Logger or
return a suitable existing Logger. It is important to note that
the Logger returned by one of the getLogger
factory methods
may be garbage collected at any time if a strong reference to the
Logger is not kept.
Logging messages will be forwarded to registered Handler objects, which can forward the messages to a variety of destinations, including consoles, files, OS logs, etc.
Each Logger keeps track of a "parent" Logger, which is its nearest existing ancestor in the Logger namespace.
Each Logger has a "Level" associated with it. This reflects a minimum Level that this logger cares about. If a Logger's level is set to null, then its effective level is inherited from its parent, which may in turn obtain it recursively from its parent, and so on up the tree.
The log level can be dynamically changed by calls on the Logger.setLevel method.
On each logging call the Logger initially performs a cheap check of the request level (e.g., SEVERE or FINE) against the effective log level of the logger. If the request level is lower than the log level, the logging call returns immediately.
After passing this initial (cheap) test, the Logger will allocate a LogRecord to describe the logging message. It will then call a Filter (if present) to do a more detailed check on whether the record should be published. If that passes it will then publish the LogRecord to its output Handlers. By default, loggers also publish to their parent's Handlers, recursively up the tree.
The CLDC version of this class does not support Resource bundles and cannot be subclassed.
Formatting is the responsibility of the output Handler, which will typically call a Formatter.
Note that formatting need not occur synchronously. It may be delayed until a LogRecord is actually written to an external sink.
The logging methods are grouped in the following main categories:
There are a set of "log" methods that take a log level, a message string, and optionally some parameters to the message string.
There are a set of "logp" methods (for "log precise") that are like the "log" methods, but also take an explicit source class name and method name.
For the methods that do not take an explicit source name and method name, the Logging framework will make a "best effort" to determine which class and method called into the logging method. However, it is important to realize that this automatically inferred information may only be approximate (or may even be quite wrong!). Virtual machines are allowed to do extensive optimizations when JITing and may entirely remove stack frames, making it impossible to reliably locate the calling class and method.
All methods on Logger are multi-thread safe.
Subclassing Information: Note that a LogManager class may provide its own implementation of named Loggers for any point in the namespace. Therefore, any subclasses of Logger (unless they are implemented in conjunction with a new LogManager class) should take care to obtain a Logger instance from the LogManager class and should delegate operations such as "isLoggable" and "log(LogRecord)" to that instance. Note that in order to intercept all logging output, subclasses need only override the log(LogRecord) method. All the other logging methods are implemented as calls on this log(LogRecord) method.
Modifier and Type | Field and Description |
---|---|
static String |
GLOBAL_LOGGER_NAME
GLOBAL_LOGGER_NAME is a name for the global logger.
|
Modifier and Type | Method and Description |
---|---|
void |
addHandler(Handler handler)
Add a log Handler to receive logging messages.
|
static Logger |
getAnonymousLogger()
Create an anonymous Logger.
|
Filter |
getFilter()
Get the current filter for this Logger.
|
static Logger |
getGlobal()
Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
|
Handler[] |
getHandlers()
Get the Handlers associated with this logger.
|
Level |
getLevel()
Get the log Level that has been specified for this Logger.
|
static Logger |
getLogger(String name)
Find or create a logger for a named subsystem.
|
String |
getName()
Get the name for this logger.
|
Logger |
getParent()
Return the parent for this Logger.
|
boolean |
getUseParentHandlers()
Discover whether or not this logger is sending its output
to its parent logger.
|
boolean |
isLoggable(Level level)
Check if a message of the given level would actually be logged
by this logger.
|
void |
log(Level level,
String msg)
Log a message, with no arguments.
|
void |
log(Level level,
String msg,
Object param1)
Log a message, with one object parameter.
|
void |
log(Level level,
String msg,
Object[] params)
Log a message, with an array of object arguments.
|
void |
log(Level level,
String msg,
Throwable thrown)
Log a message, with associated Throwable information.
|
void |
log(LogRecord record)
Log a LogRecord.
|
void |
logp(Level level,
String sourceClass,
String sourceMethod,
String msg)
Log a message, specifying source class and method,
with no arguments.
|
void |
logp(Level level,
String sourceClass,
String sourceMethod,
String msg,
Object param1)
Log a message, specifying source class and method,
with a single object parameter to the log message.
|
void |
logp(Level level,
String sourceClass,
String sourceMethod,
String msg,
Object[] params)
Log a message, specifying source class and method,
with an array of object arguments.
|
void |
logp(Level level,
String sourceClass,
String sourceMethod,
String msg,
Throwable thrown)
Log a message, specifying source class and method,
with associated Throwable information.
|
void |
removeHandler(Handler handler)
Remove a log Handler.
|
void |
setFilter(Filter newFilter)
Set a filter to control output on this Logger.
|
void |
setLevel(Level newLevel)
Set the log level specifying which message levels will be
logged by this logger.
|
void |
setParent(Logger parent)
Set the parent for this Logger.
|
void |
setUseParentHandlers(boolean useParentHandlers)
Specify whether or not this logger should send its output
to its parent Logger.
|
public static final String GLOBAL_LOGGER_NAME
public void addHandler(Handler handler) throws SecurityException
By default, Loggers also send their output to their parent logger. Typically the root Logger is configured with a set of Handlers that essentially act as default handlers for all loggers.
handler
- a logging HandlerSecurityException
- if a security manager exists,
this logger is not anonymous, and the caller
does not have LoggingPermission("control").public static Logger getAnonymousLogger()
Even although the new logger is anonymous, it is configured to have the root logger ("") as its parent. This means that by default it inherits its effective level and handlers from the root logger.
public Filter getFilter()
public static final Logger getGlobal()
public Handler[] getHandlers()
public Level getLevel()
public static Logger getLogger(String name)
If a new logger is created its log level will be configured based on the LogManager configuration and it will configured to also send logging output to its parent's Handlers. It will be registered in the LogManager global namespace.
Note: The LogManager may only retain a weak reference to the newly
created Logger. It is important to understand that a previously
created Logger with the given name may be garbage collected at any
time if there is no strong reference to the Logger. In particular,
this means that two back-to-back calls like
getLogger("MyLogger").log(...)
may use different Logger
objects named "MyLogger" if there is no strong reference to the
Logger named "MyLogger" elsewhere in the program.
name
- A name for the logger. This should
be a dot-separated name and should normally
be based on the package name or class name
of the subsystem, such as java.net
or javax.swingNullPointerException
- if the name is null.public String getName()
public Logger getParent()
This method returns the nearest extant parent in the namespace. Thus if a Logger is called "a.b.c.d", and a Logger called "a.b" has been created but no logger "a.b.c" exists, then a call of getParent on the Logger "a.b.c.d" will return the Logger "a.b".
The result will be null if it is called on the root Logger in the namespace.
public boolean getUseParentHandlers()
public boolean isLoggable(Level level)
level
- a message logging levelpublic void log(Level level, String msg)
If the logger is currently enabled for the given message level then the given message is forwarded to all the registered output Handler objects.
level
- One of the message level identifiers, e.g., SEVEREmsg
- The string message (or a key in the message catalog)public void log(Level level, String msg, Object param1)
If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.
level
- One of the message level identifiers, e.g., SEVEREmsg
- The string message (or a key in the message catalog)param1
- parameter to the messagepublic void log(Level level, String msg, Object[] params)
If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.
level
- One of the message level identifiers, e.g., SEVEREmsg
- The string message (or a key in the message catalog)params
- array of parameters to the messagepublic void log(Level level, String msg, Throwable thrown)
If the logger is currently enabled for the given message level then the given arguments are stored in a LogRecord which is forwarded to all registered output handlers.
Note that the thrown argument is stored in the LogRecord thrown property, rather than the LogRecord parameters property. Thus it is processed specially by output Formatters and is not treated as a formatting parameter to the LogRecord message property.
level
- One of the message level identifiers, e.g., SEVEREmsg
- The string message (or a key in the message catalog)thrown
- Throwable associated with log message.public void log(LogRecord record)
All the other logging methods in this class call through this method to actually perform any logging. Subclasses can override this single method to capture all log activity.
record
- the LogRecord to be publishedpublic void logp(Level level, String sourceClass, String sourceMethod, String msg)
If the logger is currently enabled for the given message level then the given message is forwarded to all the registered output Handler objects.
level
- One of the message level identifiers, e.g., SEVEREsourceClass
- name of class that issued the logging requestsourceMethod
- name of method that issued the logging requestmsg
- The string message (or a key in the message catalog)public void logp(Level level, String sourceClass, String sourceMethod, String msg, Object param1)
If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.
level
- One of the message level identifiers, e.g., SEVEREsourceClass
- name of class that issued the logging requestsourceMethod
- name of method that issued the logging requestmsg
- The string message (or a key in the message catalog)param1
- Parameter to the log message.public void logp(Level level, String sourceClass, String sourceMethod, String msg, Object[] params)
If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.
level
- One of the message level identifiers, e.g., SEVEREsourceClass
- name of class that issued the logging requestsourceMethod
- name of method that issued the logging requestmsg
- The string message (or a key in the message catalog)params
- Array of parameters to the messagepublic void logp(Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown)
If the logger is currently enabled for the given message level then the given arguments are stored in a LogRecord which is forwarded to all registered output handlers.
Note that the thrown argument is stored in the LogRecord thrown property, rather than the LogRecord parameters property. Thus it is processed specially by output Formatters and is not treated as a formatting parameter to the LogRecord message property.
level
- One of the message level identifiers, e.g., SEVEREsourceClass
- name of class that issued the logging requestsourceMethod
- name of method that issued the logging requestmsg
- The string message (or a key in the message catalog)thrown
- Throwable associated with log message.public void removeHandler(Handler handler) throws SecurityException
Returns silently if the given Handler is not found or is null
handler
- a logging HandlerSecurityException
- if a security manager exists,
this logger is not anonymous, and the caller
does not have LoggingPermission("control").public void setFilter(Filter newFilter) throws SecurityException
After passing the initial "level" check, the Logger will call this Filter to check if a log record should really be published.
newFilter
- a filter object (may be null)SecurityException
- if a security manager exists,
this logger is not anonymous, and the caller
does not have LoggingPermission("control").public void setLevel(Level newLevel) throws SecurityException
If the new level is null, it means that this node should inherit its level from its nearest ancestor with a specific (non-null) level value.
newLevel
- the new value for the log level (may be null)SecurityException
- if a security manager exists,
this logger is not anonymous, and the caller
does not have LoggingPermission("control").public void setParent(Logger parent)
It should not be called from application code.
parent
- the new parent loggerSecurityException
- if a security manager exists and if
the caller does not have LoggingPermission("control").public void setUseParentHandlers(boolean useParentHandlers)
useParentHandlers
- true if output is to be sent to the
logger's parent.SecurityException
- if a security manager exists,
this logger is not anonymous, and the caller
does not have LoggingPermission("control").Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. Use of this specification is subject to license terms.