Log4j Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 30: Line 30:


A LoggerConfig is said to be a ''parent'' of a ''child'' LoggerConfig is there are no ancestors between itself and the descendant LoggerConfig.
A LoggerConfig is said to be a ''parent'' of a ''child'' LoggerConfig is there are no ancestors between itself and the descendant LoggerConfig.
==Root LoggerConfig==
The root LoggerConfig resides at the top of the LoggerConfig hierarchy. It always exists and it is part of every hierarchy. The [[#Logger|Logger]] that is directly linked to the root LoggerConfig can be obtained as follows:
<pre>
Logger root = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
</pre>


=Log Levels=
=Log Levels=

Revision as of 00:11, 8 August 2016

Internal

Overview

The main advantage of log4j over System.out.println() is the ability to disable certain log statements while allowing others, via configuration.

Logger

Logger is the central class in the log4j package. Most logging operations, except configuration, are done through this class.

Category deprecation note: Logger is a subclass of Category and extends it. Category has been deprecated. Internally, whenever log4j is asked to produce a Category object, it will instead produce a Logger object. In order to preserve backward compatibility, methods that previously accepted category objects still continue to accept category objects.

Creating a Logger Instance

The recommended way to create Logger instance is to request them by name from the LogManager:

Logger log = LogManager.getLogger("some-name");

LoggerConfig

The logging hierarchy is maintained as a relationship between LoggerConfig instances.

LoggerConfig Named Hierarchy

A LoggerConfig is said to be an ancestor of another LoggerConfig if its name followed by a dot is a prefix of the descendant name.

A LoggerConfig is said to be a parent of a child LoggerConfig is there are no ancestors between itself and the descendant LoggerConfig.

Root LoggerConfig

The root LoggerConfig resides at the top of the LoggerConfig hierarchy. It always exists and it is part of every hierarchy. The Logger that is directly linked to the root LoggerConfig can be obtained as follows:

Logger root = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

Log Levels

Category

This class has been deprecated and replaced with the Logger subclass. There is no need for new client code to use or refer to the Category class.

Appender