在pom.xml 中添加
1、连接mysql依赖包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
log4jdbc 打印sql参数包
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<version>1.2</version>
</dependency>
2、properties 的配置文件中配置log4j的驱动
spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy
spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/test?serverTimezone=UTC
3、添加logback配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<contextName>Logback For demo Mobile</contextName>
<!-- 设置log日志存放地址 -->
<!--(改) 单环境设置 -->
<property name="LOG_HOME" value="D:/log" />
<!-- 多环境设置 -->
<!-- <springProfile name="dev">
<property name="LOG_HOME" value="/Users/liuyanzhao/Desktop/temp/log" />
</springProfile>
<springProfile name="prod">
<property name="LOG_HOME" value="/logger/log" />
</springProfile>-->
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder默认配置为PartternLayoutEncoder -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} -%msg%n</pattern>
</encoder>
<target>System.out</target>
</appender>
<!-- 按照每天生成日志文件 -->
<appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名 ,每天保存(侧翻)一次 -->
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>180</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} -%msg%n</pattern>
</encoder>
<!--日志文件最大的大小 -->
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>20MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- (改)过滤器,可以指定哪些包,哪个记录到等级, -->
<!-- 运用的场景比如,你只需要com.demo.controller包下的error日志输出。定义好name="com.demo.controller" level="ERROR" 就行了 -->
<logger name="com" level="ERROR">
<appender-ref ref="ROLLING_FILE" />
</logger>
<!-- 全局,控制台遇到INFO及以上级别就进行输出 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="jdbc.connection" additivity="false" level="FATAL">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<logger name="jdbc.resultset" additivity="false" level="ERROR">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<logger name="jdbc.audit" additivity="false" level="ERROR">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<logger name="jdbc.sqlonly" additivity="false" level="DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<logger name="jdbc.sqltiming" additivity="false" level="ERROR">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
</configuration>