A quick overview on Log4j Levels

Share

log 4j


M
ost often we deal with logging in our projects and come across to different log levels but tend to forget the role and definition of each.  In this post I will refresh your memories by describing different levels of logging in log4j and their usage.

Log4j has three main components: loggersappenders and layouts. These three types of components work together to enable developers to log messages according to message type and level. Logger takes care of the logging mechanism and deals with level of logging

 

Let’s look at the log levels and messages in log 4j in decreasing order of their priority:

TRACE Level

This log4j level gives more detailed information than any other level in the hierarchy. This level is introduced from version 1.2.12 in log4j. You should show all such information in the log files only.

DEBUG Level

This log4j level helps developer to debug application and is generally useful for providing support to the application developers. You should show all such information in the log files only.

INFO Level

This log4j level gives the progress and chosen state information and is generally useful for end user. You should show all such information on the console.

WARN Level

Use of deprecated APIs, poor use of API, ‘almost’ errors, other runtime situations that are undesirable or unexpected, but not necessarily “wrong”.  You should show all such information on the console.

ERROR Level

This log4j level gives information about a serious error which needs to be addressed and may result in unstable state. You should show all such errors on the console.

FATAL Level

Such errors result in premature termination and you don’t often get this error. You should show all such errors on the console.

Two special log4j levels

 ALL Level

This log4j level is used to turn on all levels of logging.

OFF Level

This would turn off logging.

You can always have your own custom made level too by extending org.apache.log4j.Level class, if you wish to do so.

When a logger is created, generally you assign a level. The logger outputs all those messages equal to that level andalso all greater levels than it. So the order for the log4j levels are:

Log4J Levels

TRACE Level

DEBUG Level

INFO Level

WARN Level

ERROR Level

FATAL Level

TRACE Level

Y

Y

Y

Y

Y

Y

DEBUG Level

N

Y

Y

Y

Y

Y

INFO Level

N

N

Y

Y

Y

Y

WARN Level

N

N

N

Y

Y

Y

ERROR Level

N

N

N

N

Y

Y

FATAL Level

N

N

N

N

N

Y

ALL Level

Y

Y

Y

Y

Y

Y

OFF Level

N

N

N

N

N

N

Log4j Level Inheritance Rule

There can be instances when you don’t assign a level to the logger. In such cases log4j handles it seamlessly based on the following level inheritance rule:
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.

That means, if you have a package as com.foo.bar and you didn’t allocate a level, then it will inherit the level from com.foo package. Still in that package also if the level is not available, then it will inherit from the log4j root level. The log4j’s root logger is instantiated and available always. Log4j’s root logger by default has DEBUG level.