02 - log4j Architechture and Components

2.1 log4j architecture overview

Log4j has primarily three main components and they are Logger, Layout and Appenders. Together these three components, allow developers to write a log messages in specified format and to specified destination.

                          Log4j Architecure and Components

 

  1. Logger Object - Primary responsibility of logger object is to store all the information that need to be logged.
  2. Layout Object - Layout Object is responsible to format / style the logging information to desired format
  3. Appender Object - Appender Object sends the styled logging information to the specified destination like file system, console, database, JMS etc. We can send the logs to destinations asynchronously.

 

2.2 Logger Object:

Primary responsibility of logger object is to store all the information that need to be logged.

2.2.1 Logger Hierarchy

Log4j logger object follows the hierarchy name rules and the name of logger object is case-sensitive.

Any logger object will be considered as an ancestor of another logger object if its name is there in child logger object with “.”. For example “com” is the ancestor logger of logger object with name “com.test.abc”

If there is no ancestor between logger then it will be considered as a parent logger. For example “com” is the parent logger of “com.test” and ancestor of “com.test.abc”.

Logger follows its properties from its parent or ancestor logger which means say if parent logger has configured the logging level as INFO then the logging level of its child will also be INFO until they override it.

We can get the instance of logger by calling the “getLogger(String name)” static method of “org.apache.log4j.Logger” class (refer below example )

Logger x = Logger.getLogger("com.test");

Note:Calling getLogger() method for same logger(means passing same logger name) will always return the same logger instance. Below two loggers (x and y) will refer to same instance because both are obtained with same name.

Logger x = Logger.getLogger("com.test");

Logger y = Logger.getLogger("com.test");

2.2.2 Root Logger

Root Logger is the parent of all logger object(similar to Object class is parent of all classes in Java). We can get the instance of Root logger by calling the getRootLogger()static method of “org.apache.log4j.Logger” class

2.3  Appenders

Appender Object sends the styled logging information to the specified destination like file system, console, database, JMS etc. We can send the logs to destinations asynchronously.

As we mentioned, that several appenders are supported by log4j and even we can write our own also, at the same time we can send the logs to multiple appenders (which means we can configure multiple appenders for one logger object).

2.3.1 Appender Additivity

By default, appenders are inherited from the parent logger’s appender and we can add more appender at a child logger. In other words, if a parent logger (“com”)  is configured to use Console Appender, then child logger (“com.test”) gets Console appender by default and it can configure add more appender if needed.

2.3.2 Additivity Flag

We can turn off the additivity so that parent logger’s appender does not get inherited at a child logger by simply set the appender–additivity flag to false.

Since Root logger is the parent of all loggers, additivity is not applicable on Root logger.

Let’s discuss some scenarios for appender additivity.

  1. If Root logger is configured to use Appender A1 then it all logs statements will be directed to A1 destination.
  2. Logger with name “com” configured to use Appender A2, then all logs statements will be directed to A1 and A2 destination ( appenders of both Root and “com”)
  3. Logger with name “xyz” has not configured any  Appender, then all logs statements will be directed to A1( appenders ofRoot)
  4. Logger with name “com.test” configured to use Appender A3, then all logs statements will be directed to A1, A2  and A3 destination ( appenders of Root, “com” and “com.test”)
  5. Logger with name “abc” configured to use Appender A4 and set the additivity as false, then all logs statements will be directed to A4 destination only ( appenders of “abc”)
  6. Logger with name “abc.zyz” has not configured any appeender and  set the additivity as true, then all logs statements will be directed to A4 destination only ( appenders of “abc”)

2.4 Layout

Layout Object is responsible to format / style the logging information to desired format. By now we understand that we can configure the different destinations but we can also configure the layout of our log statement. PatternLayout is one of the most commonly used layouts which can be configured with pattern in printf style.

Like us on Facebook