springboot输出json格式日志

1、在pom文件中添加依赖

<dependency>
   <groupId>net.logstash.logback</groupId>
   <artifactId>logstash-logback-encoder</artifactId>
   <version>6.2</version>
</dependency>

2、创建logback-spring.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
说明:
    1. 文件的命名和加载顺序有关
       logback.xml早于application.yml加载,logback-spring.xml晚于application.yml加载
       如果logback配置需要使用application.yml中的属性,需要命名为logback-spring.xml
    2. logback使用application.yml中的属性
       使用springProperty才可使用application.yml中的值 可以设置默认值
-->
<configuration debug="false" scan="true" scanPeriod="600000">
    <springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="logs"/>
    <springProperty scope="context" name="appName" source="spring.application.name" defaultValue="ygtcloud-base"/>
    <property name="LOG_HOME" value="${logPath}" />
    <contextName>${HOSTNAME}</contextName>
    <springProperty scope="context" name="appName"
                    source="spring.application.name" />
    <springProperty scope="context" name="ip"
                    source="spring.cloud.client.ipAddress" />
    <property name="osName" value="${os.name}" />
 
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
    <property name="CONSOLE_LOG_PATTERN"
              value="%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %line [%yellow(%X{X-B3-TraceId}),%green(%X{X-B3-SpanId})] [%yellow(%thread)] %green(%logger{30}) %msg%n" />
 
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf-8</charset>
        </encoder>
    </appender>
 
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 正在记录的日志文件的路径及文件名 -->
        <file>${LOG_HOME}/${appName}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_HOME}/${appName}.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>1MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!-- 每个日志文件到100MB的时候开始切分,最多保留30天,但最大到20GB,哪怕没到30天也要删除多余的日志 -->
            <MaxHistory>30</MaxHistory>
            <maxFileSize>100MB</maxFileSize>
            <totalSizeCap>20GB</totalSizeCap>
        </rollingPolicy>
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "level": "%level",
                        "appName": "${appName}",
                        "requestId": "%X{requestId}",
                        "remoteIp": "%X{remoteIp}",
                        "traceId": "%X{X-B3-TraceId:-}",
                        "spanId": "%X{X-B3-SpanId:-}",
                        "parent": "%X{X-B3-ParentSpanId:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "line": "%line",
                        "message": "%message",
                        "stack_trace": "%exception"
                        }
                    </pattern>
                </pattern>
                <stackTrace>
                    <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                        <maxDepthPerThrowable>100</maxDepthPerThrowable>
                        <maxLength>20480</maxLength>
                        <rootCauseFirst>true</rootCauseFirst>
                    </throwableConverter>
                </stackTrace>
            </providers>
        </encoder>
    </appender>
 
    <root level="info">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>






扫描下方二维码,关注公众号:程序进阶之路,实时获取更多优质文章推送。


扫码关注

评论