Slf 4 j+logback JSON形式ログ出力を実現

6901 ワード

に頼る
<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.8</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.28</version>

</dependency>

<dependency>

<groupId>net.logstash.logback</groupId>

<artifactId>logstash-logback-encoder</artifactId>

<version>6.1</version>

</dependency>

<dependency>

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

<artifactId>logback-classic</artifactId>

<version>1.2.3</version>

</dependency>

<dependency>

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

<artifactId>logback-access</artifactId>

<version>1.2.3</version>

</dependency>

logback.xml

            
                
                    
                        {
                            "date":"%date{\"yyyy-MM-dd HH:mm:ss.SSS\",UTC}",
                            "level":"%level",
                            "msg":"%msg"
                        }
                    
                
            
            UTF-8
        

出力:
{"date":"2019-09-17 08:29:20.879","level":"INFO","msg":"{\"tags\":{\"namespace\":\"service.MQTT.push\",,\"metric.correct\":true}}"}
{"date":"2019-09-17 08:30:20.784","level":"INFO","msg":"{\"tags\":{\"namespace\":\"service.MQTT.command\",\"metric.correct\":true}}"}
{"date":"2019-09-17 08:30:20.854","level":"INFO","msg":"{\"tags\":{\"namespace\":\"service.MQTT.push\",\"metric.correct\":true}}"}