简体   繁体   中英

Having trouble in logging in JSON format while using the logback library

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" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
            <file>${LOG_PATH}/test.log</file>
            <jsonFormatter
                class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <maxIndex>10</maxIndex>
                <FileNamePattern>${LOG_ARCHIVE}/test.log.%i</FileNamePattern>
            </rollingPolicy>
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <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"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/test.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <maxIndex>10</maxIndex>
            <FileNamePattern>${LOG_ARCHIVE}/test.log.%i</FileNamePattern>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <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>

Your appender should look like this:

<configuration>
    <appender name="RollingFile-Appender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
            <jsonFormatter
                    class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
            <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
        </layout>
        <file>${LOG_PATH}/test.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <maxIndex>10</maxIndex>
            <FileNamePattern>${LOG_ARCHIVE}/test.log.%i</FileNamePattern>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>250MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

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

you have nested the appender configuration in the layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM