本文介绍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文件如下:
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文件如下:
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
本文标题:SpringBoot使用Graylog日志收集的实现示例
链接分享:http://www.jxjierui.cn/article/ijcsdd.html


咨询
建站咨询
