Integration of Spring Logging with log4j – Eclipse IDE Coding

1. Objective

In this Spring Logging Tutorial, you will be seeing the implementation of logging with log4j in Spring Framework. Moreover, we will learn several methods & examples for Spring Logging with log4j. Apache log4j is a Java-based logging utility which was originally written by Ceki. Log4j is one of several Java logging frameworks. It is a part of Apache Logging Services project of Apache foundation.
So, let’s start Spring Logging with log4J.

Integration of Spring Logging with log4j - Eclipse IDE Coding

Integration of Spring Logging with log4j – Eclipse IDE Coding

2. Integration Spring Logging with log4J

Log4j is an easy to use functionality inside Spring Framework applications. It is assumed that you already have log4j installed. If not then download it from https://logging.apache.org/ and unpack the file in any folder. For integrations of Spring Logging with Log4J, we use log4j-x.y.z.jar. You will see a simple example for integrating log4j and Spring framework. Here, you will be making a dynamic form-based web application using Spring Framework. This example is explained using Eclipse IDE.
The following steps are required to make this application:

  1. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
  2. Add the Spring Libraries that are required using the Add External JARs options.
  3. Add log4j-x.y.z.jar to your project using Add External JARs.
  4. Create HelloWorld.java and MainApp.java under the above-made package.
  5. Define config file Bean.xml under src.
  6. Create a log4j config file log4j.properties under src.
  7. Finally, write code for all Java files and Bean config file and run the application as described.

Follow this link to know more about Spring Batch Tutorial

a. Code for HelloWorld.java

package com.example;
public class HelloWorld {
private String message;
public void setMessage(String message)
{
this.message = message;
}
public void getMessage()
{
System.out.println("Your Message : " + message);
}
}

b. Code for MainApp.java

package com.example;
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");
obj.getMessage();
log.info("Exiting the program");
}
}

c. Beans XML File

<?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 = "com.example.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>

Now write log4j.properties which will define a standard rule for log4j to produce log messages.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:\\log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Finally, after creating source and beans config files run your application. It will show the following messages as output if everything is fine.
Your Message: Hello World!
Then go to your C drive you will find your log.out.It will have messages somethings as follow.
<!– initialization log messages –>
Going to create HelloWord Obj
Returning cached instance of singleton bean ‘helloWorld’
Exiting the program
Let’s explore the Advantages and Disadvantages of Spring Framework
Also, you can use Jakarta Commons Logging (JCL) API to generate log in your application. Instead of putting log4j-x.y.z.jar in your CLASSPATH put commons-logging-x.y.z.jar file. For using Spring logging functionality define the org.apache.commons.logging.log object.

d. Methods with Object Message

Use the following methods with object message as a parameter:

  • Fatal()
  • Error()
  • Warn()
  • Info()
  • Debug()
  • Trace()

Use the below replacement code of MainApp.java and run the application as above. You will get the same message as your output.
package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.logging. Log;
import org.apache.commons.logging. LogFactory;
public class MainApp {
static Log log = LogFactory.getLog(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");
obj.getMessage();
log.info("Exiting the program");
}
}

So, this was all about integration of Spring Logging with Log4j. Hope you like our explanation.

3. Conclusion

Hence, in this session, you studied about the integration of Apache’s log4j with Spring Logging. Along with that, you made dynamic Form-based Web Application. This you coded in your Eclipse IDE with the detailed steps mentioned above. You also got an alternate way by using JCL API in your code.
Related Topic- Spring Boot CLI
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.