Spring uses Log4J to log

Posted by mrausch on Sun, 02 Jan 2022 12:37:20 +0100

Log using Log4J

It is very easy to use the function of Log4J in Spring applications. The following example will take you through simple steps to explain the simple integration between Log4J and Spring.

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

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

stepdescribe
1 Create a project named SpringExample, and create a package com. In the src. Folder of the project tutorialspoint.
2 Use the Add External JARs option to add the required Spring library. See the Spring Hello World Example Chapter for an explanation.
3 Use the Add External JARs option to also add the log4j library log4j-x.y.z.jar to your project.
4 On COM Create Java classes HelloWorld and MainApp under the tutorialspoint package.
5 Create the Bean configuration file beans. In the {src} file xml.
6 Create the log4J configuration file log4J. In the {src} file properties.
7 The last step is to create the contents of all Java files and Bean configuration files, and run the application, as explained below.

This is HelloWorld Contents of Java} 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);
   }
}

Below is the second file, mainapp Contents of Java:

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 information messages we have generated, you can generate debugging and error messages. Now let's take a look at beans Contents of 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.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Here is Log4J Properties, 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 have created the source and bean configuration files, we can run the application. If your application is all right, the following information will be output from the Eclipse console:

Your Message : Hello World!

At the same time, if you check your C: \ driver, you should find that the log file containing various log messages is log Out, 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. JCL can be accessed from http://jakarta.apache.org/commons/logging/ Download. Technically, the only file we need for this package is the commons-logging-x.y.z.jar file. We need to use a method similar to the log4j-x.y.z.jar you used in the above example to put commons-logging-x.y.z.jar in your classpath.

In order to use the logging function, 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)

The following is a description of mainapp. Com using JCL API Java replacement:

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 ensure that the commons-logging-x.y.z.jar file has been introduced into your project before compiling and running the program.

Now keep the remaining configuration and content in the above example unchanged. If you compile and run your application, you will get results similar to those obtained after using the Log4J API.