SpringBoot使用Graylog日誌收集
- 2019 年 12 月 9 日
- 筆記
本文介紹SpringBoot如何使用Graylog日誌收集。
1.Graylog介紹
Graylog是一個生產級別的日誌收集系統,集成Mongo和Elasticsearch進行日誌收集。其中Mongo用於存儲Graylog的元數據資訊和配置資訊,ElasticSearch用於存儲數據。
架構圖如下:
生產環境配置圖如下:
2.安裝Graylog
在官方文檔上推薦了很多種安裝的方式,這裡以docker-compose的方式為例,進行安裝Graylog,mongo,elasticsearch。
docker-compose.yml內容如下(這裡是在官網的基礎上改了一下):
version: '2'services: # MongoDB: https://hub.docker.com/_/mongo/ mongodb: image: mongo:3 # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/docker.html elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.6.1 environment: - http.host=0.0.0.0 - transport.host=localhost - network.host=0.0.0.0 - "ES_JAVA_OPTS=-Xms256m -Xmx256m" ulimits: memlock: soft: -1 hard: -1 mem_limit: 512m # Graylog: https://hub.docker.com/r/graylog/graylog/ graylog: image: graylog/graylog:3.0 environment: # CHANGE ME (must be at least 16 characters)! - GRAYLOG_PASSWORD_SECRET=somepasswordpepper # Password: admin - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 - GRAYLOG_HTTP_EXTERNAL_URI=http://106.13.35.42:9000/ links: - mongodb:mongo - elasticsearch depends_on: - mongodb - elasticsearch ports: # Graylog web interface and REST API - 9000:9000 # Syslog TCP - 1514:1514 # Syslog UDP - 1514:1514/udp # GELF TCP - 12201:12201 # GELF UDP - 12201:12201/udp
其中106.13.35.42是我的外網ip,本地服務使用127.0.0.1即可。
其他方式可以查看官方文檔,https://docs.graylog.org/en/3.0/pages/installation.html
3.配置Graylog
在瀏覽器訪問http://ip:9000,如圖:
這裡默認用戶名密碼都是admin,進入後如圖所示。
選擇System按鈕中的input,錄入一個輸入源,如圖
這裡以GELF UDP為例,在圖中位置選擇GELF UDP,選擇完成後點擊Launch new input,如圖
在Node處選擇自己安裝的,剩下的就根據需要填寫即可,如圖
保存完成後如圖,到這裡就已經配置完成了。
4.SpringBoot日誌輸出到Graylog
這裡分別舉例Logback日誌和Log4j2日誌。
4.1 Logback日誌
這裡使用的logback-gelf向Graylog輸出日誌,在github上有對logback-gelf的詳細使用介紹,這裡只是簡單舉例。Github地址:https://github.com/osiegmar/logback-gelf。
新建項目,加入logback-gelf依賴,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> <artifactId>springboot2_graylog</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot2_graylog</name> <description>springboot2_graylog</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>de.siegmar</groupId> <artifactId>logback-gelf</artifactId> <version>2.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
加入logback日誌配置,新建logback-spring.xml,內容如下:
<configuration> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender"> <graylogHost>106.13.35.42</graylogHost> <graylogPort>12201</graylogPort> </appender> <!-- 控制台輸出日誌級別 --> <root level="info"> <appender-ref ref="GELF" /> <appender-ref ref="STDOUT" /> </root> </configuration>
啟動項目,當前項目埠是8081,查看Graylog控制台如圖:
4.2 Log4j2日誌
log4j2日誌使用的是log4j2-gelf依賴,github上面也有對應的介紹,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> <artifactId>springboot2_graylog_log4j</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot2_graylog_log4j</name> <description>springboot2_graylog_log4j</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-logging</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.graylog2.log4j2</groupId> <artifactId>log4j2-gelf</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
創建log4j2-spring.xml進行配置輸出日誌資訊,如下:
<?xml version="1.0" encoding="UTF-8"?><Configuration status="OFF" packages="org.graylog2.log4j2"> <Properties> <Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss:SSS} - %-5level - %pid - %t - %c{1.}:%L - %m%n</Property> </Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT" follow="true"> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="${LOG_PATTERN}"/> </Console> <GELF name="gelfAppender" server="106.13.35.42" port="12201" hostName="appserver01.example.com"> <PatternLayout pattern="%logger{36} - %msg%n"/> <Filters> <Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/> <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/> </Filters> <!-- Additional fields --> <KeyValuePair key="foo" value="bar"/> <KeyValuePair key="jvm" value="${java:vm}"/> </GELF> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="gelfAppender"/> <AppenderRef ref="Console"/> </Root> </Loggers></Configuration>
這個項目使用的埠號是8888,可以在日誌中清晰的看到。
5. ELK vs Graylog
這裡僅以日誌收集為例,簡單說一下二者之間的選擇,我個人的建議就是取決於現有技術棧,比如現在就有現成的Mongodb,那麼選擇Graylog可以節省不少成本,ELK類似,不要盲目的追求技術而選擇。
6. 源碼
springboot2_graylog源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_graylog
springboot2grayloglog4j源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_graylog_log4j