diff --git a/spring-boot-demo-graylog/README.md b/spring-boot-demo-graylog/README.md new file mode 100644 index 0000000..e45a46c --- /dev/null +++ b/spring-boot-demo-graylog/README.md @@ -0,0 +1,289 @@ +# spring-boot-demo-graylog + +> 此 demo 主要演示了 Spring Boot 项目如何接入 GrayLog 进行日志管理。 + +## 注意 + +作者在编写此 demo 时,`graylog` 采用 `docker-compose` 启动,其中 `graylog` 依赖的 `mongodb` 以及 `elasticsearch` 都同步启动,生产环境建议使用外部存储。 + +## 1. 环境准备 + +**编写 `graylog` 的 `docker-compose` 启动文件** + +> 如果本地没有 `mongo:3` 和 `elasticsearch-oss:6.6.1` 的镜像,会比较耗时间 + +```yaml +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=-Xms512m -Xmx512m" + ulimits: + memlock: + soft: -1 + hard: -1 + mem_limit: 1g + # Graylog: https://hub.docker.com/r/graylog/graylog/ + graylog: + image: graylog/graylog:3.0 + environment: + # 加密盐值,不设置,graylog会启动失败 + # 该字段最少需要16个字符 + - GRAYLOG_PASSWORD_SECRET=somepasswordpepper + # 设置用户名 + - GRAYLOG_ROOT_USERNAME=admin + # 设置密码,此为密码进过SHA256加密后的字符串 + # 加密方式,执行 echo -n "Enter Password: " && head -1 + + 4.0.0 + + spring-boot-demo-graylog + 1.0.0-SNAPSHOT + jar + + spring-boot-demo-graylog + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + de.siegmar + logback-gelf + 2.0.0 + + + + + spring-boot-demo-graylog + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## 3. application.yml + +```yaml +spring: + application: + name: graylog +``` + +## 4. logback-spring.xml + +```xml + + + + + + + + + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + utf8 + + + + + + localhost + 12201 + 508 + true + + true + true + true + false + false + true + + ${GRAY_LOG_SHORT_PATTERN} + + + ${GRAY_LOG_FULL_PATTERN} + + app_name:${APP_NAME} + os_arch:${os.arch} + os_name:${os.name} + os_version:${os.version} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +## 5. 配置 graylog 控制台,接收日志来源 + +1. 登录 `graylog`,打开浏览器访问:http://localhost:9000 + + 输入 `docker-compose.yml` 里配置的 `用户名/密码` 信息 + + ![登录graylog](assets/image-20190423164322865.png) + +2. 设置来源信息 + + ![设置Inputs](assets/image-20190423164633440.png) + + ![image-20190423164748993](assets/image-20190423164748993.png) + + ![image-20190423164932488](assets/image-20190423164932488.png) + + ![image-20190423165120586](assets/image-20190423165120586.png) + +## 6. 启动 Spring Boot 项目 + +启动成功后,返回graylog页面查看日志信息 + +![image-20190423165936711](assets/image-20190423165936711.png) + +## 参考 + +- graylog 官方下载地址:https://www.graylog.org/downloads#open-source + +- graylog 官方docker镜像:https://hub.docker.com/r/graylog/graylog/ + +- graylog 镜像启动方式:http://docs.graylog.org/en/stable/pages/installation/docker.html + +- graylog 启动参数配置:http://docs.graylog.org/en/stable/pages/configuration/server.conf.html + + 注意,启动参数需要加 `GRAYLOG_` 前缀 + +- 日志收集依赖:https://github.com/osiegmar/logback-gelf \ No newline at end of file diff --git a/spring-boot-demo-graylog/assets/image-20190423164322865.png b/spring-boot-demo-graylog/assets/image-20190423164322865.png new file mode 100644 index 0000000..f18fe40 Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423164322865.png differ diff --git a/spring-boot-demo-graylog/assets/image-20190423164633440.png b/spring-boot-demo-graylog/assets/image-20190423164633440.png new file mode 100644 index 0000000..f8b5f49 Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423164633440.png differ diff --git a/spring-boot-demo-graylog/assets/image-20190423164744615.png b/spring-boot-demo-graylog/assets/image-20190423164744615.png new file mode 100644 index 0000000..39afb4f Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423164744615.png differ diff --git a/spring-boot-demo-graylog/assets/image-20190423164748993.png b/spring-boot-demo-graylog/assets/image-20190423164748993.png new file mode 100644 index 0000000..39afb4f Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423164748993.png differ diff --git a/spring-boot-demo-graylog/assets/image-20190423164932488.png b/spring-boot-demo-graylog/assets/image-20190423164932488.png new file mode 100644 index 0000000..d388ab8 Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423164932488.png differ diff --git a/spring-boot-demo-graylog/assets/image-20190423165120586.png b/spring-boot-demo-graylog/assets/image-20190423165120586.png new file mode 100644 index 0000000..5bb8c16 Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423165120586.png differ diff --git a/spring-boot-demo-graylog/assets/image-20190423165936711.png b/spring-boot-demo-graylog/assets/image-20190423165936711.png new file mode 100644 index 0000000..14874bf Binary files /dev/null and b/spring-boot-demo-graylog/assets/image-20190423165936711.png differ diff --git a/spring-boot-demo-graylog/pom.xml b/spring-boot-demo-graylog/pom.xml index 46b64e1..167e62b 100644 --- a/spring-boot-demo-graylog/pom.xml +++ b/spring-boot-demo-graylog/pom.xml @@ -34,6 +34,7 @@ test + de.siegmar logback-gelf