Search code examples
javaspringlogginglogbackslf4j

How can I change a key name in the JSON output using Logback?


Here's my logback configuration.

<springProfile name="prod">
    <root level="info">
        <appender name="naki" class="ch.qos.logback.core.ConsoleAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>${logging_level}</level> <!-- setup via ENV variable log level -->
            </filter>
            <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
                    <timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
                    <timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
                    <appendLineSeparator>true</appendLineSeparator> <!-- don't forget line break -->

                    <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                        <prettyPrint>false
                        </prettyPrint> <!-- in prod never pretty print, line breaks are considered as separate log entry -->
                    </jsonFormatter>
                </layout>
            </encoder>
        </appender>
    </root>
</springProfile>

<springProfile name="dev">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <root level="info">
        <appender-ref ref="CONSOLE"/>
    </root>
</springProfile>

Here's the output that I get:

{"timestamp":"2020-01-13T13:38:38.001Z","level":"INFO","thread":"main","logger":"com.nakipower.identity.api.config.Application","message":"Started Application in 8.605 seconds (JVM running for 9.439)","context":"default"}

How could I rename JSON level key name to severity? So, I get "severity":"INFO" instead of "level":"INFO". Can I actually rename JSON key names while configuring logging with logback?


Solution

  • You can extend ch.qos.logback.contrib.jackson.JacksonJsonFormatter and override toJsonString(Map m) method. There you can replace the level entry in the given map with a new one with the key severity.

    public class CustomJsonFormatter extends JacksonJsonFormatter{
    
        @Override
        public String toJsonString(Map map) throws IOException {
            map.put("severity", map.get("level"));
            map.remove("level");
            return super.toJsonString(map);
        }
    }
    

    In your xml config, in the jsonFormatter tag point to the new formatter you just created

    <appender name="naki" class="ch.qos.logback.core.ConsoleAppender">
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>INFO</level> <!-- setup via ENV variable log level -->
                </filter>
                <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                    <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
                        <timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
                        <timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
                        <appendLineSeparator>true</appendLineSeparator> <!-- don't forget line break -->
    
                        <jsonFormatter class="mypackage.CustomJsonFormatter"> <!-- Here point to your custom json formatter -->
                            <prettyPrint>false
                            </prettyPrint> <!-- in prod never pretty print, line breaks are considered as separate log entry -->
                        </jsonFormatter>
                    </layout>
                </encoder>
            </appender>