Spring MVC Log4j Interation

Spring MVC Log4j Integration

To integrate Log4j with Spring MVC application you will have to include log4j.jar in your class path and attach either log4j.properties or log4j.xml file in classpath.

Note: If you add both log4j.propertites and log4j.xml in class path then log4j.xml takes preference over properties file. Use only one file either log4j.propertites or  log4j.xml. In this example we have included both for demo purpose. 

Steps:

  • Create maven project name: SpringMVCLog4j in your eclipse workspace
  • Final project structure will look as below:

Spring MVC Log4j Integration

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javahonk</groupId>
    <artifactId>SpringMVCLog4j</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringMVCLog4j Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <SpringVersion>4.0.6.RELEASE</SpringVersion>
        <log4j.version>1.2.16</log4j.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${SpringVersion}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${SpringVersion}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${SpringVersion}</version>
        </dependency>
        
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>SpringMVCLog4j</finalName>
    </build>
</project>

  • log4j.properties:
# rootLogger options
log4j.rootLogger= debug,stdout,file
 
# Write message to log file using RollingFileAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:/JavaHonk/SpringMVCLog4j.log
log4j.appender.file.MaxFileSize=2MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Log on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
  • log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC
  "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
    </appender>
    
    <appender name="fileAppend" class="org.apache.log4j.RollingFileAppender">
        <param name="Threshold" value="debug" />
        <param name="File" value="C:/JavaHonk/Log4jXML.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
        </layout>
    </appender>

    <logger name="com.javahonk.controller.SpringController" additivity="false">
        <level value="INFO" />
        <appender-ref ref="console" />
        <appender-ref ref="fileAppend" />
    </logger>
    
    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="fileAppend" />
    </root>

</log4j:configuration>
  • SpringController.java:
package com.javahonk.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SpringController {
    
    private static final Logger logger = Logger.getLogger(SpringController.class);
    
    @RequestMapping(value="/helloWorld.web",method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        logger.info("Log4j info is working");
        logger.warn("Log4j warn is working");       
        logger.debug("Log4j debug is working");
        logger.error("Log4j error is working");
        System.out.println("System out is working");
        
        model.addAttribute("message", "Integration");
        return "helloWorld";

    }
}

Spring MVC Log4j Integration

  • log on console:

Spring MVC Log4j Integration

  • C:/JavaHonk/Log4jXML.log:

Spring MVC Log4j Integration

  • For more information please visit here

download Download Project: SpringMVCLog4j

One thought on “Spring MVC Log4j Integration”

Leave a Reply

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