Spring Boot 使用 Graylog 收集日志

您所在的位置:网站首页 springboot日志输出到数据库 Spring Boot 使用 Graylog 收集日志

Spring Boot 使用 Graylog 收集日志

#Spring Boot 使用 Graylog 收集日志| 来源: 网络整理| 查看: 265

点击蓝色“程序猿DD”关注我哟

加个“星标”,不忘签到哦

来源:SpringForAll社区

本文介绍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

springboot2grayloglog4j源码地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_graylog_log4j

推荐阅读:

Mybatis中 $ 和 # 千万不要乱用! 一千个不用 Null 的理由 推荐一款让你纵横Github的读码神器 还在使用 SimpleDateFormat?你的项目崩没? 公益:开放一台Nacos服务端

号外:最近整理了之前编写的一系列内容做成了PDF,关注我并回复相应口令获取: - 001 :领取《Spring Boot基础教程》 - 002 :领取《Spring Cloud基础教程》



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3