1. docker
    1. mongodb 4.2
    2. elasticsearch 7.10.2
    3. graylog 4.1
  2. spring
    1. boot 2.5.4
    2. logback-gelf 3.0.0(de.siegmar

docker-compose file

gray 的访问地址为:127.0.0.1:9000

2021-08-30 目前官网最新 注意:

  1. 设置时区GRAYLOG_ROOT_TIMEZONE: Asia/Shanghai,默认UTC
  1. version: "3.8"
  2. services:
  3. mongodb:
  4. image: "mongo:4.2"
  5. container_name: graylog_demo_mongo
  6. volumes:
  7. - "mongodb_data:/data/db"
  8. restart: "always"
  9. elasticsearch:
  10. environment:
  11. ES_JAVA_OPTS: "-Xms1g -Xmx1g"
  12. bootstrap.memory_lock: "true"
  13. discovery.type: "single-node"
  14. http.host: "0.0.0.0"
  15. action.auto_create_index: "false"
  16. image: "docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2"
  17. container_name: graylog_demo_elasticsearch
  18. ulimits:
  19. memlock:
  20. hard: -1
  21. soft: -1
  22. volumes:
  23. - "es_data:/usr/share/elasticsearch/data"
  24. restart: "always"
  25. graylog:
  26. image: "graylog/graylog:4.1"
  27. container_name: graylog_demo_graylog
  28. depends_on:
  29. elasticsearch:
  30. condition: "service_started"
  31. mongodb:
  32. condition: "service_started"
  33. entrypoint: "/usr/bin/tini -- wait-for-it elasticsearch:9200 -- /docker-entrypoint.sh"
  34. environment:
  35. GRAYLOG_NODE_ID_FILE: "/usr/share/graylog/data/config/node-id"
  36. GRAYLOG_PASSWORD_SECRET: somepasswordpepper # CHANGE ME (must be at least 16 characters)!
  37. GRAYLOG_ROOT_PASSWORD_SHA2: 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 # Password: admin
  38. GRAYLOG_HTTP_BIND_ADDRESS: "0.0.0.0:9000"
  39. GRAYLOG_HTTP_EXTERNAL_URI: "http://localhost:9000/"
  40. GRAYLOG_ELASTICSEARCH_HOSTS: "http://elasticsearch:9200"
  41. GRAYLOG_MONGODB_URI: "mongodb://mongodb:27017/graylog"
  42. GRAYLOG_ROOT_TIMEZONE: Asia/Shanghai
  43. ports:
  44. - "5044:5044/tcp" # Beats
  45. - "5140:5140/udp" # Syslog
  46. - "5140:5140/tcp" # Syslog
  47. - "5555:5555/tcp" # RAW TCP
  48. - "5555:5555/udp" # RAW TCP
  49. - "9000:9000/tcp" # Server API
  50. - "12201:12201/tcp" # GELF TCP
  51. - "12201:12201/udp" # GELF UDP
  52. #- "10000:10000/tcp" # Custom TCP port
  53. #- "10000:10000/udp" # Custom UDP port
  54. - "13301:13301/tcp" # Forwarder data
  55. - "13302:13302/tcp" # Forwarder config
  56. volumes:
  57. - "graylog_data:/usr/share/graylog/data/data"
  58. - "graylog_journal:/usr/share/graylog/data/journal"
  59. restart: "always"
  60. volumes:
  61. mongodb_data:
  62. es_data:
  63. graylog_data:
  64. graylog_journal:

spring boot

配置日志传输通道

image.png

依赖

选用的日志插件是:https://github.com/osiegmar/logback-gelf 插件查询地址:https://marketplace.graylog.org/

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.5.4</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.tan</groupId>
  12. <artifactId>graylog</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>graylog</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. <!--日志平台-->
  35. <!-- https://mvnrepository.com/artifact/de.siegmar/logback-gelf -->
  36. <dependency>
  37. <groupId>de.siegmar</groupId>
  38. <artifactId>logback-gelf</artifactId>
  39. <version>3.0.0</version>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. <configuration>
  48. <excludes>
  49. <exclude>
  50. <groupId>org.projectlombok</groupId>
  51. <artifactId>lombok</artifactId>
  52. </exclude>
  53. </excludes>
  54. </configuration>
  55. </plugin>
  56. </plugins>
  57. </build>
  58. </project>

logback.xml

官方配置:https://github.com/osiegmar/logback-gelf/blob/v3.0.0/examples/advanced_udp.xml 注意:

  1. includeLevelName是否显示日志级别的名字(默认看的是等级1-6)
  2. <staticField>app_name:graylog</staticField> 设置当前应用的名称,方便查询(一定要设置)
  3. 数据传输的端口和地址
    1. <graylogHost>localhost</graylogHost>
    2. <graylogPort>12201</graylogPort>
    3. image.png
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- logging.config= classpath:logback-spring.xml -->
  3. <!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 -->
  4. <!-- scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true -->
  5. <!-- scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒;当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
  6. <!-- debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态,默认值为false。 -->
  7. <configuration
  8. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9. xsi:noNamespaceSchemaLocation="http://www.padual.com/java/logback.xsd"
  10. debug="false" scan="true" scanPeriod="10 second">
  11. <!-- 配置日志输出目录文件夹以及进行重命名 -->
  12. <property name="PROJECT" value="controller" />
  13. <property name="ROOT" value="logs/${PROJECT}/" />
  14. <!-- 配置日志文件大小 -->
  15. <property name="FILESIZE" value="50MB" />
  16. <!-- 设置日志保留的时间,单位为天 -->
  17. <property name="MAXHISTORY" value="2" />
  18. <!-- 格式化输出日期 -->
  19. <timestamp key="DATETIME" datePattern="yyyy-MM-dd HH:mm:ss.SSS" />
  20. <!-- 控制台打印 -->
  21. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  22. <encoder charset="utf-8">
  23. <!-- 格式化日志输出:
  24. %-5level表示级别从左显示5个字符宽度,%d表示日期,%thread表示线程名,%m表示日志消息,%n是换行符
  25. %logger{36}表示logger是class的全名,36表示限制最长字符
  26. -->
  27. <pattern>%yellow(%date{yyyy-MM-dd HH:mm:ss}) |%highlight(%-5level) |%green(%logger:%line) |%white(%msg%n)</pattern>
  28. </encoder>
  29. </appender>
  30. <!-- ERROR日志输出到文件 -->
  31. <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
  32. <encoder charset="utf-8">
  33. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %m%n</pattern>
  34. </encoder>
  35. <!-- 设置当前日志文档输出的级别,只记录ERROR级别的日志 -->
  36. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  37. <level>ERROR</level>
  38. <onMatch>ACCEPT</onMatch>
  39. <onMismatch>DENY</onMismatch>
  40. </filter>
  41. <!-- 设置日志记录器的滚动策略,按日期和大小记录-->
  42. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  43. <fileNamePattern>${ROOT}%d/error.%i.log</fileNamePattern>
  44. <maxHistory>${MAXHISTORY}</maxHistory>
  45. <timeBasedFileNamingAndTriggeringPolicy
  46. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  47. <maxFileSize>${FILESIZE}</maxFileSize>
  48. </timeBasedFileNamingAndTriggeringPolicy>
  49. </rollingPolicy>
  50. </appender>
  51. <!-- WARN日志输出到文件 -->
  52. <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
  53. <encoder charset="utf-8">
  54. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %m%n</pattern>
  55. </encoder>
  56. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  57. <level>WARN</level>
  58. <onMatch>ACCEPT</onMatch>
  59. <onMismatch>DENY</onMismatch>
  60. </filter>
  61. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  62. <fileNamePattern>${ROOT}%d/warn.%i.log</fileNamePattern>
  63. <maxHistory>${MAXHISTORY}</maxHistory>
  64. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  65. <maxFileSize>${FILESIZE}</maxFileSize>
  66. </timeBasedFileNamingAndTriggeringPolicy>
  67. </rollingPolicy>
  68. </appender>
  69. <!-- INFO日志输出到文件 -->
  70. <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
  71. <encoder charset="utf-8">
  72. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %m%n</pattern>
  73. </encoder>
  74. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  75. <level>INFO</level>
  76. <onMatch>ACCEPT</onMatch>
  77. <onMismatch>DENY</onMismatch>
  78. </filter>
  79. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  80. <fileNamePattern>${ROOT}%d/info.%i.log</fileNamePattern>
  81. <maxHistory>${MAXHISTORY}</maxHistory>
  82. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  83. <maxFileSize>${FILESIZE}</maxFileSize>
  84. </timeBasedFileNamingAndTriggeringPolicy>
  85. </rollingPolicy>
  86. </appender>
  87. <!-- DEBUG日志输出到文件 -->
  88. <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
  89. <encoder charset="utf-8">
  90. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %m%n</pattern>
  91. </encoder>
  92. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  93. <level>DEBUG</level>
  94. <onMatch>ACCEPT</onMatch>
  95. <onMismatch>DENY</onMismatch>
  96. </filter>
  97. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  98. <fileNamePattern>${ROOT}%d/debug.%i.log</fileNamePattern>
  99. <maxHistory>${MAXHISTORY}</maxHistory>
  100. <timeBasedFileNamingAndTriggeringPolicy
  101. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  102. <maxFileSize>${FILESIZE}</maxFileSize>
  103. </timeBasedFileNamingAndTriggeringPolicy>
  104. </rollingPolicy>
  105. </appender>
  106. <!-- TRACE日志输出到文件 -->
  107. <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  108. <encoder charset="utf-8">
  109. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %m%n</pattern>
  110. </encoder>
  111. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  112. <level>TRACE</level>
  113. <onMatch>ACCEPT</onMatch>
  114. <onMismatch>DENY</onMismatch>
  115. </filter>
  116. <rollingPolicy
  117. class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  118. <fileNamePattern>${ROOT}%d/trace.%i.log</fileNamePattern>
  119. <maxHistory>${MAXHISTORY}</maxHistory>
  120. <timeBasedFileNamingAndTriggeringPolicy
  121. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  122. <maxFileSize>${FILESIZE}</maxFileSize>
  123. </timeBasedFileNamingAndTriggeringPolicy>
  124. </rollingPolicy>
  125. </appender>
  126. <!-- <appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender">
  127. <graylogHost>localhost</graylogHost>
  128. <graylogPort>12201</graylogPort>
  129. </appender>-->
  130. <appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender">
  131. <graylogHost>localhost</graylogHost>
  132. <graylogPort>12201</graylogPort>
  133. <maxChunkSize>508</maxChunkSize>
  134. <useCompression>true</useCompression>
  135. <messageIdSupplier class="de.siegmar.logbackgelf.MessageIdSupplier"/>
  136. <encoder class="de.siegmar.logbackgelf.GelfEncoder">
  137. <originHost>localhost</originHost>
  138. <includeRawMessage>false</includeRawMessage>
  139. <includeMarker>true</includeMarker>
  140. <includeMdcData>true</includeMdcData>
  141. <includeCallerData>false</includeCallerData>
  142. <includeRootCauseData>false</includeRootCauseData>
  143. <includeLevelName>true</includeLevelName>
  144. <shortPatternLayout class="ch.qos.logback.classic.PatternLayout">
  145. <pattern>%d - %m%nopex</pattern>
  146. </shortPatternLayout>
  147. <fullPatternLayout class="ch.qos.logback.classic.PatternLayout">
  148. <pattern>%d - %m%n</pattern>
  149. </fullPatternLayout>
  150. <numbersAsString>false</numbersAsString>
  151. <staticField>app_name:graylog</staticField>
  152. <staticField>os_arch:${os.arch}</staticField>
  153. <staticField>os_name:${os.name}</staticField>
  154. <staticField>os_version:${os.version}</staticField>
  155. </encoder>
  156. </appender>
  157. <!-- 输出sql相关日志,不需要可删除;additivity:是否向上级loger传递打印信息,默认是true-->
  158. <logger name="org.apache.ibatis" level="INFO" additivity="false" />
  159. <logger name="org.mybatis.spring" level="INFO" additivity="false" />
  160. <logger name="com.github.miemiedev.mybatis.paginator" level="INFO" additivity="false" />
  161. <!-- nacos改变日志级别 -->
  162. <logger name="com.alibaba.nacos.client" level="ERROR" additivity="false" />
  163. <springProfile name="dev">
  164. <root level="debug">
  165. <appender-ref ref="STDOUT"/>
  166. <appender-ref ref="GELF" />
  167. </root>
  168. </springProfile>
  169. <!-- 非dev环境下-->
  170. <springProfile name="!dev">
  171. <!-- logger设置输出到文件,输出级别为info -->
  172. <root level="INFO">
  173. <appender-ref ref="GELF" />
  174. <appender-ref ref="STDOUT" />
  175. <appender-ref ref="DEBUG" />
  176. <appender-ref ref="ERROR" />
  177. <appender-ref ref="WARN" />
  178. <appender-ref ref="INFO" />
  179. <appender-ref ref="TRACE" />
  180. </root>
  181. </springProfile>
  182. </configuration>