SpringブートSLF4Jロギングの例

Spring Boot SLF4j Logbackの例

spring boot slf4j logback

このチュートリアルでは、Spring BootフレームワークでLogbackを使用する方法を示します。

使用される技術:

  • Spring Boot 2.1.2.RELEASE

  • Spring 5.1.4.RELEASE

  • ログバック1.2.3

  • メーベン3

  • Java 8

1. プロジェクトディレクトリ

project directory

2. メーベン

Spring Bootでは、Logbackがデフォルトのロギングフレームワークであり、spring-boot-starter-webを追加するだけで、logbackの依存関係が取得されます。

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
            
        
    

プロジェクトの依存関係を表示します。

$ 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

上記のロギング構成は、開発に十分です。 本番環境では、ファイルローリングやSMTPなどのロギング機能がさらに必要です。 Spring Bootでも、標準のlogback.xmlを介してログバックを構成できます。

project directory

logback.xml



    

    
        ${HOME_LOG}

        
            logs/archived/app.%d{yyyy-MM-dd}.%i.log
            
            10MB
            
            20GB
            
            60
        

        
            %d %p %c{1.} [%t] %m%n
        
    

    
        
    

    
        
    

5. プロファイルによるスプリングブートロギング

Note
これを読む–Profile-specific configuration

Spring Bootが提供するテンプレート機能を利用するために、クラスパスのルートにlogback-spring.xmlを作成できます。

project directory

以下の構成で:

  • アクティブなプロファイルがない場合(デフォルト)、コンソールにログを記録します。

  • プロファイルがprodの場合、ローリングファイルにログを記録します。

logback-spring.xml


    

    
        

        
            
        
    

    

        
            app.log

            
                logs/archived/app.%d{yyyy-MM-dd}.%i.log
                
                10MB
                
                20GB
                
                60
            

            
                %d %p %c{1.} [%t] %m%n
            
        

        
        
        
            
        
    

spring.profiles.activeプロパティを定義して、現在アクティブなプロファイルを設定します。

application.properties

## if no active profile, default is 'default'
spring.profiles.active=prod

6. こんにちはログバック

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);
    }

}

demo

ターミナル

15:15:11.588 [http-nio-8080-exec-1] DEBUG com.example.HelloController - Hello from Logback [1, 2, 3, 4, 5]

ソースコードをダウンロード

$ git clone https://github.com/example/spring-boot.git
$ cd logging-slf4j-logback
$ mvn spring-boot:run

アクセスlocalhost:8080
コンソールでログ情報を確認します。