public class Logger extends Object
Loggers normally have a name, which is an arbitrary string. Named Loggers are created with the factory getLogger(String)
. This
method will either create a new Logger or return a suitable existing Logger, using the name.
It also possible to create anonymous Loggers which are independent from the named Loggers and independent from one another.
Each Logger has an associated Level
. This reflects a minimum level that this Logger will care about. If the level is high, the
Logger will only care about important messages. When it low, it will care about more messages. Caring means logging. The Level can be
changed dynamically, which is useful to augment or diminish the amount of messages that are actually logged.
The names of the logging methods, such as severe(String)
or finer(String)
denote the importance of the message. Their
names match the standard levels given by the class Level
. When using one of these methods, the Logger performs a test to compare
its own Level and the Level associated with the logging request. If the level of the request is lower than the Logger's level, nothing
happens. Otherwise, the Logger forwards the message to a list of Handler
s that have been added with the method
addHandler(Handler)
.
Modifier and Type | Field and Description |
---|---|
protected Level |
effectiveLevel
This is the real level of the logger.
|
static String |
GLOBAL_LOGGER_NAME
This is a name for the global logger.
|
protected Level |
publicLevel
This is the level set with
setLevel(Level) and returned by getLevel() . |
Modifier | Constructor and Description |
---|---|
protected |
Logger(String name,
String resourceBundleName)
Protected method to create a logger.
|
Modifier and Type | Method and Description |
---|---|
void |
addHandler(Handler handler)
Add a log Handler to the list of Handlers known by the Logger.
|
void |
config(String msg)
Log a CONFIG message.
|
void |
fine(String msg)
Log a FINE message.
|
void |
finer(String msg)
Log a FINER message.
|
void |
finest(String msg)
Log a FINEST message.
|
static Logger |
getAnonymousLogger()
Each call to this method creates a new Logger.
|
static Logger |
getGlobal()
Get the logger with the name Logger.GLOBAL_LOGGER_NAME.
|
Level |
getLevel()
Get the current level of this Logger.
|
static Logger |
getLogger(String name)
Find or create a logger.
|
String |
getName()
Get the name of this logger.
|
Logger |
getParent()
Get the parent of this logger.
|
void |
info(String msg)
Log an INFO message.
|
boolean |
isLoggable(Level level)
Check if a message of the given level would be logged by this Logger.
|
void |
log(Level level,
String msg)
Log a message, with no arguments.
|
void |
log(Level level,
String msg,
Throwable thrown)
Log a message, with an associated throwable object.
|
void |
log(LogRecord record)
Log a LogRecord.
|
void |
removeHandler(Handler handler)
Remove the given Handler from the Logger's list.
|
void |
setLevel(Level newLevel)
Set the specified Level to the Logger.
|
void |
severe(String msg)
Log a SEVERE message.
|
void |
throwing(String sourceClass,
String sourceMethod,
Throwable thrown)
Log a that a method is terminating by throwing an Exception.
|
void |
warning(String msg)
Log a WARNING message.
|
protected Level effectiveLevel
public static final String GLOBAL_LOGGER_NAME
protected Level publicLevel
setLevel(Level)
and returned by getLevel()
. It may be null, that's why implementation
will use the effectiveLevel
to determine whether a message must be logged or not.public void addHandler(Handler handler)
handler
- a new Handler for the Loggerpublic void config(String msg)
If the logger is currently enabled for the CONFIG message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.public void fine(String msg)
If the logger is currently enabled for the FINE message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.public void finer(String msg)
If the logger is currently enabled for the FINER message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.public void finest(String msg)
If the logger is currently enabled for the FINEST message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.public static Logger getAnonymousLogger()
public static final Logger getGlobal()
public Level getLevel()
public static Logger getLogger(String name)
If a logger has already been with the same name, this logger is returned (note that case is not taken into account). Otherwise a new logger is created.
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
- used to identify a logger. Case is not taken into account.NullPointerException
- if name is null
.public String getName()
public Logger getParent()
null
for the root loggerpublic void info(String msg)
If the logger is currently enabled for the INFO message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.public boolean isLoggable(Level level)
level
- a logging Level.true
if the message would be logged.public void log(Level level, String msg)
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, Throwable thrown)
level
- One of the message level identifiers, e.g. SEVEREmsg
- The string message (or a key in the message catalog)thrown
- The throwable associated with the messagepublic void log(LogRecord record)
record
- the log record to publishpublic void removeHandler(Handler handler)
handler
- the Handler to removepublic void setLevel(Level newLevel)
Level.OFF can be used to turn off logging.
newLevel
- the new Level for the Logger (may be null)public void severe(String msg)
If the logger is currently enabled for the SEVERE message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.public void throwing(String sourceClass, String sourceMethod, Throwable thrown)
sourceClass
- name of the class that requested the logsourceMethod
- name of the method inside this classthrown
- the throwable being thrownpublic void warning(String msg)
If the logger is currently enabled for the WARNING message level then the given message is forwarded to all the registered output handler objects.
msg
- message to log.