Exemple de logback Spring Boot SLF4j
Dans ce tutoriel, nous allons vous montrer comment utiliserLogback dans le framework Spring Boot.
Technologies utilisées:
-
Spring Boot 2.1.2.RELEASE
-
Spring 5.1.4.RELEASE
-
Logback 1.2.3
-
Maven 3
-
Java 8
1. Répertoire des projets
2. Maven
Dans Spring Boot, Logback est le cadre de journalisation par défaut, ajoutez simplementspring-boot-starter-web
, il extraira les dépendances delogback
.
pom.xml
4.0.0 spring-boot-slf4j jar Spring Boot SLF4j 1.0 org.springframework.boot spring-boot-starter-parent 2.1.2.RELEASE 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-surefire-plugin 2.22.0
Affichez les dépendances du projet.
$ mvn dependency:tree +- org.springframework.boot:spring-boot-starter-web:jar:2.1.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.1.2.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.2.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
$ mvn dependency:tree org.springframework.boot:spring-boot-slf4j:jar:1.0 [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.1.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.1.2.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.2.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.1:compile [INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.1:compile [INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | | +- org.springframework:spring-core:jar:5.1.4.RELEASE:compile [INFO] | | | \- org.springframework:spring-jcl:jar:5.1.4.RELEASE:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.23:runtime [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.1.2.RELEASE:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.2.RELEASE:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.14:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.14:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.14:compile [INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.14.Final:compile [INFO] | | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.4.0:compile [INFO] | +- org.springframework:spring-web:jar:5.1.4.RELEASE:compile [INFO] | | \- org.springframework:spring-beans:jar:5.1.4.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.1.4.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:5.1.4.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:5.1.4.RELEASE:compile [INFO] | \- org.springframework:spring-expression:jar:5.1.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.1.2.RELEASE:compile [INFO] | +- org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile [INFO] | | +- org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile [INFO] | | | +- org.attoparser:attoparser:jar:2.0.5.RELEASE:compile [INFO] | | | \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile [INFO] | | \- org.slf4j:slf4j-api:jar:1.7.25:compile [INFO] | \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.2.RELEASE:compile [INFO] \- org.springframework.boot:spring-boot-devtools:jar:2.1.2.RELEASE:compile (optional) [INFO] +- org.springframework.boot:spring-boot:jar:2.1.2.RELEASE:compile [INFO] \- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.2.RELEASE:compile
3. application.properties
3.1 Configure the logging in application.properties
application.properties
# logging level logging.level.org.springframework=ERROR logging.level.com.example=DEBUG # output to a file logging.file=app.log # temp folder example #logging.file=${java.io.tmpdir}/app.log logging.pattern.file=%d %p %c{1.} [%t] %m%n logging.pattern.console=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n ## if no active profile, default is 'default' ##spring.profiles.active=prod # root level #logging.level.=INFO
3.2 This is the equivalent in YAML format.
application.yml
logging: level: org.springframework: ERROR com.example: DEBUG pattern: console: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" file: "%d %p %c{1.} [%t] %m%n" file: app.log
4. logback.xml
La configuration de journalisation ci-dessus est suffisante pour le développement. Pour la production, nous avons besoin de plus de fonctionnalités de journalisation comme le roulement de fichiers ou SMTP. Dans Spring Boot, nous permettons toujours de configurer la connexion via le standardlogback.xml
logback.xml
${HOME_LOG} logs/archived/app.%d{yyyy-MM-dd}.%i.log 10MB 20GB 60 %d %p %c{1.} [%t] %m%n
5. Journalisation de Spring Boot par profil
Note
Lire ceci -Profile-specific configuration
Pour tirer parti des fonctionnalités de création de modèles fournies par Spring Boot, nous pouvons créer unlogback-spring.xml
à la racine du chemin de classe.
Dans la configuration ci-dessous:
-
Si aucun profil actif (par défaut), se connecte à la console.
-
Si le profil est
prod
, enregistre dans un fichier de roulement.
logback-spring.xml
app.log logs/archived/app.%d{yyyy-MM-dd}.%i.log 10MB 20GB 60 %d %p %c{1.} [%t] %m%n
Définissez une propriétéspring.profiles.active
pour définir le profil actif actuel.
application.properties
## if no active profile, default is 'default' spring.profiles.active=prod
6. Bonjour Logback
6.1 A simple Spring MVC web application, logs something.
HelloController.java
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.Arrays; import java.util.List; @Controller public class HelloController { private static final Logger logger = LoggerFactory.getLogger(HelloController.class); @GetMapping("/") public String hello(Model model) { Listdata = Arrays.asList(1, 2, 3, 4, 5); logger.debug("Hello from Logback {}", data); model.addAttribute("num", data); return "index"; // index.html } }
6.2 Template
src/resources/templates/index.html
Spring Boot SLF4j Logback Spring Boot + SLF4j Logback example
6.3 CSS
src/resources/static/css/main.css
h1{ color:#0000FF; }
7. Demo
7.1 Start Spring Boot
StartWebApplication.java
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StartWebApplication { public static void main(String[] args) { SpringApplication.run(StartWebApplication.class, args); } }
Terminal
15:15:11.588 [http-nio-8080-exec-1] DEBUG com.example.HelloController - Hello from Logback [1, 2, 3, 4, 5]
Télécharger le code source
$ git clone https://github.com/example/spring-boot.git
$ cd logging-slf4j-logback
$ mvn spring-boot:run
Accédez àlocalhost:8080
Consultez la console pour les informations de journalisation.