diff --git a/demo-graylog/.gitignore b/demo-graylog/.gitignore deleted file mode 100644 index 153c933..0000000 --- a/demo-graylog/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -HELP.md -/target/ -!.mvn/wrapper/maven-wrapper.jar - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ -/build/ - -### VS Code ### -.vscode/ diff --git a/demo-graylog/README.md b/demo-graylog/README.md deleted file mode 100644 index 84fdd96..0000000 --- a/demo-graylog/README.md +++ /dev/null @@ -1,289 +0,0 @@ -# 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](http://static.xkcoding.com/spring-boot-demo/graylog/063124.jpg) - -2. 设置来源信息 - - ![设置Inputs](http://static.xkcoding.com/spring-boot-demo/graylog/063125.jpg) - - ![image-20190423164748993](http://static.xkcoding.com/spring-boot-demo/graylog/063121-1.jpg) - - ![image-20190423164932488](http://static.xkcoding.com/spring-boot-demo/graylog/063121.jpg) - - ![image-20190423165120586](http://static.xkcoding.com/spring-boot-demo/graylog/063122.jpg) - -## 6. 启动 Spring Boot 项目 - -启动成功后,返回graylog页面查看日志信息 - -![image-20190423165936711](http://static.xkcoding.com/spring-boot-demo/graylog/063123.jpg) - -## 参考 - -- 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/demo-graylog/src/main/java/com/xkcoding/graylog/SpringBootDemoGraylogApplication.java b/demo-graylog/src/main/java/com/xkcoding/graylog/SpringBootDemoGraylogApplication.java deleted file mode 100644 index efb57c8..0000000 --- a/demo-graylog/src/main/java/com/xkcoding/graylog/SpringBootDemoGraylogApplication.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.xkcoding.graylog; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - *

- * 启动器 - *

- * - * @author yangkai.shen - * @date Created in 2019-04-23 09:43 - */ -@SpringBootApplication -public class SpringBootDemoGraylogApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringBootDemoGraylogApplication.class, args); - } - -} diff --git a/demo-graylog/src/main/resources/application.yml b/demo-graylog/src/main/resources/application.yml deleted file mode 100644 index 89e8d8d..0000000 --- a/demo-graylog/src/main/resources/application.yml +++ /dev/null @@ -1,3 +0,0 @@ -spring: - application: - name: graylog \ No newline at end of file diff --git a/demo-graylog/src/test/java/com/xkcoding/graylog/SpringBootDemoGraylogApplicationTests.java b/demo-graylog/src/test/java/com/xkcoding/graylog/SpringBootDemoGraylogApplicationTests.java deleted file mode 100644 index f766812..0000000 --- a/demo-graylog/src/test/java/com/xkcoding/graylog/SpringBootDemoGraylogApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xkcoding.graylog; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringBootDemoGraylogApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/demo-log/demo-log-graylog/README.md b/demo-log/demo-log-graylog/README.md new file mode 100644 index 0000000..b34778b --- /dev/null +++ b/demo-log/demo-log-graylog/README.md @@ -0,0 +1,198 @@ +# spring-boot-demo-graylog + +> 此 demo 主要演示了 Spring Boot 项目如何接入 GrayLog 进行日志管理。 + +## 1.环境准备 + +主要是 graylog 环境的搭建,因为 graylog 依赖 mongo 和 elasticsearch,所以这里我提供了 docker-compose 文件,方便同学们一键启动测试环境 + +> 第一次运行,下载镜像会比较慢,依赖本地的网络环境 + +```bash +$ cd demo-log/demo-log-graylog +$ docker compose -f docker-compose.env.yml up +``` + +## 2.开发步骤 + +### 2.1.添加依赖 + +```xml + + + com.xkcoding + common-tools + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + de.siegmar + logback-gelf + ${logback.gelf.version} + + + + org.projectlombok + lombok + true + + +``` + +### 2.2.添加 logback 配置 + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + utf8 + + + + + + ${GRAY_LOG_HOST} + ${GRAY_LOG_PORT} + 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} + + + + + + + + + + +``` + +### 2.3.修改配置文件 + +```yaml +spring: + application: + name: spring-boot-demo-log-graylog +``` + +## 3.测试 + +### 3.1.模拟日志输出 + +```java +@Slf4j +@EnableScheduling +@SpringBootApplication +public class GraylogApplication { + + public static void main(String[] args) { + SpringApplication.run(GraylogApplication.class, args); + } + + /** + * 定时模拟输出日志 + */ + @Scheduled(initialDelay = 5000, fixedRate = 10000) + public void testLog() { + log.info("[测试日志]log test, name: {}", RandomUtil.randomString(RandomUtil.randomInt(4, 8))); + int seed = RandomUtil.randomInt(0, 20); + if (seed < 5) { + log.error("[测试日志]log error, {}", seed); + } + } + +} +``` + +### 3.2.配置 graylog 控制台,接收日志来源 + +登录 `graylog`,打开浏览器访问:http://localhost:9000 输入配置的 `用户名/密码` 信息(默认为admin/admin) + +![登录graylog](http://static.xkcoding.com/spring-boot-demo/graylog/063124.jpg) + + + +设置日志来源信息 + +![设置Inputs](http://static.xkcoding.com/spring-boot-demo/graylog/063125.jpg) + +![image-20190423164748993](http://static.xkcoding.com/spring-boot-demo/graylog/063121-1.jpg) + +![image-20190423164932488](http://static.xkcoding.com/spring-boot-demo/graylog/063121.jpg) + +![image-20190423165120586](http://static.xkcoding.com/spring-boot-demo/graylog/063122.jpg) + +### 3.3.启动 Spring Boot 项目 + +启动成功后,返回graylog页面查看日志信息 + +![GrayLog 首页](https://static.xkcoding.com/spring-boot-demo/demo-log/demo-log-graylog/2022-09-06-075044.png) + +## 4.参考 + +- [Graylog 官方文档](https://docs.graylog.org/) +- [Graylog 官方文档之docker启动](https://docs.graylog.org/docs/docker) +- [Graylog 镜像地址](https://hub.docker.com/r/graylog/graylog) +- [logback gelf log appender 官方文档](https://github.com/osiegmar/logback-gelf) diff --git a/demo-log/demo-log-graylog/docker-compose.env.yml b/demo-log/demo-log-graylog/docker-compose.env.yml new file mode 100644 index 0000000..166c785 --- /dev/null +++ b/demo-log/demo-log-graylog/docker-compose.env.yml @@ -0,0 +1,51 @@ +version: "3.8" + +services: + mongo: + image: mongo:6.0.1 + + elasticsearch: + image: elasticsearch:7.17.6 + environment: + - http.host=0.0.0.0 + - transport.host=localhost + - network.host=0.0.0.0 + - "ES_JAVA_OPTS=-Dlog4j2.formatMsgNoLookups=true -Xms512m -Xmx512m" + ulimits: + memlock: + soft: -1 + hard: -1 + deploy: + resources: + limits: + memory: 1G + + graylog: + image: graylog/graylog:4.3 + environment: + # 加密盐值,不设置,graylog会启动失败 + # 该字段最少需要16个字符 + - GRAYLOG_PASSWORD_SECRET=somepasswordpepper + # 设置用户名 + - GRAYLOG_ROOT_USERNAME=admin + # 设置密码,此为密码进过SHA256加密后的字符串 + # 加密方式,执行 echo -n "Enter Password: " && head -1 + + com.xkcoding + demo-log + 1.0.0-SNAPSHOT + + 4.0.0 - demo-graylog + demo-log-graylog 1.0.0-SNAPSHOT jar - demo-graylog + demo-log-graylog Demo project for Spring Boot - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - UTF-8 - UTF-8 - 1.8 + 17 + + 4.0.2 + + com.xkcoding + common-tools + + org.springframework.boot spring-boot-starter-web @@ -38,12 +43,18 @@ de.siegmar logback-gelf - 2.0.0 + ${logback.gelf.version} + + + + org.projectlombok + lombok + true - demo-graylog + demo-log-graylog org.springframework.boot diff --git a/demo-log/demo-log-graylog/src/main/java/com/xkcoding/graylog/GraylogApplication.java b/demo-log/demo-log-graylog/src/main/java/com/xkcoding/graylog/GraylogApplication.java new file mode 100644 index 0000000..d6d5dad --- /dev/null +++ b/demo-log/demo-log-graylog/src/main/java/com/xkcoding/graylog/GraylogApplication.java @@ -0,0 +1,39 @@ +package com.xkcoding.graylog; + +import cn.hutool.core.util.RandomUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; + +/** + *

