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

Graylog是一个生产级别的日志收集系统,集成Mongo和Elasticsearch进行日志收集,这篇文章主要介绍了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://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.bootspring-boot-starter-parent2.1.4.RELEASEcom.dalaoyangspringboot2_graylog0.0.1-SNAPSHOTspringboot2_graylogspringboot2_graylog 1.8  org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-testtest de.siegmarlogback-gelf2.0.0   org.springframework.bootspring-boot-maven-plugin

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

    ${CONSOLE_LOG_PATTERN}UTF-8 106.13.35.4212201 

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

4.2 Log4j2日志

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

   4.0.0 org.springframework.bootspring-boot-starter-parent2.1.4.RELEASEcom.dalaoyangspringboot2_graylog_log4j0.0.1-SNAPSHOTspringboot2_graylog_log4jspringboot2_graylog_log4j 1.8  org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-testtest org.springframework.bootspring-boot-starter  spring-boot-starter-loggingorg.springframework.boot org.springframework.bootspring-boot-starter-log4j2 org.graylog2.log4j2log4j2-gelf1.3.1   org.springframework.bootspring-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日志收集的实现示例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java