Log4j Logging Levels

In this Log4j2 tutorial, learn about log4j logging levels. The logging levels are used to control the amount and type of information added to the log statements.

Many analysis tools, like Splunk, can be used to prepare reports based on filters on logging levels.

1. Logging Levels

In Log4j, logging levels are instances of org.apache.log4j.Level class. Log4j has the following levels of logging.

Log LevelDescriptionInteger Value
ALLALL is used to turn on all levels of logging. Once this is configured, other levels are not considered at all, including custom levels.Integer.MAX_VALUE
TRACETRACE was introduced in version 1.2 and adds even more information to debug level logs. At this level, the amount of logs generated can be overwhelming so be careful.600
DEBUGDEBUG is used to log the fine-grained informational events that are most useful to debug an application.
This level is turned off in the production environment, until it needs to be turned on to debug critical production issues that are not reproducible in the development environment.
500
INFOINFO is the most used level in production and logs informational messages that highlight the progress of the application at coarse-grained level.
In real-time, system administrators watch the info logs to ensure what’s happening on the system right now, and if there is any problem everything is alright in the system.
400
WARNWARN suggests that the application might be continued, but we should take extra caution. Generally, the applications can tolerate warning messages, but they should always be justified and examined.300
ERRORERROR shouts at us that something had gone terribly wrong, and we must investigate immediately. It simply means that the application has met really undesired state. For example database unavailability, or unexpected formatted input etc.200
FATALFATAL signals very bad news the ERROR, even the application crash.100
OFFSimple enough. NO LOGGING !!0

Note that by default Log4j assigns the root logger to Level.ERROR.

2. How to Set Log Levels

2.1. Setting Log Level in log4j2.properties

The given example sets DEBUG logging for root logger, except the package ‘com.howtodoinjava.web.controller‘ where it logs INFO logs.

rootLogger.level = debug

appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#Log info messages for package 'com.howtodoinjava.web.controller'
logger.com.howtodoinjava.web.controller=INFO, consoleLogger

Read More: Log4j2.properties Configuration Example.

2.2. Setting Log Level in log4j2.xml

<Configuration status="warn">
    <Appenders>
    	<!-- Console appender configuration -->
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </Console>
    </Appenders>
    <Loggers>
    	<!-- Root logger referring to console appender -->
        <Root level="DEBUG" additivity="false">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>

Read More: Log4j2.xml Configuration Example.

3. How Log Levels Work?

In log4j, log levels are ordered according to the integer values assigned to them as shown in the first table.

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

If we set the log level to ‘X‘ then any log request with ‘level <= X‘ (lesser scopes) will be logged in log files. All requests with higher-order will not be logged.

For example, If we set the logging level to INFO, then the application will log messages with scopes – INFO, WARN, ERROR and FATAL. All DEBUG and TRACE logs will not be added to the log file.

In this diagram, the vertical header shows the Level of the LogEvent, while the horizontal header shows the Log Level associated with the appropriate logging configuration. The intersection identifies whether the LogEvent would be allowed to pass for further processing (Yes) or discarded (No).

Log4j Log Levels
Log4j Log Level Hierarchy

4. Log4j Logging Level Example

Java program to demonstrate usage of log levels. The example sets the log level to INFO, and then logs multiple messages at different levels.

As per out discussion, the program will print all log messages except DEBUG and TRACE.

import org.apache.log4j.*;

public class LogLevelExample
{
   private static Logger logger = Logger.getLogger(LogLevelExample.class);

   public static void main(String[] args) {
      logger.setLevel(Level.INFO);

      logger.trace("Trace Message!");
      logger.debug("Debug Message!");
      logger.info("Info Message!");
      logger.warn("Warn Message!");
      logger.error("Error Message!");
      logger.fatal("Fatal Message!");
   }
}

Program output.

Info Message!
Warn Message!
Error Message!
Fatal Message!

Happy Learning !!

Leave a Reply

5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.