我希望能够将 logstash logback 编码器生成的一些字段包装在另一个字段中。这可以通过 logback-spring.xml 中的 XML 配置来完成,还是我必须实现一些类,然后在配置中引用它?
我尝试阅读有关实现 Factory 和 Decorator 方法的信息,但它似乎没有让我有任何收获。
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/Users/name/dev/test.log
</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/Users/name/dev/log/test.%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"component":"webserver","datacenter":"ord"}
</customFields>
</encoder>
</appender>
我在记录某些内容时得到的当前 JSON 是:
{
"@timestamp": "2019-07-18T18:12:49.431-07:00",
"@version": "1",
"message": "Application shutdown requested.",
"logger_name": "org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar$SpringApplicationAdmin",
"thread_name": "RMI TCP Connection(2)-127.0.0.1",
"level": "INFO",
"level_value": 20000,
"component": "webserver",
"datacenter": "ord"
}
我想要的是:
{
"@timestamp": "2019-07-18T18:12:49.431-07:00",
"@version": "1",
"component": "webserver",
"datacenter": "ord",
"data": {
"message": "Application shutdown requested.",
"logger_name": "org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar$SpringApplicationAdmin",
"thread_name": "RMI TCP Connection(2)-127.0.0.1",
"level": "INFO",
"level_value": 20000
}
}
如您所见,一组选定的字段被包装在“数据”中,而不是稍后在外部。