日志全局配置
系统默认提供logback的日志,具体配置如下
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="./logs"/>
<springProperty scope="context" name="appName" source="spring.application.name" defaultValue="company-demo"/>
<property name="LOG_HOME" value="${logPath}"/>
<!--
<contextName>${appName}</contextName>
-->
<conversionRule conversionWord="clr"
converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex"
converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
<conversionRule conversionWord="wEx"
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<!-- 控制台输出 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志文件(正常类型文件) -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${appName}.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}.log.zip</FileNamePattern>
<maxFileSize>200MB</maxFileSize>
<MaxHistory>7</MaxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志文件(正常类型文件) -->
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${appName}-info.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}-info.log.zip</FileNamePattern>
<maxFileSize>200MB</maxFileSize>
<MaxHistory>7</MaxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 按照每天生成日志文件(警告类型文件) -->
<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${appName}-warn.log</file>
<append>true</append>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}-warn.log.zip</FileNamePattern>
<maxFileSize>200MB</maxFileSize>
<MaxHistory>7</MaxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 按照每天生成日志文件(错误类型文件) -->
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${appName}-error.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd}.%i.${appName}-error.log.zip</FileNamePattern>
<maxFileSize>200MB</maxFileSize>
<MaxHistory>7</MaxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>[%d{yyyyMMdd HH:mm:ss}][${appName}][%thread][%level][%logger{36}][%X{traceId}][%X{userId}][%msg]%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--配置CatAppender,所有的log.error日志,都会打印到cat监控面板-->
<appender name="CatAppender" class="com.dianping.cat.logback.CatLogbackAppender"/>
<!-- 指定环境输出指定日志 -->
<springProfile name="local,dev,test">
<logger name="java.sql.Connection" level="WARN"/>
<logger name="java.sql.Statement" level="WARN"/>
<logger name="java.sql.PreparedStatement" level="WARN"/>
<logger name="com.company.demo" level="INFO"/>
<logger name="com.company.demo.dmr.mapper" level="INFO"/>
<logger name="RocketmqClient" level="WARN"/>
<logger name="RocketmqCommon" level="WARN"/>
<logger name="RocketmqRemoting" level="WARN"/>
<logger name="org.springframework.boot.actuate.endpoint.web.servlet" level="WARN"/>
<!-- 日志输出级别 -->
<root level="INFO">
<!---->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="CatAppender"/>
<!--
<appender-ref ref="INFO"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="WARN"/>
-->
</root>
</springProfile>
<!-- 指定环境输出指定日志 -->
<springProfile name="staging,pro,task">
<logger name="java.sql.Connection" level="WARN"/>
<logger name="java.sql.Statement" level="WARN"/>
<logger name="java.sql.PreparedStatement" level="WARN"/>
<logger name="com.company.demo" level="INFO"/>
<logger name="com.company.demo.dmr.mapper" level="INFO"/>
<logger name="RocketmqClient" level="WARN"/>
<logger name="RocketmqCommon" level="WARN"/>
<logger name="RocketmqRemoting" level="WARN"/>
<logger name="org.springframework.boot.actuate.endpoint.web.servlet" level="WARN"/>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="CatAppender"/>
<!--
<appender-ref ref="ERROR"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
-->
</root>
</springProfile>
</configuration>
请求/返回参数日志
提供基于@LogPoint注解方法打印和RequstMapping等spring注解的方法对请求返回参数进行打印。
��配置如下:
#方法上添加LogPoint注解
xy.log.type=annotation
#拦截restful请求各种Mapping进行拦截
xy.log.type=mapping
xy.log.annotation.enable=true
#指定方url不打印请求入参数据
xy.log.annotation.skip-request=/public/sentinelTest0,/public/sentinelTest1
#指定url不打印请求返回数据
xy.log.annotation.skip-response=/public/sentinelTest0,/public/sentinelTest1
xy.log.mapping.enable=true
#指定方url不打印请求入参数据
xy.log.mapping.skip-request=/ss
#指定url不打印请求返回数据
xy.log.mapping.skip-response=/ss
日志格式
[请求时间]-[服务名]-[线程ID]-[日志级别]-[当前日志输出的Java对象]-[traceId]-[userId]-[日志类容]
taceId在整个请求链路同步处理中唯一且相同。
对于异步线程处理,若需要记录traceId,需要手动从主线程中通过MDC.get(“traceId”)获取并传递给异步线程。