SpringBoot logback Elasticsearch 日志

1、引入Elasticsearch依赖

  1. <dependency>
  2. <groupId>com.internetitem</groupId>
  3. <artifactId>logback-elasticsearch-appender</artifactId>
  4. <version>1.6</version>
  5. </dependency>

2、添加logback-spring.xml到resource目录的根目录下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <property name="LOGS" value="./logs"/>
  4. <appender name="Console"
  5. class="ch.qos.logback.core.ConsoleAppender">
  6. <layout class="ch.qos.logback.classic.PatternLayout">
  7. <Pattern>
  8. %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
  9. </Pattern>
  10. </layout>
  11. </appender>
  12. <appender name="RollingFile"
  13. class="ch.qos.logback.core.rolling.RollingFileAppender">
  14. <file>${LOGS}/spring-boot-logger.log</file>
  15. <encoder
  16. class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  17. <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
  18. </encoder>
  19. <rollingPolicy
  20. class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  21. <!-- rollover daily and when the file reaches 10 MegaBytes -->
  22. <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
  23. </fileNamePattern>
  24. <timeBasedFileNamingAndTriggeringPolicy
  25. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  26. <maxFileSize>10MB</maxFileSize>
  27. </timeBasedFileNamingAndTriggeringPolicy>
  28. </rollingPolicy>
  29. </appender>
  30. <appender name="ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
  31. <url>ES连接字符串</url>
  32. <index>linkanyway-logs-%date{yyyy-MM-dd}</index>
  33. <type>tester</type>
  34. <loggerName>linkanyway-logger</loggerName> <!-- optional -->
  35. <errorLoggerName>linkanyway-error-logger</errorLoggerName> <!-- optional -->
  36. <connectTimeout>30000</connectTimeout> <!-- optional (in ms, default 30000) -->
  37. <errorsToStderr>false</errorsToStderr> <!-- optional (default false) -->
  38. <includeCallerData>false</includeCallerData> <!-- optional (default false) -->
  39. <logsToStderr>false</logsToStderr> <!-- optional (default false) -->
  40. <maxQueueSize>104857600</maxQueueSize> <!-- optional (default 104857600) -->
  41. <maxRetries>3</maxRetries> <!-- optional (default 3) -->
  42. <readTimeout>30000</readTimeout> <!-- optional (in ms, default 30000) -->
  43. <sleepTime>250</sleepTime> <!-- optional (in ms, default 250) -->
  44. <rawJsonMessage>false</rawJsonMessage> <!-- optional (default false) -->
  45. <includeMdc>false</includeMdc> <!-- optional (default false) -->
  46. <maxMessageSize>100</maxMessageSize> <!-- optional (default -1 -->
  47. <authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication"/> <!-- optional -->
  48. <properties>
  49. <property>
  50. <name>host</name>
  51. <value>${HOSTNAME}</value>
  52. <allowEmpty>false</allowEmpty>
  53. </property>
  54. <property>
  55. <name>severity</name>
  56. <value>%level</value>
  57. </property>
  58. <property>
  59. <name>thread</name>
  60. <value>%thread</value>
  61. </property>
  62. <property>
  63. <name>stacktrace</name>
  64. <value>%ex</value>
  65. </property>
  66. <property>
  67. <name>logger</name>
  68. <value>%logger</value>
  69. </property>
  70. </properties>
  71. <headers>
  72. <header>
  73. <name>Content-Type</name>
  74. <value>application/json</value>
  75. </header>
  76. </headers>
  77. </appender>
  78. <!-- LOG everything at INFO level -->
  79. <root level="info">
  80. <appender-ref ref="RollingFile"/>
  81. <appender-ref ref="Console"/>
  82. <!--所有的日志都会通过ELASTIC为名的appender计入es-->
  83. <appender-ref ref="ELASTIC"/>
  84. </root>
  85. <!-- LOG "com.baeldung*" at TRACE level -->
  86. <logger name="com.baeldung" level="trace" additivity="false">
  87. <appender-ref ref="RollingFile"/>
  88. <appender-ref ref="ELASTIC"/>
  89. </logger>
  90. <logger name="es-logger" level="INFO" additivity="false">
  91. <appender-ref ref="ELASTIC"/>
  92. </logger>
  93. </configuration>

3、在application.yml中设定日志level

  1. logging:
  2. level:
  3. root: info

4、在root部分不适用es记录日志而在需要的代码中用如下方式记录日志

  1. package com.linkanyway.portal.controllers;
  2. import com.linkanyway.sdk.exception.ApiResult;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * @author Fcant
  10. * @version 1.0
  11. * @name EsLoggerController
  12. * @description TODO
  13. * @date 2020/12/13 13:14
  14. */
  15. @RestController
  16. @RequestMapping("log")
  17. public class EsLoggerController {
  18. private Logger logger = LoggerFactory.getLogger("es-logger");
  19. @GetMapping("log")
  20. public ApiResult<String> log()
  21. {
  22. logger.warn ("eslogger");
  23. return ApiResult.success ("done!");
  24. }
  25. }