Exemple Spring MVC + Logback SLF4j

Dans ce didacticiel, nous allons vous montrer comment configurer slf4j etlogback dans une application Web Spring MVC.
Technologies utilisées:
-
Spring 4.1.6.RELEASE
-
Logback 1.1.3
-
Maven 3 ou Gradle 2.0
-
Tomcat 7
-
Eclipse 4.4
Note
Par défaut, Spring utilise l'API Jakarta Commons Logging (JCL), lisezthis.
Pour configurer le cadre de déconnexion, vous devez:
-
Exclure
commons-loggingdespring-core -
Reliez la journalisation de Spring de JCL à SLF4j, via
jcl-over-slf4j -
Inclure la déconnexion comme dépendance
-
Créez un
logback.xmldans le dossiersrc/main/resources -
Done
1. Outils de construction
1.1 For Maven
pom.xml
1.7 4.1.6.RELEASE 1.1.3 1.7.12 org.springframework spring-core ${spring.version} commons-logging commons-logging org.springframework spring-webmvc ${spring.version} org.slf4j jcl-over-slf4j ${jcl.slf4j.version} ch.qos.logback logback-classic ${logback.version}
1.2 For Gradle
build.gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
repositories {
mavenCentral()
}
//1. exclude commons-logging
configurations.all {
exclude group: "commons-logging", module: "commons-logging"
}
dependencies {
//2. bridge logging from JCL to SLF4j
compile 'org.slf4j:jcl-over-slf4j:1.7.12'
//3. Logback
compile 'ch.qos.logback:logback-classic:1.1.3'
compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
}
2. Répertoire des projets
Créez unlogback.xml dans le dossiersrc/main/resources

3. logback.xml
Celogback.xml enverra tous les journaux à la console.
logback.xml
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
Pour les autres ajouts (sortie de journal), comme les journaux dans un fichier, veuillez visiter celog.xml examples, ou celogback appender guide
4. Exemple de déconnexion
WelcomeController.java
package com.example.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;
@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 {}", "example");
logger.error("This is Error message", new Exception("Testing"));
model.addAttribute("msg", "Hello Spring MVC + Logback");
return "welcome";
}
}
5. Demo
Téléchargez lesource code et exécutez-le avec Maven ou Gradle.
5.1 Maven
mvn jetty:run
5.2 Gradle
gradle jettyRun
URL d'accès:http://localhost:8080/spring-mvc-logback
Console
...
2015-06-19 21:53:33 DEBUG o.s.web.servlet.DispatcherServlet - Initializing servlet 'hello-dispatcher'
2015-06-19 21:53:33 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-19 21:53:33 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-19 21:53:33 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-19 21:53:33 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
2015-06-19 21:53:33 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-19 21:53:33 DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources
[servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
Jun 19, 2015 9:53:33 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'hello-dispatcher'
20
...
2015-06-19 21:53:45 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'welcomeController'
2015-06-19 21:53:45 DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/spring-mvc-logback/] is: -1
2015-06-19 21:53:45 ERROR c.m.c.controller.WelcomeController - This is Error message
java.lang.Exception: Testing
at com.example.common.controller.WelcomeController.welcome(WelcomeController.java:21) [WelcomeController.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_65]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_65]
...
La journalisation des applications Spring et Web sera envoyée à la console.
Télécharger le code source
Téléchargez-le -spring-mvc-logback-example.zip (6 Ko)