How to configure custom PatternLayout of a LoggingEventCompositeJsonEncoder?

i have created a custom PatternLayout i'm using in Access and File appenders of logback, and I would like to use it as well for a LoggingEventCompositeJsonEncoder.

Is it possible to configure the encoder with my layout, and how can i achieve this ?

Exemple of the file appender configuration :

<appender name="FILE"> <encoder> <layout> <pattern>${LOG_PATTERN}</pattern> </layout> </encoder> <file>${LOG_FILE}</file> <rollingPolicy> <!-- rotate every day --> <fileNamePattern>${logback.rollingPolicy.file.fileNamePattern}</fileNamePattern> <!-- Days of history --> <maxHistory>${logback.maxHistory.retention:-7}</maxHistory> </rollingPolicy> </appender> 

My logstash encoder :

<encoder> <providers> <timestamp>%d{ISO8601}</timestamp> <message/> <loggerName/> <context/> <mdc/> <pattern> <pattern> { "appVersion": "${build.version}", "appName": "${build.artifact}", "resourceType": "${logback.application.resource.type}", "resourceID": "${logback.application.resource.id}", "level": "%level", "hostname": "${logback.server.host}", "indexType": "${logback.logstash.index.type}" } </pattern> </layout> </pattern> <stackTrace/> </providers> </encoder> 

Where can i define the same PatternLayout here ? I've read here that

Every value in the template is treated as a pattern for logback's standard PatternLayout so it can be a combination of literal strings (for some constants) and various conversion specifiers (like %d for date).

So how i can configure my own ? Thanks for your help.

2

1 Answer

Well to solve this, i had to use a custom valueMasker to do the filtering, and apply the same filtering method than my layout to filter the field value.

Added to logback.xml :

<jsonGeneratorDecorator> <valueMasker/></jsonGeneratorDecorator> 

Java class is something like :

package com.me.util.logging; import com.fasterxml.jackson.core.JsonStreamContext; import net.logstash.logback.mask.ValueMasker; public class CustomValueMasker implements ValueMasker { @Override public Object mask(JsonStreamContext jsonStreamContext, Object o) { if (o instanceof CharSequence) { return doTransform((String) o); } return o; } /** * Do the message filtering * * @param msg the message of the event log * * @return the filtered message */ private String doTransform(String msg) { // Do whatever filtering needed here return msg; } } 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like