Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Spring Boot 2 application I have configured Log4j2 with JsonLayout like below

    ....

    <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>

    .....

and I got output like below

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

Output is fine but I don't want attributes like "endofBatch", "threadPriority" and others but it is getting displayed in logs, how to avoid unwanted (default) attributes in JsonLayout based logs.

like image 424
Ravikumar Avatar asked Sep 19 '25 16:09

Ravikumar


1 Answers

If you want to log only level and loggerName than customize like below in your configuration file.

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

The parameter are described at here. Find Patterns at Pattern Layout.

like image 109
Han Avatar answered Sep 22 '25 07:09

Han