springboot+mybatis-plus的logback+Slf4j⽇志配置及sql打印
第⼀步加⼊如下log包
// mvnrepository/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
// mvnrepository/artifact/org.springframework.boot/spring-boot-starter-logging
compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: '2.3.1.RELEASE'
 在resources下新建l放在resources下springboot可⾃动装配。
如果想⾃定义可在mybatisplus配置中设置如下
# 加载全局的配置⽂件
configLocation: classpath:l
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<!--定义⽇志⽂件的存储地址勿在 LogBack 的配置中使⽤相对路径-->
<property name="LOG_HOME" value="logs" />
<!-- 彩⾊⽇志 -->
<!-- 彩⾊⽇志依赖的渲染类 -->
<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" />
<!-- 彩⾊⽇志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%    <!-- 控制台输出 -->
<appender name="STDOUT" class="ch.ConsoleAppender">
<!--⽇志级别过滤INFO以下-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!--            <level>info</level>-->
<!-- 过滤的级别 -->
<level>debug</level>
</filter>
<encoder class="ch.qos.der.PatternLayoutEncoder">
<!--格式化输出:%d表⽰⽇期,%thread表⽰线程名,%-5level:级别从左显⽰5个字符宽度%msg:⽇志消息,%n是换⾏符-->
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- 按照每天⽣成⽇志⽂件 -->
<appender name="FILE"  class="ch.olling.RollingFileAppender">
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<!--⽇志⽂件输出的⽂件名-->
<FileNamePattern>${LOG_HOME}/promotion.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--⽇志⽂件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.der.PatternLayoutEncoder">
<!--格式化输出:%d表⽰⽇期,%thread表⽰线程名,%-5level:级别从左显⽰5个字符宽度%msg:⽇志消息,%n是换⾏符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<!--⽇志⽂件最⼤的⼤⼩-->
<triggeringPolicy class="ch.olling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
springframework包</appender>
<!-- ⽇志输出级别 -->
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<!--    mapper(sql)⽇志级别,放在yml控制-->
<!--    <logger name="adle.mapper" level="debug" />-->
</configuration>
mapper(sql)⽇志级别
logging:
level:
 使⽤例⼦
@RestController
@RequestMapping("/pay-record")
@Slf4j
public class PayRecordController {
@Autowired
private IPayRecordService service;
/**
*  查询列表
* @return
*/
@GetMapping("/list")
public AjaxResult list(PayRecord payRecord) {
log.info("⽇志测试");
LambdaQueryWrapper<PayRecord> wrapper = new QueryWrapper<PayRecord>().lambda();
if(null != PayId()){
wrapper.eq(PayRecord::PayId());
}
return AjaxResult.success(service.list(wrapper));
}
}
  打印出的log
2020-09-12 18:45:52.093  INFO 11740 --- [          main] o.urrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-12 18:45:52.256  INFO 11740 --- [          main] http11.Http11NioProtocol      : Starting ProtocolHandler ["http-nio-8080"]
2020-09-12 18:45:52.353  INFO 11740 --- [          main] o.s.at.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-12 18:45:52.365  INFO 11740 --- [          main] ample.DemoApplication              : Started DemoApplication in 2.236 seconds (JVM running for 3.306)
2020-09-12 18:45:56.119  INFO 11740 --- [nio-8080-exec-1] C.[Tomcat].[localhost].[/]      : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-12 18:45:56.123  INFO 11740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-09-12 18:45:56.131  INFO 11740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms
2020-09-12 18:45:56.171  INFO 11740 --- [nio-8080-exec-1] roller.PayRecordController    : ⽇志测试
2020-09-12 18:45:56.305  INFO 11740 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource  : {dataSource-1} inited
2020-09-12 18:45:57.588 DEBUG 11740 --- [nio-8080-exec-1] apper.PayRecordMapper.selectList  : ==>  Preparing: SELECT pay_id,receipt_by,dept_name,p
ay_time,remark,del_flag,paid_money,receipt_time,pay_money,pay_idnumber,upd 2020-09-12 18:45:57.620 DEBUG 11740 --- [nio-8080-exec-1] apper.PayRecordMapper.selectList  : ==> Parameters:
2020-09-12 18:45:57.714 DEBUG 11740 --- [nio-8080-exec-1] apper.PayRecordMapper.selectList  : <==      Total: 1
  完美解决sql的打印
⽣产环境只需吧mapper⽇志级别改为info即可。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。