首頁 > Java > Spring boot 日誌記錄-如何刪除追蹤日誌前綴

Spring boot 日誌記錄-如何刪除追蹤日誌前綴

PHPz
發布: 2024-02-09 21:03:08
轉載
1175 人瀏覽過

php小編子墨為您帶來Spring Boot日誌記錄的相關內容。在使用Spring Boot進行應用程式開發時,我們經常會遇到追蹤日誌前綴的問題,這些前綴會影響日誌的可讀性和美觀性。本文將向您介紹如何刪除追蹤日誌前綴,讓您的日誌更加清晰明了。無論您是初學者還是有一定經驗的開發者,本文都將對您有所幫助。接下來,讓我們一起來探索這個問題的解決方案吧!

問題內容

1.message log
{"@timestamp":"2023-12-18t22:36:22.449z","severity":"info","service":"mopservice-jcr-svc","traceid":"afa32548086d2994","spanid":"afa32548086d2994","user-id":"","iv-user":"","customer-number":"","exportable":"","pid":"1","thread":"http-nio-8443-exec-1","class":"o.a.c.m.jmx.instrumentationmanagerimpl","message":"registering mbean org.apache.cxf:bus.id=cxf619002012,type=performance.counter.server,service=\"{http://www.canadapost.ca/ws/payment/methodsofpayment/soap/2017/10}methodsofpaymentporttypeservice\",port=\"methodsofpaymentporttypeport\",operation=\"getallowedmethodsofpayment\": org.apache.cxf.management.counters.responsetimecounter@99c7fd6"}
登入後複製
2.trace log - message field contains trace information without any actual message.
{"@timestamp":"2023-12-18t22:36:21.076z","severity":"info","service":"mopservice-jcr-svc","traceid":"afa32548086d2994","spanid":"-5697962974162517179","user-id":"","iv-user":"","customer-number":"","exportable":"","pid":"1","thread":"http-nio-8443-exec-1","class":"c.c.s.config.sleuth.loggingspanreporter","message":"{\"traceid\":\"afa32548086d2994\",\"parentid\":\"57583fd122326bb7\",\"id\":\"b0ecc4dd35b70345\",\"kind\":\"client\",\"name\":\"micrometer://timer:get-allowed-methods-of-payment-time?action=start\",\"timestamp\":1702938981069686,\"duration\":6630,\"localendpoint\":{\"servicename\":\"micrometer://timer:get-allowed-methods-of-payment-time?action=start\",\"ipv4\":\"10.129.6.120\"},\"tags\":{\"camel.client.endpoint.url\":\"micrometer://timer:get-allowed-methods-of-payment-time?action=start\",\"camel.client.exchange.id\":\"cbe3d7cc933d03a-0000000000000001\",\"camel.client.exchange.pattern\":\"inout\"}}"}
登入後複製

這些是來自我的 springboot 應用程式的應用程式的日誌。在第一個日誌中,有實際的訊息,第二個日誌只是一個追蹤日誌,其中訊息內部有追蹤資訊。

只要有訊息,第一行就可以。但第二行我只想列印訊息內的追蹤日誌(以 { taceid 開頭)。我將在下面發布我的 logback 配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
    <!-- Example for logging into the build folder of your project -->

    <!-- You can override this to have a custom pattern -->
    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %-5level [${springAppName},%X{traceId:-},%X{spanId:-}] %clr([%X{iv-user:-},%X{user-id:-},%X{customer-number:-}]) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

    <!-- Appender to log to console -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
            <immediateFlush>true</immediateFlush>
        </encoder>
    </appender>

    <appender name="ASYNC-CONSOLE" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="CONSOLE"/>
        <!-- do not discard any messages -->
        <discardingThreshold>0</discardingThreshold>
        <!-- size of the blocking queue -->
        <queueSize>500</queueSize>
    </appender>

    <!-- Appender to log to file in a JSON format -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "severity": "%level",
                        "service": "${springAppName:-}",
                        "traceId": "%X{traceId:-}",
                        "spanId": "%X{spanId:-}",
                        "user-id":"%X{user-id}",
                        "iv-user":"%X{iv-user}",
                        "customer-number":"%X{customer-number}",
                        "exportable": "%X{spanExportable:-}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "message": "%message"
                        }
                    </pattern>
                </pattern>
                <stackTrace>
                    <throwableConverter
                            class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                        <maxDepthPerThrowable>10</maxDepthPerThrowable>
                        <maxLength>2048</maxLength>
                        <shortenedClassNameLength>32</shortenedClassNameLength>
                        <exclude>sun\.reflect\..*\.invoke.*</exclude>
                        <exclude>net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>
                        <rootCauseFirst>true</rootCauseFirst>
                    </throwableConverter>
                </stackTrace>
            </providers>
            <immediateFlush>false</immediateFlush>
        </encoder>
    </appender>

    <appender name="ASYNC-STDOUT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="STDOUT"/>
        <!-- do not discard any messages -->
        <discardingThreshold>0</discardingThreshold>
        <!-- size of the blocking queue -->
        <queueSize>500</queueSize>
    </appender>

    <if condition='!isDefined("LOG_LEVEL")'>
        <then>
            <property name="LOG_LEVEL" value="INFO" />
        </then>
    </if>

    <root level="${LOG_LEVEL}">
        <if condition='isDefined("ELK_OUTPUT")'>
            <then>
                <appender-ref ref="ASYNC-STDOUT" />
            </then>
            <else>
                <appender-ref ref="ASYNC-CONSOLE" />
            </else>
        </if>
    </root>
</configuration>
登入後複製

這是我的logback-spring xml,我想在其中自訂stdout模式。在 %message 中,我已經獲取了 json 格式的跟踪信息,並且“net.logstash.logback.encoder.loggingeventcompositejsonencoder”類正在將 json 轉換為 json,此時反斜杠進入消息的每個子字段。

所以這裡的要求是刪除追蹤訊息的前綴,以便它只列印 %message,它是 json。

並且還刪除 json 編碼器,這樣我就不會在每個子欄位中插入這些反斜線。

我厭倦了刪除在編譯時給我帶來錯誤的類,並且還嘗試提供核心編碼器,這在編譯時也給了 logback 配置錯誤。

我沒有找到刪除 c.c.s.config.sleuth.loggingspanreporter 類別所產生的日誌的所有前綴的方法。

請建議一種方法,以便在 loggingspanreporter 有日誌行時僅列印追蹤並刪除 json encoder 類別。我想要的只是 json 格式,以便 kibana 解析為子字段。

解決方法

<appender name="TRACE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%message%n</pattern>
<charset>utf8</charset>
<immediateFlush>true</immediateFlush>
</encoder>
</appender>
<appender name="ASYNC-TRACE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="TRACE"/>
<!-- do not discard any messages -->
<discardingThreshold>0</discardingThreshold>
<!-- size of the blocking queue -->
<queueSize>500</queueSize>
</appender>
<!--Appends Custom pattern for Trace Logs-->
<logger name="com.cpg.springboot.config.sleuth.LoggingSpanReporter" level="INFO" additivity="false">
<appender-ref ref="ASYNC-TRACE"/>
</logger>
登入後複製

這解決了我的模式問題,kibana 能夠解析 json 並映射到字段中。此附加程式的目標是 sleuth.loggingspanreporter 類別產生的日誌 並套用該模式並僅列印 %message 內的追蹤。

以上是Spring boot 日誌記錄-如何刪除追蹤日誌前綴的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板