Logback:在消息字段中嵌套标记

时间:2017-11-20 14:24:51

标签: java json logging logback slf4j

我正在使用logback和logstash-logback-encoder来允许JSON日志记录,并且要求在消息字段中包含Marker内容。

带标记的日志语句:

log.error(append("errorCode", errorInfo.getErrorCode())
        .and(append("errorMessage", errorInfo.getErrorMessage())),
        "bla",
        ex);

输出:

{
    "@timestamp":"2017-11-20T13:34:49.147+00:00",
    "severity":"ERROR",
    "threadID":"main",
    "logger":"com.mycompany.MyClass",
    "message": {
        "errorCode":"XYZ-0001",
        "errorMessage":"Some Exception occured",
        "stack_trace":"java.lang.RuntimeException: Some Text\r\n\ <rest of stacktrace>"
    }
}

这是我的appender配置:

<appender name="STDOUT_JSON" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
        <providers>
            <timestamp>
                <timeZone>UTC</timeZone>
            </timestamp>
            <pattern>
                <pattern>
                    {
                    "severity": "%level",
                    "threadID": "%thread",
                    "logger": "%logger{40}"
                    }
                </pattern>
            </pattern>
            <nestedField>
                <fieldName>message</fieldName>
                <providers>
                    <logstashMarkers />
                    <stackTrace />
                </providers>
            </nestedField>
        </providers>
    </encoder>
</appender>

我现在面临的问题是没有标记的日志语句不再填充消息字段:

log.info("some info message");

产生一个空消息字段:

{"@timestamp":"2017-11-20T13:45:12.146+00:00","severity":"INFO","threadID":"main","logger":"s.s.a.AuthorizationFacadeRepository","message":{}}

如果我在模式或nestedField中包含"message": "%message",我会在JSON的第一级获得两个嵌套的消息元素或两个消息元素。

那么如何以正确的方式将标记作为嵌套对象包含在消息字段中呢?

0 个答案:

没有答案