Logging in Spring Framework using Log4j - Part (2/3) Log4j Architecture and Levels

Welcome to our second part of log4j tutorial series i.e. log4j with spring and spring log4j configuration. In previous blog we discussed what logging is ? and ways of implementing logging in Spring. In this particular blog we will see LOG4J examples in java and how to integrate LOG4J in Spring MVC.

LOG4J configuration with Spring - Part 1 : Logging Introduction
LOG4J configuration with Spring - Part 2 : LOG4J architecture and levels
LOG4J configuration with Spring - part 3 : Log4J configuration with spring

LOG4J is most commonly used framework for logging in Java applications. LOG4J is a open source logging framework from apache software foundation. LOG4J can be used in any Java program to provide easy, fast and customizable logging.

While using LOG4J with spring a programmer can manage and control logging nature and flow from an external configuration file i.e. log.xml or log4j.properties. The external property file can be used to set the log format and the level of logging.

LOG4J Architecture

LOG4J with spring consists of a layered architecture, it has three main components :

1) Logger
2) Appender
3) Layout

1) Logger in LOG4J

Logger is the core of LOG4J framework, it is responsible to capture logging information on the basis of defined log levels.

Log levels in LOG4J

There are 6 normal log levels defined in LOG4J framework.


This level of LOG4J framework provides most detailed information that any other level in the hierarchy. One should use and external log file to store the information from TRACE Level logging.


This level is used to support application developer and is used to help the developer in debugging the application.

INFO Level

In general circumstances INFO Level provides progress and chosen information only. This level should be used to show messages to end user and information should be displayed in console rather than in log file.

WARN Level

This level is used to show information about unexpected conditions arised through the running of application. This should be used to show and store log messages for events that are near error level but not errors exactly. One should show these messages on console.


This level is used to inform serious failure reports or the information that may lead to terminate the application. One should show these messages on console.


This level is used to record events that may lead to permanent failure or complete termination of application. One should show these messages on console.

Other than these, there are two special levels as well

ALL : It turns on all levels.

OFF : It turns off all levels.

Important notes about levels using LOG4J in spring

A ) LOG4J level hierarchy and preferences

LOG4J logger levels have a preference order, whenever a logger code line is found the execution checks for logger property file to decide if the activity needs to be recorded in log or not. A logger will only output messages that are of a level greater than or equal to it and skipped all other entries those are lower than the logger.

B) What if a logger is not assigned a level

If a logger is not assigned a level in this case it will inherit the level of  parent upwards in the hierarchy. As if we have a package com.beingjavaguys.logging and we didn?t assigned a level to it than  it will inherit level from its parent i.e. level of com.beingjavaguys ? and what if even com.beingjavaguys is not having a level than it will inherit a level from com ?and if all upwards parent are exhausted and no one is assigned a level than LOG4J?s root logger is assigned that is ?DEBUG?.

2) Appender in LOG4J

LOG4J can represent the logs in many formats and this is what Appneders does exactly. Appender is responsible for publishing the final log output to destinations. There are a number of appenders some of them are listed below


ConsoleAppender appender = new ConsoleAppender(new PatternLayout());


FileAppender appender = new FileAppender(new PatternLayout(),"filename");


WriterAppender appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename"));

Othet than these appenders listed above there are some other appenders that LOG4J provides : SMTPAppender, SocketAppender, TelnetAppender ?etc.

3) Layout in LOG4J

In LOG4J logging framework, each appender requires a layout which formats the final output to be displayed by appenders. User can control and manage the output formats of log messages by modifying layout settings in log4j.properties file.

There are basically three types of layout, HTML Layout, Pattern Layout and Simple Layout.

In this part of blog series on log4j with spring we discussed basic architecture, levels and configurations of LOG4J, in next and final part of series we will come across the actual configuration and implementation of LOG4J in Spring. Click Here to go next part of the tutorial.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.