spring boot, logback 设置json log

filebeat
logback

add dependency
The minimum required logback version is 1.1.

Download the latest version of Elastic logging:
https://search.maven.org/search?q=g:co.elastic.logging%20AND%20a:logback-ecs-encoder

<dependency>
  <groupId>co.elastic.logging</groupId>
  <artifactId>logback-ecs-encoder</artifactId>
  <version>1.1.0</version>
</dependency>

In src/main/resources/logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <include resource="co/elastic/logging/logback/boot/ecs-file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ECS_JSON_FILE"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

configure the following properties to your application.properties:

spring.application.name=my-application
# for Spring Boot 2.2.x+
logging.file.name=/path/to/my-application.log
# for older Spring Boot versions
# logging.file=/path/to/my-application.log

start log

{"@timestamp":"2021-07-12T06:07:07.897Z", "log.level": "INFO", "message":"Starting DemoApplication using Java 1.8.0_252 on 10CNLPC1KYN1A with PID 14888 (C:\\kaili\\demo\\logback-demo\\target\\classes started by kaili in C:\\kaili\\demo\\logback-demo)", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"com.spring.logback.demo.DemoApplication"}
{"@timestamp":"2021-07-12T06:07:07.901Z", "log.level": "INFO", "message":"No active profile set, falling back to default profiles: default", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"com.spring.logback.demo.DemoApplication"}
{"@timestamp":"2021-07-12T06:07:09.382Z", "log.level": "INFO", "message":"Tomcat initialized with port(s): 8089 (http)", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer"}
{"@timestamp":"2021-07-12T06:07:09.396Z", "log.level": "INFO", "message":"Starting service [Tomcat]", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"org.apache.catalina.core.StandardService"}
{"@timestamp":"2021-07-12T06:07:09.396Z", "log.level": "INFO", "message":"Starting Servlet engine: [Apache Tomcat/9.0.48]", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"org.apache.catalina.core.StandardEngine"}
{"@timestamp":"2021-07-12T06:07:09.588Z", "log.level": "INFO", "message":"Initializing Spring embedded WebApplicationContext", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]"}
{"@timestamp":"2021-07-12T06:07:09.589Z", "log.level": "INFO", "message":"Root WebApplicationContext: initialization completed in 1627 ms", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext"}
{"@timestamp":"2021-07-12T06:07:10.103Z", "log.level": "INFO", "message":"Tomcat started on port(s): 8089 (http) with context path ''", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer"}
{"@timestamp":"2021-07-12T06:07:10.118Z", "log.level": "INFO", "message":"Started DemoApplication in 2.934 seconds (JVM running for 4.13)", "ecs.version": "1.2.0","service.name":"my-application","event.dataset":"my-application.log","process.thread.name":"main","log.logger":"com.spring.logback.demo.DemoApplication"}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值