Spring - Logging with Log4J



This is a very easy-to-use Log4J functionality inside Spring applications. The following example will take you through simple steps to explain the simple integration between Log4J and Spring. To start with, let us have a working Eclipse IDE in place and take the following steps to develop a Dynamic Form based Web Application using Spring Web Framework −

Example - Logging with Log4j

Steps Description
1 Create a Maven project with a name spring, groupid com.tutorialspoint, artifactid spring and create a package com.tutorialspoint under the src folder in the created project.
2 Update the pom.xml as explained in the Spring - Environment Setup chapter. Add log4j as dependency as shown below.
3 Create Java classes TextEditor,SpellChecker and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src/main/resources folder. Add log4j2.properties as shown below.
5 The final step is to create the content of all the Java files and Bean Configuration file. Finally, run the application as explained below.

HelloWorld.java

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); } }

MainApp.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"); } }

You can generate debug and error message in a similar way as we have generated info messages. Now let us see the content of Beans.xml file

Beans.xml

<?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>

log4j2.properties

Following is the content of log4j2.properties which defines the standard rules required for Log4J to produce log messages.

appender.0.type = File
appender.0.name = MAIN
appender.0.fileName = logs/main.log
appender.0.layout.type = PatternLayout
appender.0.layout.pattern = %p - %m%n

rootLogger.level = INFO
rootLogger.appenderRef.0.ref = MAIN

Output

Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message in Eclipse console −

Your Message : Hello World!

If you check logs folder under your current directory , then you should find your log file main.log with various log messages, like something as follows −

INFO - Going to create HelloWord Obj
INFO - Exiting the program

Jakarta Commons Logging (JCL) API

Alternatively you can use Jakarta Commons Logging (JCL) API to generate a log in your Spring application. JCL can be incorporated as following dependency −

<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.3.5</version> </dependency>

The only file we technically need out of this package is the commons-logging-x.y.z.jar file, which needs to be placed in your classpath in a similar way as you had put log4j-x.y.z.jar in the above example.

To use the logging functionality you need a org.apache.commons.logging.Log object and then you can call one of the following methods as per your requirment −

  • fatal(Object message)
  • error(Object message)
  • warn(Object message)
  • info(Object message)
  • debug(Object message)
  • trace(Object message)

MainApp.java

Following is the replacement of MainApp.java, which makes use of 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 have to make sure that you have included commons-logging-x.y.z.jar file in your project, before compiling and running the program.

Now keeping the rest of the configuration and content unchanged in the above example, if you compile and run your application, you will get a similar result as what you got using Log4J API.

Advertisements