# 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