logback PatternLayoutEncoder and CompsotiteConverter with logstash structured arguments is not logging expected message

82 Views Asked by At

Initially I was working with masking using logback CompositeConverter and it was working as expected. I am currently using PatternLayoutEncoder, and I have attempted to pass logstash encoder structured arguments during logging, but the output was not as expected.


<configuration>
<conversionRule conversionWord="mask" converterClass="com.me.filter.MessageCompositeConverter" />
The appender with pattern referring to the conversion
<appender name="REPORT" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        </encoder>
  <!--below is PatternLayoutEncoder-->
    <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %mask(%m){ssn, email}%n</pattern>          
        </encoder>
</appender>

I do not have more experience with logback or logging framework, and I am learning on the go. My assumption was the logstash Encoder encode will get applied first then paternLayout encoder would do the masking. Calling below

LOGGER.info("my request message" , kv("body", ObjLogged))

Expected to get

{
  "@timestamp": "2023-08-08T18:27:01.099+02:00",
  "@version": "1",
  "message": "my request message"
  "logger_name": "REPORT",
  "thread_name": "main",
  "level": "INFO",
  "level_value": 20000,
  "orderId": "123",
  "body": {
    "ssn":"*********345",
    "email":"******@gmail.com"
  }
}

The actual

my request message

Through debugging I found out that my transform logic for CompsiteConverter is not getting getting the message, I was expecting. The structured arguments are associated with ObjectAppendingMarker object in ILoggingEvent

public class MessageCompositeConverter extends CompositeConverter<ILoggingEvent> 
    protected String transform(ILoggingEvent event, String in) {
        String updatedMessage = in;
        final StringBuilder message = new StringBuilder(in);
        ....
        if (maskableOptions != null) {
            maskingUtils.mask(message, maskableOptions);
            updatedMessage = maskingUtils.getMessageBuilder().toString();
        } 
    
        return updatedMessage;
    }

Now if I removed structured arguments key value pair and log,

//ObjLogged - is json string converted with ObjectMapper
LOGGER.info(ObjLogged)

I get the result

{
"ssn":"*********345", "email":"******@gmail.com"
}

Is it possible to use logstash StructuredArguments by applying the Custome CompositeConverter I created?

0

There are 0 best solutions below