Springs - SPLessons

Spring Log4J

Chapter 21

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Log4J

Spring Log4J

shape Description

While writing java programs to print the line developer uses System.out.println then application is effective. But if developer wants to add more printing lines, it takes more time, to remove this strategy Log4J will consists of control flags.Control flags contains OFF and ALL, by using this developer can add lines to the application depends on the requirement of clients. Elements of Spring Log4J as follows:

Example for Log4J with explanation as follows

shape Features

Features of log4J are as follows:

Log4J with Spring

shape Description

Spring Log4J is simple to implement it’s functionality inside the Spring framework. Before proceeding with Log4J, developer should have Log4J installation in the system. The installation of Log4J as shown below.

Log4J installation link

shape Example

Example of Log4J with eclipse is as shown below. Before proceeding with Log4J application developer needs to have library files and property files to set up in IDE.


package splessons;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;

   public void getMessage(){
      System.out.println("Your Message : " + message);

Here just created the variable message and performed SET and GET methods.

package splessons;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.log4j.Logger;

public class MainApp {

   static Logger log = Logger.getLogger(MainApp.class.getName());

   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      log.info("Going to create HelloWord Obj");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");


      log.info("Exiting the program");

The Application Context is spring’s more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="Splessons.HelloWorld">
       <property name="message" value="Hello World!"/>


Here message has been passed that is Hello World.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
# Set the name of the file

# Set the immediate flush to true (default)

# Set the threshold to debug mode

# Set the append to false, overwrite

# Define the layout for file appender

After creation of source and bean configuration files, run the program.This will print the output in Eclipse console as follows.

Your Message : Hello World!

Now check C:\\ drive, log.out file is found with various log messages as follows:

;!-- initialization log messages --

Going to create HelloWord Obj
Returning cached instance of singleton bean 'helloWorld'
Exiting the program


shape Key Points

  • Spring Log4J can be used to write less code in JAVA applications.
  • Spring Log4J is a Java SW library that has practical experience in logging.