您好,登錄后才能下訂單哦!
本文介紹SpringBoot如何使用Graylog日志收集。
1.Graylog介紹
Graylog是一個(gè)生產(chǎn)級(jí)別的日志收集系統(tǒng),集成Mongo和Elasticsearch進(jìn)行日志收集。其中Mongo用于存儲(chǔ)Graylog的元數(shù)據(jù)信息和配置信息,ElasticSearch用于存儲(chǔ)數(shù)據(jù)。
架構(gòu)圖如下:
生產(chǎn)環(huán)境配置圖如下:
2.安裝Graylog
在官方文檔上推薦了很多種安裝的方式,這里以docker-compose的方式為例,進(jìn)行安裝Graylog,mongo,elasticsearch。
docker-compose.yml內(nèi)容如下(這里是在官網(wǎng)的基礎(chǔ)上改了一下):
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是我的外網(wǎng)ip,本地服務(wù)使用127.0.0.1即可。
其他方式可以查看官方文檔,https://docs.graylog.org/en/3.0/pages/installation.html
3.配置Graylog
在瀏覽器訪問http://ip:9000,如圖:
這里默認(rèn)用戶名密碼都是admin,進(jìn)入后如圖所示。
選擇System按鈕中的input,錄入一個(gè)輸入源,如圖
這里以GELF UDP為例,在圖中位置選擇GELF UDP,選擇完成后點(diǎn)擊Launch new input,如圖
在Node處選擇自己安裝的,剩下的就根據(jù)需要填寫即可,如圖
保存完成后如圖,到這里就已經(jīng)配置完成了。
4.SpringBoot日志輸出到Graylog
這里分別舉例Logback日志和Log4j2日志。
4.1 Logback日志
這里使用的logback-gelf向Graylog輸出日志,在github上有對(duì)logback-gelf的詳細(xì)使用介紹,這里只是簡(jiǎn)單舉例。Github地址:https://github.com/osiegmar/logback-gelf。
新建項(xiàng)目,加入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,內(nèi)容如下:
<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> <!-- 控制臺(tái)輸出日志級(jí)別 --> <root level="info"> <appender-ref ref="GELF" /> <appender-ref ref="STDOUT" /> </root> </configuration>
啟動(dòng)項(xiàng)目,當(dāng)前項(xiàng)目端口是8081,查看Graylog控制臺(tái)如圖:
4.2 Log4j2日志
log4j2日志使用的是log4j2-gelf依賴,github上面也有對(duì)應(yīng)的介紹,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>
創(chuàng)建log4j2-spring.xml進(jìn)行配置輸出日志信息,如下:
<?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>
這個(gè)項(xiàng)目使用的端口號(hào)是8888,可以在日志中清晰的看到。
5. ELK vs Graylog
這里僅以日志收集為例,簡(jiǎn)單說一下二者之間的選擇,我個(gè)人的建議就是取決于現(xiàn)有技術(shù)棧,比如現(xiàn)在就有現(xiàn)成的Mongodb,那么選擇Graylog可以節(jié)省不少成本,ELK類似,不要盲目的追求技術(shù)而選擇。
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。