Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring logs using Log4J


May 14, 2021 Spring


Table of contents


Log4J is used to log

Using Log4J's functionality in Spring applications is very easy. The following example takes you through simple steps to explain the simple integration between Log4J and Spring.

Suppose you already have Log4J installed on your machine, and if you don't already have Log4J, you can download it from http://logging.apache.org/ and extract compressed files only in any folder. In our project, we will only use log4j-x.y.z .jar.

Next, let's get the Eclipse IDE to work in the right place and follow these steps to develop a dynamic web application-based form using the Spring Web framework:

Steps Describe
1 Create a project named SpringExample, and create a package com.tutorialspoint in the src folder that created the project.
2 Use the Add External JARs option to add the Spring library you want, as explained in the Spring Hello World Example section.
3 Using the Add External JARs option, add log4j library log4j-x.y.z to your project .jar.
4 Create Java-class HelloWorld and MainApp under the com.tutorialspoint package.
5 Create a bean profile in the src file .xml.
6 Create a log4J profile log4j.properties in the src file.
7 The final step is to create the contents of all Java files and bean profiles and run the application as explained below.

This is the contents .java HelloWorld file:

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

Here's what the second file, MainApp.java content:

package com.tutorialspoint;
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");
   }
}

Using a method similar to the one we have generated for information messages, you can generate debug and error messages. Now let's look at .xml contents of the Beans 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.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Here's what log4j.properties is about, which defines the standard rules required to generate log information using Log4J:

# 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

Once you've finished creating the profile for the source and bean, we're ready to run the application. If your application is all right, the following information will be output in the Eclipse console:

Your Message : Hello World!

And if you check your C: D river, then you should find log file log.out with various log messages, some of which are as follows:

<!-- initialization log messages -->

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

Jakarta Commons Logging (JCL) API

Alternatively, you can use the Jakarta Commons Logging (JCL) API to generate logs in your Spring application. J CL can be downloaded http://jakarta.apache.org/commons/logging/ the website. The only file that we technically need for this package is the commons-logging-x.y.z.jar file, which needs to be placed in your class path using a similar method to the log4j-x.y.z.jar in.jar the example above.

In order to use the log feature, you need an org.apache.commons.logging.Log object, and then you can call any of the following methods according to your needs:

  • fatal(Object message)

  • error(Object message)

  • warn(Object message)

  • info(Object message)

  • debug(Object message)

  • trace(Object message)

Here's a replacement for MainApp .java the JCL API:

package com.tutorialspoint;
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");
   }
}

You should make sure that commons-logging-x.y.z files have been introduced into your project before compiling and .jar program.

Now leave the configuration and content unchanged in the example above, and if you compile and run your application, you'll get results similar to those you get with the Log4J API.