• notice
  • Congratulations on the launch of the Sought Tech site

logback custom json log output

foreword

Let’s talk about the usage scenario of the landlord first. Some method calls of the program are recorded in the file in json format, and provided to the big data for data analysis. Of course, this requirement is very simple to implement, just output the content to the file uniformly through the aop interception aspect. The following is an example of logging data to a log file in json format through the logback log system and the json log dependency provided by logstash.

dependent jars

logstash-logback-encoder:  https://github.com/logstash/logstash-logback-encoder

maven coordinates

<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>5.2</version>
</dependency>

Configure Appender Nodes

<appender name="jsonLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logging.path}/customerBuriedPoint.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logging.path}/customerBuriedPoint.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator">
                <escape>
                    <targetCharacterCode>10</targetCharacterCode>
                    <escapeSequence>\u2028</escapeSequence>
                </escape>
            </jsonFactoryDecorator>
            <providers>
                <pattern>
                    <pattern>
                        {
                        "timestamp":"%date{ISO8601}",
                        "userId":"%mdc{userId}",
                        "requestIp":"%mdc{requestIp}",
                        "event":"%mdc{event}"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

appender configuration instructions:

Encoder: All other configurations can be configured as logback should be configured. The key point is the LoggingEventCompositeJsonEncoder provided by logstash, a json format encoder.

jsonFactoryDecorator: Solve the problem of Chinese transcoding, if this is not added, Chinese will be encoded into ASCII code output

providers: json format provider, define what you want in json for any field. The types in logevent can be directly defined for output here, such as timestamp, message, thread_name, etc. The values of other custom fields can be set through MDC Come in, the format is %mdc{xx}, where xx is the value set in your log context MDC, such as MDC.put("requestIp",requestIp);

Configure the logger node


<logger name="buriedPoint" level="info" additivity="false">
        <appender-ref ref="jsonLog"/>
    </logger>

logger configuration instructions:


Here, the logger node whose name is buriedPoint is defined, then in the log system, only the log defined as buriedPoint will be output, for example:

Logger logger = LoggerFactory.getLogger("buriedPoint");

The final effect is shown in the following figure:


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+