SpringBoot使用Graylog日志收集的实现示例

网友投稿 364 2023-01-08


SpringBoot使用Graylog日志收集的实现示例

本文介绍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://elastic.co/guide/en/elasticsearch/reference/6.6/docker.html

elasticsearch:

image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.6.1

environment:

- http.hoshttp://t=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文件如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.4.RELEASE

com.dalaoyang

springboot2_graylog

0.0.1-SNAPSHOT

springboot2_graylog

springboot2_graylog

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

de.siegmar

logback-gelf

2.0.0

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.4.RELEASE

com.dalaoyang

springboot2_graylog

0.0.1-SNAPSHOT

springboot2_graylog

springboot2_graylog

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

de.siegmar

logback-gelf

2.0.0

org.springframework.boot

spring-boot-maven-plugin

加入logback日志配置,新建logback-spring.xml,内容如下:

${CONSOLE_LOG_PATTERN}

UTF-8

106.13.35.42

12201

启动项目,当前项目端口是8081,查看Graylog控制台如图:

4.2 Log4j2日志

log4j2日志使用的是log4j2-gelf依赖,github上面也有对应的介绍,pom文件如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.4.RELEASE

com.dalaoyang

springboot2_graylog_log4j

0.0.1-SNAPSHOT

springboot2_graylog_log4j

springboot2_graylog_log4j

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter

spring-boot-starter-logging

org.springframework.boot

org.springframework.boot

spring-boot-starter-log4j2

org.graylog2.log4j2

log4j2-gelf

1.3.1

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.4.RELEASE

com.dalaoyang

springboot2_graylog_log4j

0.0.1-SNAPSHOT

springboot2_graylog_log4j

springboot2_graylog_log4j

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter

spring-boot-starter-logging

org.springframework.boot

org.springframework.boot

spring-boot-starter-log4j2

org.graylog2.log4j2

log4j2-gelf

1.3.1

org.springframework.boot

spring-boot-maven-plugin

创建log4j2-spring.xml进行配置输出日志信息,如下:

%d{yyyy-MM-dd HH:mm:ss:SSS} - %-5level - %pid - %t - %c{1.}:%L - %m%n

这个项目使用的端口号是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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:自动框架接口测试(接口测试自动化框架)
下一篇:微服务网关用什么开发(微服务网关集群怎么实现)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~