Home  >  Article  >  Spring boot logging-how to remove trace log prefix

Spring boot logging-how to remove trace log prefix

PHPz
PHPzforward
2024-02-09 21:03:081143browse

php editor Zimo brings you related content about Spring Boot logging. When using Spring Boot for application development, we often encounter the problem of tracking log prefixes, which affect the readability and aesthetics of logs. This article will show you how to remove the trace log prefix to make your logs clearer. Whether you are a beginner or an experienced developer, this article will help you. Next, let’s explore the solution to this problem!

Question content

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\"}}"}

These are the application's logs from my springboot application. In the first log, there are actual messages, the second log is just a trace log with trace information inside the messages.

As long as there is a message, the first line is fine. But on the second line I just want to print the trace log inside the message (starting with { taceid ). I'll post my logback configuration below

<?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>

This is my logback-spring xml in which I want to customize the stdout pattern. In %message I have fetched the trace information in json format and the "net.logstash.logback.encoder.loggingeventcompositejsonencoder" class is converting the json to json with backslashes going into each subfield of the message.

So the requirement here is to remove the prefix of the trace message so that it only prints %message which is json.

And also remove the json encoder so that I don't insert these backslashes in every subfield.

I got tired of removing the classes that gave me errors at compile time, and also tried providing the core encoder, which also gave logback configuration errors at compile time.

I didn't find a way to remove all prefixes for logs generated by the c.c.s.config.sleuth.loggingspanreporter class.

Please suggest a way to only print the trace when the loggingspanreporter has log lines and remove the json encoder class. All I want is the json format for kibana to parse into subfields.

Solution

<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>

This solved my schema issue, kibana was able to parse the json and map it into fields. This appender targets logs generated by the sleuth.loggingspanreporter class And apply the pattern and print only traces within %message.

The above is the detailed content of Spring boot logging-how to remove trace log prefix. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete