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 logs from my application from my springboot application. In first log there is actual message and second log it's just a trace log where inside message there is trace info.
First line is okay whenever there is a message. but second line I want to print only the trace log which is inside the message(starts 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, I want to customize the STDOUT pattern in this. In the %message I'm getting the trace info already in JSON and again "net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder" class is converting json into json that's when backslashes are getting inside every subfield of message.
So here requirement is to remove the prefix for the trace messages so that it only prints %message which is JSON.
and also remove JSON encoder so I won't get those backslashes inserted in every subfield.
I tired removing class which gave me error while compiling and also tried giving core encoder which also gave logback configuration error while compiling.
I didn't find a way to remove all the prefix for the log generated by c.c.s.config.sleuth.LoggingSpanReporter class.
Please suggest a way to print only the traces whenever there is a log line from LoggingSpanReporter and remove JSON Encoder class . All I want is in JSON format so Kibana parses into subfields.
<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 issue with the pattern and Kibana was able to parse JSON and map into fields. This appender targets the logs generated by Sleuth.LoggingSpanReporter Class and applies the pattern and prints only the trace inside the %message.