Reading:  

What is Log4J. An absolute beginners tutorial.


Different Logging Levels in Log4J 2

The below are the logging level provided by the org.apache.log4j level class.

Level

Description

All

All levels along with custom level.

DEBUG

Fine grained level application information for debugging

INFO

Coarse grained level application information highlighting the progress

ERROR

Error event of application allowing to continue running

FATAL

Severe error  event leading the application to abort

OFF

Turn off logging

TRACE          

Fine grained level application information more detailed than DEBUG

WARN

Harmful situations.

 

How Level Works?

The log request level r in a logger with level s, is enabled if r >= s. This is primary rule of log4j. For standard levels the rules are ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; 
 
public class LogClassExample
{
    static final Logger logger_c = LogManager.getLogger(Log4jConfigurationExample.class.getName());
    public static void main(String[] args)
    {
			logger_c.setLevel(Level.WARN);
			logger_c.debug("Message Debug!!");
			logger_c.info( " Message Info!!");
			logger_c.trace("Message Trace!!");
			logger_c.warn( " Message Warn!!");
			logger_c.error("Message Error!!");
			logger_c.fatal( " Message Fatal!!");

    }
}

 

In the above the example since the set level is set to desired logging level anything lesser than that will not be displayed. Here Debug and INFO message will be filtered out.

Message Warn!!

Message Error!!

Message Fatal!!

Setting Levels using Configuration File:

Using configuration file level setting can be changed based on level setting. In below example we are setting it to the LEVEL.WARN

 
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
  <appenders>
    <File name="MyFile" fileName=" Applicationdemo.log ">
        <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>            
  </appenders>
  <loggers>    
    <root level="debug">
      <appender-ref ref="MyFile " level="warn"/>
    </root>   
  </loggers>
</configuration>

Example Program

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
 
public class Log4jConfigurationExample
{
    static final Logger logger_c = LogManager.getLogger(Log4jConfigurationExample.class.getName());
    public static void main(String[] args)
    {
        logger_c.debug("Message Debug!!");
        logger_c.info( " Message Info!!");
        logger_c.trace("Message Trace!!");
        logger_c.warn( " Message Warn!!");
        logger_c.error("Message Error!!");
        logger_c.fatal( " Message Fatal!!");
    }
}

Compile and run the program to get output in Applicationdemo.log in project folder.

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

Description

This tutorial is aimed at learners who want to get an understaind on what Log4J is and what it is used for. This tutorial have 10 part to it as shown below

  1. Overview    
  2. How to Install Log4J
  3. Understanding Architecture
  4. Getting started with Log4J Configuration
  5. Sample Program
  6. Logging Methods
  7. Understanding different Logging Levels
  8. Log4J log formatting
  9. How to log in files
  10. How to log in database using Log4J

We hope that this tutorial will help you out with Log4J. Please note that this tutorial is for absolute starters and does not go into much depth. Let us know how we can improve by sending  your feedback.



Prerequisites

A good understanding of Java programming language is required.

Audience

Absolute beginners

Author: Subject Coach
Added on: 7th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!