The first and foremost advantage of
any logging API over plain System.out.println
resides in its ability to disable
certain log statements while allowing
others to print unhindered. This capability
assumes that the logging space, that
is, the space of all possible logging
statements, is categorized according
to some developer-chosen criteria.
This observation had previously led
to choose category as the central
concept of the package. However, since
log4j version 1.2, Logger class has
replaced the Category class. For those
familiar with earlier versions of
log4j, the Logger class can be considered
as a mere alias to the Category class.
Loggers are named entities. Logger
names are case-sensitive and they
follow the hierarchical naming rule.
A logger is said to be an ancestor of another logger if its name followed
by a dot is a prefix of the descendant logger name. A logger is said to be
a parent of a child logger if there are no ancestors between
itself and the descendant logger.
For example, the logger named "com.parent"
is a parent of the logger named "com.parent.child".
Similarly, "ancestor" is
a parent of "ancestor.parent"
and an ancestor of " ancestor.parent.child".
The root logger resides at the top
of the logger hierarchy. It is exceptional
in two ways:
1. it always exists,
2. it cannot be retrieved by name.
Invoking the class static Logger.getRootLogger
method retrieves it. All other loggers
are instantiated and retrieved with
the class static Logger.getLogger
method. This method takes the name
of the desired logger as a parameter.
Some of the basic methods in the Logger
class are listed below.
The logger is the core component of
the logging process. In log4j, there
are 5 normal levels Levels of logger
available (not including custom Levels),
the following is borrowed from the
log4j API (http://jakarta.apache.org/log4j/docs/api/index.html):
Level DEBUG - The DEBUG Level
designates fine-grained informational
events that are most useful to debug
static Level INFO - The INFO level designates informational
messages that highlight the progress
of the application at coarse-grained
static Level WARN - The WARN level designates potentially
static Level ERROR -
The ERROR level designates error events
that might still allow the application
to continue running.
static Level FATAL - The FATAL level designates very
severe error events that will presumably
lead the application to abort.
In addition, there are two special
levels of logging available: (descriptions
borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html):
static Level ALL -The ALL Level has the lowest possible
rank and is intended to turn on all
static Level OFF - The OFF Level has the highest possible
rank and is intended to turn off logging.
There are a number of ways to create
a logger, one can retrieve the root
Logger logger = Logger.getRootLogger();
One can create a new logger:
logger = Logger.getLogger("MyLogger");
More usually, one instantiates a static
logger globally, based on the name
of the class:
static Logger logger = Logger.getLogger(test.class);
All these create a logger called "logger",
one can set the level with:
You can use any of 7 levels; Level.DEBUG,
Level.INFO, Level.WARN, Level.ERROR,
Level.FATAL, Level.ALL and Level.OFF.