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" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.me.util.logging.PatternLayoutFiltered">
<pattern>${LOG_PATTERN}</pattern>
</layout>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rotate every day -->
<fileNamePattern>${logback.rollingPolicy.file.fileNamePattern}</fileNamePattern>
<!-- Days of history -->
<maxHistory>${logback.maxHistory.retention:-7}</maxHistory>
</rollingPolicy>
</appender>
My logstash encoder :
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<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.
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 class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
<valueMasker class="com.me.util.logging.CustomValueMasker"/></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;
}
}