如何自定义或删除 Log4j2 中的默认属性 - JSON 布局

How to customize or remove default attributes in Log4j2 - JSON Layout

Spring Boot 2 应用程序中,我已将 Log4j2 配置为 JsonLayout,如下所示

    ....

    <Appenders>
        <Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
            <JsonLayout complete="false" compact="false">
            </JsonLayout>
        </Console>
    </Appenders> 
    <Logger name="CONSOLE_JSON_APPENDER" level="INFO" additivity="false">
        <AppenderRef ref="ConsoleJSONAppender" />
    </Logger>

    .....

我得到如下输出

    {
            "timeMillis" : 1496306649058,
            "thread" : "main",
            "level" : "INFO",
            "loggerName" : "ConsoleJSONAppender",
            "message" : "Json Message",
            "endOfBatch" : false,
            "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
            "threadId" : 1,
            "threadPriority" : 5
    }

输出很好,但 我不想要 "endofBatch"、"threadPriority" 和其他 等属性,但是它正在日志中显示,如何在基于 JsonLayout 的日志中避免不需要的(默认)属性

如果您只想记录 levelloggerName 而不是像下面那样在您的配置文件中自定义。

...
<PatternLayout>
    <pattern>{"level":"%p","loggerName":"%c"}</pattern>
</PatternLayout>
...

参数在 here 中描述。在 Pattern Layout.

找到 Patterns