springboot日志配置
主要是使用了Lombok中的slf4j,实现了执行SQL语句控制台打印与日志文件输出
1.pom.xml
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.application.yml
mybatis:
type-aliases-package: com.springboottest.demo.entity #实体映射路径
mapper-locations: classpath:mapper/*.xml #mapper.xml文件路径
#在控制台打印执行的SQL语句
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#开启驼峰命名
map-underscore-to-camel-case: true
#日志保存地址必须配置logback.xml
logging:
file:
path: D:/logs/lab.log
#name: 与path大致相同但只可用一个路径自定义可放到项目路径下
3.logback.xml(必须配置)
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<!--控制台输出内容的颜色转换以及格式-->
<substitutionProperty name="logging.pattern.console"
value="%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<!--日志文件输出内容的格式-->
<substitutionProperty name="logging.pattern.file"
value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<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">
<!--控制台使用layout节点-->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
${logging.pattern.console}
</pattern>
</layout>
</appender>
<!--按天生成日志-->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>true</Prudent>
<!--滚动策略,我配置了按天生成日志文件-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--相对路径,生成的文件就在项目根目录下-->
<FileNamePattern>
logs/%d{yyyy-MM}/%d{yyyy-MM-dd}.log
</FileNamePattern>
<!--注意超过365天的日志文件会被删除,即使已经按天分开也会删除-->
<MaxHistory>365</MaxHistory>
</rollingPolicy>
<!--日志文件里只保存ERROR及以上级别的日志-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<!--文件使用encoder节点-->
<encoder>
<Pattern>
${logging.pattern.file}
</Pattern>
</encoder>
</appender>
<!--这个logger里的配置相当于之前yml里的logging.level.com.lpc: trace-->
<!--additivity的作用-->
<!--true,则子Logger不止会在自己的appender里输出,还会在root的logger的appender里输出-->
<!--而这个logger里没配置appender,所以得交给root打印-->
<!--所以com.lpc包里的日志从TRACE级别开始-->
<!--其他包里的日志根据root的配置从INFO级别开始打印-->
<logger name="com.lpc" level="TRACE" additivity="true">
</logger>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
测试代码
@RequestMapping("/test")
public class LoginController {
private static final Logger log= LoggerFactory.getLogger(LoginController.class);
@Autowired
Userservice userservice;
@ResponseBody
@PostMapping("/log")
public Responses login(@RequestBody User user){
log.info("你好test");
return Responses.success(userservice.sel(user));
}
控制台输出效果
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@34b52444] will not be managed by Spring
==> Preparing: select * from student where name =?;
==> Parameters: 李明(String)
<== Columns: id, name, age
<== Row: 1, 李明, 20
<== Total: 1
日志文件效果
2021-04-03 17:17:49.782 INFO 12824 --- [http-nio-80-exec-1] c.s.demo.contorller.LoginController : 你好test