如何配置 LoggingEventCompositeJsonEncoder 的自定义 PatternLayout?
How to configure custom PatternLayout of a LoggingEventCompositeJsonEncoder?
我已经创建了一个自定义 PatternLayout,我在 logback 的 Access 和 File appender 中使用它,我也想将它用于 LoggingEventCompositeJsonEncoder。
是否可以使用我的布局配置编码器,我该如何实现?
文件追加器配置示例:
<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>
我的 logstash 编码器:
<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>
我在哪里可以定义相同的 PatternLayout?
我读过 here
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).
那么我该如何配置自己的呢?
感谢您的帮助。
为了解决这个问题,我不得不使用自定义 valueMasker
进行过滤,并应用与我的布局相同的过滤方法来过滤字段值。
已添加到 logback.xml:
<jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
<valueMasker class="com.me.util.logging.CustomValueMasker"/></jsonGeneratorDecorator>
Java class 类似于:
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;
}
}
我已经创建了一个自定义 PatternLayout,我在 logback 的 Access 和 File appender 中使用它,我也想将它用于 LoggingEventCompositeJsonEncoder。
是否可以使用我的布局配置编码器,我该如何实现?
文件追加器配置示例:
<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>
我的 logstash 编码器:
<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>
我在哪里可以定义相同的 PatternLayout? 我读过 here
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).
那么我该如何配置自己的呢? 感谢您的帮助。
为了解决这个问题,我不得不使用自定义 valueMasker
进行过滤,并应用与我的布局相同的过滤方法来过滤字段值。
已添加到 logback.xml:
<jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
<valueMasker class="com.me.util.logging.CustomValueMasker"/></jsonGeneratorDecorator>
Java class 类似于:
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;
}
}