Monthly Archives: July 2014
What is Log4j?
Log4j is a logging Java Framework, which offers a more efficient and modular alternative to System.out.println. It is an Apache Software Foundation Project, which in turn is part of bigger initiative called Apache Logging. The current version is now Log4j 2.0, which has superseded Log4j 1.x.
Log4j controls the logging behavior through the editing of a configuration file at the runtime and without modifying the application binary. One of its distinctive features is the notion of inheritance of loggers. In a nutshell, by employing a ‘log hierarchy’ it is possible to control which log statements are outputted according to the notion of ‘logging level’.
There are 6 levels of logging in Log4j:
The ability of selecting the logging level helps reduce the volume of logged output and make life easier when inspecting logs.
The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote Log4j server, a remote Unix Syslog daemon, etc.
What do I need to know first and foremost?
There are 3 main concepts underlying Log4j, as follows :
Loggers: Loggers are basic, developer customized, org.apache.log4j.Logger objects, which provide control mechanism over disabling or enabling certain type of log statements when logged against them. Each class in your application can have an individual logger or a common logger. Log4j provides a root logger where all loggers (including custom ones) will inherit from. This also means that it is not strictly necessary to have an individual logger for each class as you can always use the root logger by calling
Logger.getRootLogger(). Nevertheless, it is not recommended to use the root logger.
As mentioned already, we can set the level of each logger, according to the set of logging levels. The root logger always has a default level assigned to it, which is
Appenders: Appenders define the properties of a Logger, meaning that they set the target or the destination of a specific logger. For example, the Default root level Logger has a default Console Appender associated with it, thus it prints to the Console by default.
Layouts: Layouts define the style and content of the output log. Some built-in layouts are provided with
log4j, although it is also possible create your own layouts, if required. For example, a layout can specify the date and time or include information about the line number from which the log originates.
Relevant links and resources: