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中文网其他相关文章!