HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Log4j log levels – Log4j2 log levels example

By Lokesh Gupta | Filed Under: Log4j

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=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
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" />
    </layout>
  </appender>

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

Check out log4j2.xml configuration example.

3. How log levels work?

In log4j, log levels have order.

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

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

Reference:

Log4j Docs

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

4
Leave a Reply

This comment form is under antispam protection
3 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Lixx

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.`

Vote Up0Vote Down  Reply
2 months ago
Lokesh Gupta

From official documentation:

Log Level intLevel
========================
OFF 0
FATAL 100
ERROR 200
WARN 300
INFO 400
DEBUG 500
TRACE 600
ALL Integer.MAX_VALUE

Now read the statement again.

Vote Up0Vote Down  Reply
2 months ago
Jay Deep Khandelwal

“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.”

Vote Up0Vote Down  Reply
11 months ago
Pratap Singh

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.

Vote Up0Vote Down  Reply
1 year ago

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz