Maven項目配置Logback輸出JSON格式日誌

最近,項目提出需求,日誌須要固定輸出爲JSON格式,以便後端Flink程序解析.java

項目背景

項目爲簡單的Maven項目,日誌由Filebeat採集,所以不須要配置輸出至Logstash.
下面爲pom.xml文件中配置的依賴,此處使用logstash-logback-encoder完成日誌格式轉換操做.程序員

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.28</version>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>6.1</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>1.2.3</version>
        </dependency>

Logback配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>probe</contextName>

   <!-- 配置文件存儲地址  -->
    <property name="LOG_PATH" value="./logs"/>

    <!-- 讀取應用程序配置文件中內容,獲取下文所須要的region屬性 --->
    <property resource="application.properties"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers class="net.logstash.logback.composite.loggingevent.LoggingEventJsonProviders">
                <pattern>
                    <pattern>
                        {
                        "date":"%d{yyyy-MM-dd HH:mm:ss.SSS}",
                        "level":"%level",
                        <!-- system屬性由程序動態肯定,經過Slf4j提供的MDC進行具體設置 -->
                        "system":"%X{system} ",
                        <!-- 讀取配置文件中的屬性,並設置至日誌中  -->
                        "region":"${application.region}",
                        "filepath":"%class:%line",
                        "msg":"%msg"
                        }
                    </pattern>
                </pattern>
            </providers>
            <charset>UTF-8</charset>
        </encoder>
        <append>true</append>

        <!--  配置日誌文件滾動存儲策略 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/probe.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <MaxHistory>10</MaxHistory>
            <maxFileSize>10MB</maxFileSize>
            <totalSizeCap>300MB</totalSizeCap>
        </rollingPolicy>
    </appender>

    <root level="FILE">
        <appender-ref ref="console"/>
    </root>
</configuration>

上述即爲logback.xml文件中的配置內容,須要注意日誌中的region從配置文件application.properties中獲取,而system屬性則同msg字段同樣由程序動態設置.shell

MDC設置system屬性

/**
     * 衡量單次http請求的時延
     *
     * @param url 請求地址
     */
    private static void measureHttpTimeDelay(String url, Platform platform) {
        // MDC中設置system屬性
        MDC.put("system", platform.toString());
        OkHttpClient client = new OkHttpClient();
        client.newBuilder().connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder().url(url).build();
        Instant before = Instant.now();
        try (Response response = client.newCall(request).execute()) {
            Instant after = Instant.now();
            if (response.isSuccessful()) {
                long duration = Duration.between(before, after).toMillis();
                log.info("請求成功!" + response.message() + "時延 = " + duration + "ms");
            } else {
                log.info("請求失敗!" + response.message());
            }
        } catch (IOException e) {
            log.error("get http response failed, url: {}, ex: {}", url, e.getMessage());
        }
        // MDC中清除system屬性設置
        MDC.remove("system");
    }

上述程序,模擬一個簡單的http請求,並記錄相應日誌.
須要注意,MDCThreadLocal實現,所以在多線程環境下使用須要注意.後端

最終日誌

最終日誌以下圖所示:api

{"date":"2019-09-05 21:16:44.643","level":"INFO","system":"ALI_YUN ","region":"HUABEI","filepath":"com.test.Application:38","msg":"請求成功!OK時延 = 439ms"}
{"date":"2019-09-05 21:16:45.326","level":"INFO","system":"HUAWEI_YUN ","region":"HUABEI","filepath":"com.test.Application:38","msg":"請求成功!OK時延 = 165ms"}
{"date":"2019-09-05 21:16:46.513","level":"INFO","system":"ALI_YUN ","region":"HUABEI","filepath":"com.test.Application:38","msg":"請求成功!OK時延 = 485ms"}
{"date":"2019-09-05 21:16:56.130","level":"INFO","system":"HUAWEI_YUN ","region":"HUABEI","filepath":"com.test.Application:38","msg":"請求成功!OK時延 = 419ms"}

PS:
若是您以爲個人文章對您有幫助,請關注個人微信公衆號,謝謝!
程序員打怪之路微信

相關文章
相關標籤/搜索