docker配置

docker-compose

  1. version: '3'
  2. services:
  3. elasticsearch:
  4. image: elasticsearch:6.6.2
  5. container_name: elasticsearch
  6. restart: always
  7. environment:
  8. - "cluster.name=elasticsearch"
  9. - "discovery.type=single-node"
  10. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  11. ports:
  12. - 9200:9200
  13. - 9300:9300
  14. kibana:
  15. image: docker.elastic.co/kibana/kibana:6.6.2
  16. container_name: kibana
  17. environment:
  18. - SERVER_NAME=kibana
  19. - ELASTICSEARCH_URL=http://elasticsearch:9200
  20. - XPACK_MONITORING_ENABLED=true
  21. ports:
  22. - 5601:5601
  23. depends_on:
  24. - elasticsearch
  25. logstash:
  26. image: logstash:6.6.2
  27. container_name: logstash
  28. volumes:
  29. - ~/docker/mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
  30. depends_on:
  31. - elasticsearch
  32. links:
  33. - elasticsearch:es
  34. ports:
  35. - 4560:4560

logstash.conf

  1. input {
  2. tcp {
  3. mode => "server"
  4. host => "0.0.0.0"
  5. port => 4560
  6. codec => json_lines
  7. type => "business"
  8. }
  9. }
  10. output {
  11. elasticsearch {
  12. hosts => ["es:9200"]
  13. action => "index"
  14. codec => json
  15. index => "%{type}-%{+YYYY.MM.dd}"
  16. template_name => "business"
  17. }
  18. }
上面docker-compose配置中,logstash有一个挂载文件,用来配置logstash. 定义输入输出规则,采用json数据输出. 启动docker容器之前,先创建~/docker/mydata/logstash/logstash.conf文件,否则则启动会有问题.

启动容器

启动命令:docker-compose up -d

logback配置

  1. <!-- 集成logstash -->
  2. <dependency>
  3. <groupId>net.logstash.logback</groupId>
  4. <artifactId>logstash-logback-encoder</artifactId>
  5. <version>5.3</version>
  6. </dependency>

logback-spring.xml

通过logback-spring.xml指定日志输出,在配置文件中添加日志输出到logstash,完整配置:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <configuration>
  3. <!--应用名称-->
  4. <springProperty scope="context" name="APP_NAME" source="spring.application.name" defaultValue="springBoot"/>
  5. <!--LogStash访问host-->
  6. <springProperty name="LOG_STASH_HOST" scope="context" source="logstash.host" defaultValue="localhost"/>
  7. <!-- 控制台输出日志 -->
  8. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  9. <encoder>
  10. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{traceId}] [%level] [%c:%L] - %msg%n</pattern>
  11. </encoder>
  12. </appender>
  13. <!--每天生成一个日志文件,保存30天的日志文件。-->
  14. <appender name="DayFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
  15. <File>logs/log.log</File>
  16. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  17. <fileNamePattern>logs/log.%d{yyyy-MM-dd}.log</fileNamePattern>
  18. <maxHistory>30</maxHistory>
  19. </rollingPolicy>
  20. <encoder>
  21. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{traceId}] [%level] [%thread] [%c:%L] - %msg%n</pattern>
  22. </encoder>
  23. </appender>
  24. <!--业务日志输出到LogStash-->
  25. <appender name="LOG_STASH_BUSINESS" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
  26. <destination>${LOG_STASH_HOST}:4560</destination>
  27. <encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
  28. <providers>
  29. <timestamp>
  30. <timeZone>Asia/Shanghai</timeZone>
  31. </timestamp>
  32. <!--自定义日志输出格式-->
  33. <pattern>
  34. <pattern>
  35. {
  36. "service": "${APP_NAME:-}",
  37. "level": "%level",
  38. "pid": "${PID:-}",
  39. "thread": "%thread",
  40. "class": "%logger",
  41. "traceId": "%X{traceId:-}",
  42. "message": "%message",
  43. "stack_trace": "%exception"
  44. }
  45. </pattern>
  46. </pattern>
  47. </providers>
  48. </encoder>
  49. </appender>
  50. <!--指定logger name为包名或类全名 指定级别 additivity设置是否传递到root logger -->
  51. <logger name="slf4j" level="INFO" additivity="false">
  52. <appender-ref ref="STDOUT"/>
  53. <appender-ref ref="DayFile"/>
  54. <appender-ref ref="LOG_STASH_BUSINESS"/>
  55. </logger>
  56. <!--slf4j2包下的类在ERROR级别时候传递到root logger中-->
  57. <logger name="slf4j2" level="ERROR"/>
  58. <!--根logger控制-->
  59. <root level="INFO">
  60. <appender-ref ref="STDOUT"/>
  61. <appender-ref ref="DayFile"/>
  62. <appender-ref ref="LOG_STASH_BUSINESS"/>
  63. </root>
  64. </configuration>