Log4j log levels – Log4j2 log levels example

In this log4j tutorial, learn about log4j logging levels. The amount and type of information shown in the system and event logs is controlled by the log4j level setting in configuration file. Remember, each message on the log is prefixed by the level of the message.

In Log4j, levels are instance of org.apache.log4j.Level class.

1. Log4j Log Levels

Log4j has following levels of logging.

Log LevelDescription
ALLThis level is used to turn on all levels of logging. Once this is configured and the levels are not considered at all. All appenders will start pouring the log events in log files.
TRACEThis has been recently introduced in version 1.2 and adds more information to debug level logs.
DEBUGYou can use them a lot for debugging the application at development time. Each and every log message will come to log files once this level is set. It basically belongs to developers.
INFOImportant business process has finished and good news is “as expected”. In real time, system administrators will watch the info logs to ensure what’s happening on system right now, and if there is any problem in normal flow.
WARNIt suggest that the application might be continued, but you should take extra caution. The application can tolerate warning messages, but they should always be justified and examined so that they did not prove hidden crackers in application waiting to fire.
ERRORIt shouts at you that something had gone terribly wrong, and you must investigate immediately. It simply means that your application has met really undesired state. For example database unavailability or unexpected formatted input etc.
FATALYou will not feel their presence very much in normal day, but once they appear, it signals very bad news, even the application death.
OFFSimple enough. NO LOGGING !!

In log4j, for root logger – default log level is DEBUG.

2. How to set log levels

2.1. Set log level in log4j.properties

log4j.rootLogger=DEBUG, consoleAppender
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n

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

Check out log4j2.properties configuration example.

2.2. Set log level in log4j.xml

<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />

  <logger name="com.howtodoinjava.web.controller">
	<level value="INFO" />
	<appender-ref ref="console" />
  	<level value="DEBUG" />
    <appender-ref ref="console"></appender>

Check out log4j2.xml configuration example.

3. How log levels work?

In log4j, log levels have order.


If we set 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 logging level to INFO, then application can log messages with scopes - INFO, WARN, ERROR and FATAL.

In this diagram, vertical header shows the Level of the LogEvent, while the horizontal header shows the 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 log level example

Java program to demonstrate usage of log levels.

import org.apache.log4j.*;

public class LogLevelExample 
   private static Logger logger = Logger.getLogger(LogLevelExample.class);
   public static void main(String[] args) {

      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 !!


Log4j Docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “Log4j log levels – Log4j2 log levels example”

  1. This explanation is wrong.

    `If we set log level to ‘X’ then any log request with ‘level = X’ (lesser scopes) will be logged in log files. All requests with lessor order will not be logged.`

  2. “If we set 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.”

  3. Hi Lokesh,

    Good job.
    Just wanted to point out contradiction in log levels priority ordering:
    Following statement is not correct:
    “If we set log level to ‘X’ then any log request with ‘level = X’ (higher priorities) will be logged in log files.


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.