Example Spring MVC logback

1. Project structure

 

2. File 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.codewr.common</groupId>

    <artifactId>spring-mvc-logback</artifactId>

    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>SpringMVC Logback Example</name>

 

    <properties>

        <jdk.version>1.7</jdk.version>

        <spring.version>4.1.6.RELEASE</spring.version>

        <logback.version>1.1.3</logback.version>

        <jcl.slf4j.version>1.7.12</jcl.slf4j.version>

        <jstl.version>1.2</jstl.version>

        <servletapi.version>3.1.0</servletapi.version>

    </properties>

 

    <dependencies>

 

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>${spring.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>commons-logging</groupId>

                    <artifactId>commons-logging</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

 

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>${spring.version}</version>

        </dependency>

 

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>jcl-over-slf4j</artifactId>

            <version>${jcl.slf4j.version}</version>

        </dependency>

 

        <dependency>

            <groupId>ch.qos.logback</groupId>

            <artifactId>logback-classic</artifactId>

            <version>${logback.version}</version>

        </dependency>

 

        <!-- jstl -->

        <dependency>

            <groupId>jstl</groupId>

            <artifactId>jstl</artifactId>

            <version>${jstl.version}</version>

        </dependency>

 

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>javax.servlet-api</artifactId>

            <version>${servletapi.version}</version>

            <scope>provided</scope>

        </dependency>

 

    </dependencies>

 

    <build>

 

        <plugins>

 

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.3</version>

                <configuration>

                    <source>${jdk.version}</source>

                    <target>${jdk.version}</target>

                </configuration>

            </plugin>

 

            <!-- embedded Jetty server, for testing -->

            <plugin>

                <groupId>org.eclipse.jetty</groupId>

                <artifactId>jetty-maven-plugin</artifactId>

                <version>9.2.11.v20150529</version>

                <configuration>

                    <scanIntervalSeconds>10</scanIntervalSeconds>

                    <webApp>

                        <contextPath>/spring-mvc-logback</contextPath>

                    </webApp>

                </configuration>

            </plugin>

 

            <!-- configure Eclipse workspace -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-eclipse-plugin</artifactId>

                <version>2.9</version>

                <configuration>

                    <downloadSources>true</downloadSources>

                    <downloadJavadocs>true</downloadJavadocs>

                    <wtpversion>2.0</wtpversion>

                    <wtpContextName>spring-mvc-logback</wtpContextName>

                </configuration>

            </plugin>

 

        </plugins>

    </build>

</project>

 

3. Class WelcomeController.java

 

package com.codewr.common.controller;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

/**

 *

 * @author codewr

 */

@Controller

public class WelcomeController {

 

    private static final Logger logger = LoggerFactory.getLogger(WelcomeController.class);

 

    @RequestMapping(value = "/", method = RequestMethod.GET)

    public String welcome(Model model) {

 

        logger.debug("welcome() is executed, value {}", "codewr");

 

        logger.error("This is Error message", new Exception("Testing"));

 

        model.addAttribute("msg", "Hello Spring MVC + Logback");

        return "welcome";

 

    }

 

}

 

4. File logback.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

 

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <layout class="ch.qos.logback.classic.PatternLayout">

            <Pattern>

                %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n

            </Pattern>

        </layout>

    </appender>

 

    <logger name="org.springframework" level="debug" additivity="false">

        <appender-ref ref="STDOUT" />

    </logger>

      

    <logger name="com.codewr.helloworld" level="debug" additivity="false">

        <appender-ref ref="STDOUT" />

    </logger>

        

    <root level="error">

        <appender-ref ref="STDOUT" />

    </root>

 

</configuration>

 

5. File welcome.jsp

 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

    <body>

        <h1>Spring MVC + Logback example</h1>

 

        <h2>${msg}</h2>

 

    </body>

</html>

 

6. File hello-dispatcher-servlet.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="

        http://www.springframework.org/schema/beans    

        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 

    <context:component-scan base-package="com.codewr" />

 

    <mvc:annotation-driven />

 

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">

            <value>/WEB-INF/pages/</value>

        </property>

        <property name="suffix">

            <value>.jsp</value>

        </property>

    </bean>

 

 

</beans>

 

7. File web.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="

        http://www.springframework.org/schema/beans    

        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 

    <context:component-scan base-package="com.codewr" />

 

    <mvc:annotation-driven />

 

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">

            <value>/WEB-INF/pages/</value>

        </property>

        <property name="suffix">

            <value>.jsp</value>

        </property>

    </bean>

 

 

</beans>

 

8. Run web:

http://localhost:8080/spring-mvc-logback/

 

9. Link full code:

https://github.com/sieurobo196/Spring-MVC/tree/master/SpringMVC-Logback-Example