Log4j: a brief introduction

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.

Why Log4j? 

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:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

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
  • APPENDERS
  • LAYOUT

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 DEBUG.

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:

http://logging.apache.org/

http://logging.apache.org/log4j/1.2/index.html

http://logging.apache.org/log4j/2.x/

https://supportweb.cs.bham.ac.uk/docs/tutorials/docsystem/build/tutorials/log4j/log4j.html

http://salmanawan.com/hello-world-%E2%80%93-log4j-tutorial/

Advertisements

Posted on July 19, 2014, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: