Home > Blockchain >  Having trouble in logging in JSON format while using the logback library
Having trouble in logging in JSON format while using the logback library

Time:09-30

These are the dependencies I specified in pom.xml

        <dependency>
            <groupId>ch.qos.logback.contrib</groupId>
            <artifactId>logback-json-classic</artifactId>
            <version>0.1.5</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback.contrib</groupId>
            <artifactId>logback-jackson</artifactId>
            <version>0.1.5</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>

This is my appender in logback-spring.xml

<appender name="RollingFile-Appender" >
        <layout >
            <file>${LOG_PATH}/test.log</file>
            <jsonFormatter
                >
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
            <rollingPolicy >
                <maxIndex>10</maxIndex>
                <FileNamePattern>${LOG_ARCHIVE}/test.log.%i</FileNamePattern>
            </rollingPolicy>
            <triggeringPolicy >
                <MaxFileSize>250MB</MaxFileSize>
            </triggeringPolicy>
            <encoder>
                <pattern>%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n</pattern>
            </encoder>
        </layout>
    </appender>

The log file is not getting generated even though it was getting generated when the logs were being rendered in a string like format. This is the previous appender in logback-spring.xml. It works fine but generates logs as strings. I want to convert them to json so tried the above specified stuff.

    <appender name="RollingFile-Appender"
        >
        <file>${LOG_PATH}/test.log</file>
        <rollingPolicy >
            <maxIndex>10</maxIndex>
            <FileNamePattern>${LOG_ARCHIVE}/test.log.%i</FileNamePattern>
        </rollingPolicy>
        <triggeringPolicy >
            <MaxFileSize>250MB</MaxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd 'at' HH:mm:ss.SSS z} %-5level %class{36}:%L %M - %msg%xEx%n</pattern>
        </encoder>
    </appender>

CodePudding user response:

Your appender should look like this:

<configuration>
    <appender name="RollingFile-Appender"
              >
        <layout >
            <jsonFormatter
                    >
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
            <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
        </layout>
        <file>${LOG_PATH}/test.log</file>
        <rollingPolicy >
            <maxIndex>10</maxIndex>
            <FileNamePattern>${LOG_ARCHIVE}/test.log.%i</FileNamePattern>
        </rollingPolicy>
        <triggeringPolicy >
            <MaxFileSize>250MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="RollingFile-Appender" />
    </root>
</configuration>

you have nested the appender configuration in the layout

  • Related