Spring Boot SLF4j Logback Beispiel

In diesem Tutorial zeigen wir Ihnen, wie SieLogback im Spring Boot Framework verwenden.
Verwendete Technologien:
-
Spring Boot 2.1.2.RELEASE
-
Spring 5.1.4.RELEASE
-
Logback 1.2.3
-
Maven 3
-
Java 8
1. Projektverzeichnis

2. Maven
In Spring Boot ist Logback das Standard-Protokollierungsframework. Fügen Sie einfachspring-boot-starter-web hinzu, und es werden dielogback-Abhängigkeiten abgerufen.
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
Zeigen Sie die Projektabhängigkeiten an.
$ 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
Die obige Protokollierungskonfiguration ist für die Entwicklung ausreichend. Für die Produktion benötigen wir mehr Protokollierungsfunktionen wie File-Rolling oder SMTP. In Spring Boot können wir die Rückmeldung weiterhin über die Standardlogback.xml konfigurieren

logback.xml
${HOME_LOG} logs/archived/app.%d{yyyy-MM-dd}.%i.log 10MB 20GB 60 %d %p %c{1.} [%t] %m%n
5. Spring Boot-Protokollierung nach Profil
Note
Lesen Sie dies -Profile-specific configuration
Um die von Spring Boot bereitgestellten Vorlagenfunktionen nutzen zu können, können wir im Stammverzeichnis des Klassenpfads einlogback-spring.xml erstellen.

In der folgenden Konfiguration:
-
Wenn kein aktives Profil vorhanden ist (Standardeinstellung), werden Protokolle an der Konsole erstellt.
-
Wenn das Profil
prodist, wird eine fortlaufende Datei protokolliert.
logback-spring.xml
app.log logs/archived/app.%d{yyyy-MM-dd}.%i.log 10MB 20GB 60 %d %p %c{1.} [%t] %m%n
Definieren Sie die Eigenschaftspring.profiles.active, um das aktuell aktive Profil festzulegen.
application.properties
## if no active profile, default is 'default' spring.profiles.active=prod
6. Hallo 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) {
List data = 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]
Quellcode herunterladen
$ git clone https://github.com/example/spring-boot.git
$ cd logging-slf4j-logback
$ mvn spring-boot:run
Zugriff auflocalhost:8080
Überprüfen Sie die Konsole auf Protokollinformationen.