您好,登錄后才能下訂單哦!
本文介紹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
springboot2_graylog_log4j源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_graylog_log4j
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。