+ * 启动器 + *

+ * + * @author yangkai.shen + * @date Created in 2022-09-05 22:15 + */ +@Slf4j +@EnableScheduling +@SpringBootApplication +public class GraylogApplication { + + public static void main(String[] args) { + SpringApplication.run(GraylogApplication.class, args); + } + + /** + * 定时模拟输出日志 + */ + @Scheduled(initialDelay = 5000, fixedRate = 10000) + public void testLog() { + log.info("[测试日志]log test, name: {}", RandomUtil.randomString(RandomUtil.randomInt(4, 8))); + int seed = RandomUtil.randomInt(0, 20); + if (seed < 5) { + log.error("[测试日志]log error, {}", seed); + } + } + +} diff --git a/demo-log/demo-log-graylog/src/main/resources/application.yml b/demo-log/demo-log-graylog/src/main/resources/application.yml new file mode 100644 index 0000000..7506b04 --- /dev/null +++ b/demo-log/demo-log-graylog/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + application: + name: spring-boot-demo-log-graylog diff --git a/demo-graylog/src/main/resources/logback-spring.xml b/demo-log/demo-log-graylog/src/main/resources/logback-spring.xml similarity index 64% rename from demo-graylog/src/main/resources/logback-spring.xml rename to demo-log/demo-log-graylog/src/main/resources/logback-spring.xml index 9454c4f..9e1f2ea 100644 --- a/demo-graylog/src/main/resources/logback-spring.xml +++ b/demo-log/demo-log-graylog/src/main/resources/logback-spring.xml @@ -25,6 +25,9 @@ + + + @@ -38,8 +41,8 @@ - localhost - 12201 + ${GRAY_LOG_HOST} + ${GRAY_LOG_PORT} 508 true @@ -65,48 +68,7 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/demo-log/demo-log-graylog/src/test/java/com/xkcoding/graylog/GraylogApplicationTests.java b/demo-log/demo-log-graylog/src/test/java/com/xkcoding/graylog/GraylogApplicationTests.java new file mode 100644 index 0000000..9eb4131 --- /dev/null +++ b/demo-log/demo-log-graylog/src/test/java/com/xkcoding/graylog/GraylogApplicationTests.java @@ -0,0 +1,13 @@ +package com.xkcoding.graylog; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class GraylogApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demo-log/pom.xml b/demo-log/pom.xml index 449be52..9a23ade 100644 --- a/demo-log/pom.xml +++ b/demo-log/pom.xml @@ -21,6 +21,7 @@ demo-log-logback demo-log-aop + demo-log-graylog
diff --git a/pom.xml b/pom.xml index cad420d..29fe967 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,6 @@ -