log4j Logging Levels - Log4j

What is log4j Logging Levels?

The org.apache.log4j.Level levels. You can as well define your custom levels by sub-classing the Level class.

Level

Description

ALL

All levels including custom levels.

DEBUG

Designates fine-grained informational events that are most useful to debug an application.

ERROR

Designates error events that might still allow the application to continue running.

FATAL

Designates very severe error events that will presumably lead the application to abort.

INFO

Designates informational messages that highlight the progress of the application at coarse-grained level.

OFF

The highest possible rank and is intended to turn off logging.

TRACE

Designates finer-grained informational events than the DEBUG.

WARN

Designates potentially harmful situations.

How do Levels Works?

A log appeal of level p in a logger with level q is enabled if p >= q. This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

The Following example shows how we can filter all our DEBUG and INFO messages. This program uses of logger method setLevel(Level.X) to set a desired logging level:

This example would print all the messages except Debug and Info:

When you compile and run the LogClass program, it would generate the resulting result –

Setting Levels using Configuration File

log4j offers you configuration file based level setting which sets you free from changing the source code when you want to change the debugging level.

Resulting is an instance configuration file which would perform the same task as we did using the log.setLevel(Level.WARN) method in the above example.


Let us now use our following program –

Now compile and run the above program and you would get following result in /usr/home/log4j/log.out file –


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Log4j Topics