0

在我的 Spring 后端:标准日志字段名称是:“消息”“级别”,并且 logback.xml 文件效果很好:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 seconds">
    <property resource="application.properties"/>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] <%-5level> [Backend] <%thread> &msg%n
            </Pattern>
        </encoder>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="stdout"/>
    </root>
</configuration>

问题: 如何使用LogstashSocketAppender将标准日志字段名称从“消息”更改为“日志消息”?

我已经尝试添加此附加程序:

<appender name="stash" class="net.logstash.logback.appender.LogstashSocketAppender">
    <host>localhost</host>
    <port>6002</port>
    <customFields>
        {
        "timestamp":"%d{yyyy-MM-dd HH:mm:ss.SSS}",
        "component": "Backend",
        "log-message": "%msg",
        "log-level": "%-5level"
        }</customFields>
</appender>

但我的输出看起来像这样:

{
"message":"correct log message here"
"log-message":"%msg" <--- BAD VALUE
4

1 回答 1

0

我已经通过使用模式做到了这一点:

<appender name="stash" class="net.logstash.logback.appender.LogstashSocketAppender">
    <host>${logstash.host}</host>
    <port>${logstash.port}</port>
    <provider class="net.logstash.logback.composite.loggingevent.LoggingEventPatternJsonProvider">
        <pattern>
            {
            "log-message": "%msg",
            "component": "Backend",
            "timestamp": "%d{yyyy-MM-dd HH:mm:ss.SSS}",
            "log-level": "%-5level",
            "thread-id": "%thread"
            }
        </pattern>
    </provider>
</appender>

编辑:当然,你必须grok > match在你的logstash中创建过滤器来监听这个模式。

于 2018-09-10T12:16:43.610 回答