上文中,咱們實現了將LogBack的日誌信息實時的推送到日誌服務器的功能。但實時進行推送,必然會增長日誌服務器的壓力。本文將闡述另外一種定時推送的方法,以減輕日誌服務器的壓力。java
目標:每10秒鐘統一推送這期間產生的日誌
https://github.com/mengyunzhi/springBootSampleCode/tree/master/log-back
開發環境: java1.8 + spring-boot:2.1.2git
爲了與官方文檔更加統一,在此,咱們以下定義application.properties
github
# URL logback.loggly.endpointUrl=http://localhost:8081/log
<?xml version="1.0" encoding="UTF-8"?> <!--啓用debug模式後,將在`spring-boot 大LOG`上方打印中logBack的配置信息--> <configuration debug="true" scan="false" scanPeriod="30 seconds"> <!--引入application配置信息--> <springProperty scope="context" name="logback.loggly.endpointUrl" source="logback.loggly.endpointUrl" defaultValue="localhost"/> <!--包含配置文件 org/springframework/boot/logging/logback/defaults.xml--> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <!--定義變量LOG_FILE,值爲${LO...}--> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> <!--包含配置文件,該配置文件中,定義了 控制檯日誌是按什麼規則,什麼形式輸出的--> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <!--包含配置文件,該配置文件中,定義了 文件日誌是按什麼規則,什麼形式輸出的--> <include resource="org/springframework/boot/logging/logback/file-appender.xml"/> <!--引入第三方appender, 起名爲http--> <appender name="HTTP" class="ch.qos.logback.ext.loggly.LogglyAppender"> <!--請求的地址--> <endpointUrl>${logback.loggly.endpointUrl}</endpointUrl> <!--定義過濾器--> <filter class="com.mengyunzhi.sample.logback.Filter"/> <!--定義輸出格式JSON--> <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> </appender> <!--引用第三方appender, 起名爲batchHttp--> <appender name="batchHttp" class="ch.qos.logback.ext.loggly.LogglyBatchAppender"> <endpointUrl>${logback.loggly.endpointUrl}</endpointUrl> <flushIntervalInSeconds>10</flushIntervalInSeconds> <!--定義輸出格式JSON--> <layout class="ch.qos.logback.contrib.json.classic.JsonLayout"> <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter"> <prettyPrint>true</prettyPrint> </jsonFormatter> </layout> </appender> <!--定義日誌等級--> <root level="INFO"> <!--啓用第一個appender爲CONSOLE, 該名稱定義於org/springframework/boot/logging/logback/console-appender.xml中--> <appender-ref ref="CONSOLE"/> <!--啓用第二個appender爲FILE, 該名稱定義於org/springframework/boot/logging/logback/file-appender.xml中--> <appender-ref ref="FILE"/> <!--啓用第三個appender爲HTTP--> <!--<appender-ref ref="HTTP"/>--> <!--啓用第4個appender爲batchHttp--> <appender-ref ref="batchHttp"/> </root> </configuration>
主要變動信息以下:spring
logUrl
變動爲:logback.loggly.endpointUrl
logback.loggly.endpointUrl
appender
-> batchHttp
啓動service
子模塊,啓動本項目。打開瀏覽器,並訪問:http://localhost:8080/send
json
發送方日誌:segmentfault
2019-03-25 15:29:02.093 INFO 28934 --- [nio-8080-exec-6] c.m.sample.logback.LogBackApplication : info 2019-03-25 15:29:02.093 WARN 28934 --- [nio-8080-exec-6] c.m.sample.logback.LogBackApplication : warn 2019-03-25 15:29:02.093 ERROR 28934 --- [nio-8080-exec-6] c.m.sample.logback.LogBackApplication : error 2019-03-25 15:29:11.564 INFO 28934 --- [nio-8080-exec-8] c.m.sample.logback.LogBackApplication : info 2019-03-25 15:29:11.565 WARN 28934 --- [nio-8080-exec-8] c.m.sample.logback.LogBackApplication : warn 2019-03-25 15:29:11.565 ERROR 28934 --- [nio-8080-exec-8] c.m.sample.logback.LogBackApplication : error
擋收方日誌瀏覽器
2019-03-25 15:29:08.286 INFO 28428 --- [nio-8081-exec-9] c.m.s.l.service.ServiceApplication : { "timestamp" : "1553498942093", "level" : "INFO", "thread" : "http-nio-8080-exec-6", "logger" : "com.mengyunzhi.sample.logback.LogBackApplication", "message" : "info", "context" : "default"}{ "timestamp" : "1553498942093", "level" : "WARN", "thread" : "http-nio-8080-exec-6", "logger" : "com.mengyunzhi.sample.logback.LogBackApplication", "message" : "warn", "context" : "default"}{ "timestamp" : "1553498942093", "level" : "ERROR", "thread" : "http-nio-8080-exec-6", "logger" : "com.mengyunzhi.sample.logback.LogBackApplication", "message" : "error", "context" : "default"} 2019-03-25 15:29:18.289 INFO 28428 --- [nio-8081-exec-1] c.m.s.l.service.ServiceApplication : { "timestamp" : "1553498951564", "level" : "INFO", "thread" : "http-nio-8080-exec-8", "logger" : "com.mengyunzhi.sample.logback.LogBackApplication", "message" : "info", "context" : "default"}{ "timestamp" : "1553498951565", "level" : "WARN", "thread" : "http-nio-8080-exec-8", "logger" : "com.mengyunzhi.sample.logback.LogBackApplication", "message" : "warn", "context" : "default"}{ "timestamp" : "1553498951565", "level" : "ERROR", "thread" : "http-nio-8080-exec-8", "logger" : "com.mengyunzhi.sample.logback.LogBackApplication", "message" : "error", "context" : "default"}
咱們發現:
發送方於29分02秒時,生成了3條日誌;接收方於29分08秒接收到了1條日誌,該日誌包括了上面3條日誌信息。
發送方於29分11秒時,生成了3條日誌;接收方於29分18秒(距離上次接收的間隔爲10秒)接收到了第2條日誌,該日誌包括了29分11秒時生成的3條日誌信息。服務器
對於小白級別的咱們而言,咱們想到的,別人其實早就實現了。因此大多數的咱們,最終比接是DEBUG的能力、看文檔的能力、對軟件工程理解的能力、對業務的掌控能力。而編碼能力,則是基本功,沒有它不行,只有它也不行。app
參考文檔:https://github.com/qos-ch/logback-extensions/wiki/Logglyspring-boot