Overview on Log4j Levels
Most often we deal with logging in our projects to log levels but tend to forget its role and definition of each. This post is for viewers who wish to refresh their knowledge in it. In this post we shall learn about different levels of logging in log4j and their usage.
Log4j has three main components:
- loggers: Responsible for capturing logging information.
- appenders : Responsible for publishing logging information to various preferred destinations.
- layouts: Responsible to format logging information in different styles.
Logger takes care of the logging mechanism and deals with level of logging. Log4j provides six standard levels of logging. There are two more special levels given by log4j. Above all these, log4j allows you to create custom levels.
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.
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
This log4j level gives a warning about an unexpected event to the user. The messages coming out of this level may not halt the progress of the system.
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 and also 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 |