日志全局配置

系统默认提供logback的日志,具体配置如下

logback.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="./logs"/>
  4. <springProperty scope="context" name="appName" source="spring.application.name" defaultValue="company-demo"/>
  5. <property name="LOG_HOME" value="${logPath}"/>
  6. <!--
  7. <contextName>${appName}</contextName>
  8. -->
  9. <conversionRule conversionWord="clr"
  10. converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
  11. <conversionRule conversionWord="wex"
  12. converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
  13. <conversionRule conversionWord="wEx"
  14. converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
  15. <!-- 控制台输出 -->
  16. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  17. <encoder>
  18. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  19. <pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
  20. </pattern>
  21. </encoder>
  22. </appender>
  23. <!-- 按照每天生成日志文件(正常类型文件) -->
  24. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  25. <file>${LOG_HOME}/${appName}.log</file>
  26. <append>true</append>
  27. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  28. <!--日志文件输出的文件名 -->
  29. <FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}.log.zip</FileNamePattern>
  30. <maxFileSize>200MB</maxFileSize>
  31. <MaxHistory>7</MaxHistory>
  32. <totalSizeCap>20GB</totalSizeCap>
  33. </rollingPolicy>
  34. <encoder>
  35. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  36. <pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
  37. </pattern>
  38. </encoder>
  39. </appender>
  40. <!-- 按照每天生成日志文件(正常类型文件) -->
  41. <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
  42. <file>${LOG_HOME}/${appName}-info.log</file>
  43. <append>true</append>
  44. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  45. <!--日志文件输出的文件名 -->
  46. <FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}-info.log.zip</FileNamePattern>
  47. <maxFileSize>200MB</maxFileSize>
  48. <MaxHistory>7</MaxHistory>
  49. <totalSizeCap>20GB</totalSizeCap>
  50. </rollingPolicy>
  51. <encoder>
  52. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  53. <pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
  54. </pattern>
  55. </encoder>
  56. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  57. <level>INFO</level>
  58. <onMatch>ACCEPT</onMatch>
  59. <onMismatch>DENY</onMismatch>
  60. </filter>
  61. </appender>
  62. <!-- 按照每天生成日志文件(警告类型文件) -->
  63. <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
  64. <file>${LOG_HOME}/${appName}-warn.log</file>
  65. <append>true</append>
  66. <rollingPolicy
  67. class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  68. <!--日志文件输出的文件名 -->
  69. <FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}-warn.log.zip</FileNamePattern>
  70. <maxFileSize>200MB</maxFileSize>
  71. <MaxHistory>7</MaxHistory>
  72. <totalSizeCap>20GB</totalSizeCap>
  73. </rollingPolicy>
  74. <encoder>
  75. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  76. <pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
  77. </pattern>
  78. </encoder>
  79. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  80. <level>WARN</level>
  81. <onMatch>ACCEPT</onMatch>
  82. <onMismatch>DENY</onMismatch>
  83. </filter>
  84. </appender>
  85. <!-- 按照每天生成日志文件(错误类型文件) -->
  86. <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
  87. <file>${LOG_HOME}/${appName}-error.log</file>
  88. <append>true</append>
  89. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  90. <!--日志文件输出的文件名 -->
  91. <FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}-error.log.zip</FileNamePattern>
  92. <maxFileSize>200MB</maxFileSize>
  93. <MaxHistory>7</MaxHistory>
  94. <totalSizeCap>20GB</totalSizeCap>
  95. </rollingPolicy>
  96. <encoder>
  97. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  98. <pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
  99. </pattern>
  100. </encoder>
  101. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  102. <level>ERROR</level>
  103. <onMatch>ACCEPT</onMatch>
  104. <onMismatch>DENY</onMismatch>
  105. </filter>
  106. </appender>
  107. <!--配置CatAppender,所有的log.error日志,都会打印到cat监控面板-->
  108. <appender name="CatAppender" class="com.dianping.cat.logback.CatLogbackAppender"/>
  109. <!-- 指定环境输出指定日志 -->
  110. <springProfile name="local,dev,test">
  111. <logger name="java.sql.Connection" level="WARN"/>
  112. <logger name="java.sql.Statement" level="WARN"/>
  113. <logger name="java.sql.PreparedStatement" level="WARN"/>
  114. <logger name="com.company.demo" level="INFO"/>
  115. <logger name="com.company.demo.dmr.mapper" level="INFO"/>
  116. <logger name="RocketmqClient" level="WARN"/>
  117. <logger name="RocketmqCommon" level="WARN"/>
  118. <logger name="RocketmqRemoting" level="WARN"/>
  119. <logger name="org.springframework.boot.actuate.endpoint.web.servlet" level="WARN"/>
  120. <!-- 日志输出级别 -->
  121. <root level="INFO">
  122. <!---->
  123. <appender-ref ref="CONSOLE"/>
  124. <appender-ref ref="FILE"/>
  125. <appender-ref ref="CatAppender"/>
  126. <!--
  127. <appender-ref ref="INFO"/>
  128. <appender-ref ref="ERROR"/>
  129. <appender-ref ref="WARN"/>
  130. -->
  131. </root>
  132. </springProfile>
  133. <!-- 指定环境输出指定日志 -->
  134. <springProfile name="staging,pro,task">
  135. <logger name="java.sql.Connection" level="WARN"/>
  136. <logger name="java.sql.Statement" level="WARN"/>
  137. <logger name="java.sql.PreparedStatement" level="WARN"/>
  138. <logger name="com.company.demo" level="INFO"/>
  139. <logger name="com.company.demo.dmr.mapper" level="INFO"/>
  140. <logger name="RocketmqClient" level="WARN"/>
  141. <logger name="RocketmqCommon" level="WARN"/>
  142. <logger name="RocketmqRemoting" level="WARN"/>
  143. <logger name="org.springframework.boot.actuate.endpoint.web.servlet" level="WARN"/>
  144. <!-- 日志输出级别 -->
  145. <root level="INFO">
  146. <appender-ref ref="CONSOLE"/>
  147. <appender-ref ref="FILE"/>
  148. <appender-ref ref="CatAppender"/>
  149. <!--
  150. <appender-ref ref="ERROR"/>
  151. <appender-ref ref="INFO"/>
  152. <appender-ref ref="WARN"/>
  153. -->
  154. </root>
  155. </springProfile>
  156. </configuration>

请求/返回参数日志

提供基于@LogPoint注解方法打印和RequstMapping等spring注解的方法对请求返回参数进行打印。

��配置如下:

  1. #方法上添加LogPoint注解
  2. xy.log.type=annotation
  3. #拦截restful请求各种Mapping进行拦截
  4. xy.log.type=mapping
  5. xy.log.annotation.enable=true
  6. #指定方url不打印请求入参数据
  7. xy.log.annotation.skip-request=/public/sentinelTest0,/public/sentinelTest1
  8. #指定url不打印请求返回数据
  9. xy.log.annotation.skip-response=/public/sentinelTest0,/public/sentinelTest1
  10. xy.log.mapping.enable=true
  11. #指定方url不打印请求入参数据
  12. xy.log.mapping.skip-request=/ss
  13. #指定url不打印请求返回数据
  14. xy.log.mapping.skip-response=/ss

日志格式

系统日志 - 图1

[请求时间]-[服务名]-[线程ID]-[日志级别]-[当前日志输出的Java对象]-[traceId]-[userId]-[日志类容]

taceId在整个请求链路同步处理中唯一且相同。

对于异步线程处理,若需要记录traceId,需要手动从主线程中通过MDC.get(“traceId”)获取并传递给异步线程。