1、使用logback+Slf4j日志输出到控制台
1.1、导入logback、Slf4j依赖包
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
1.2、在Resources目录下增加logback.xml文件
设置日志级别:info
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <logger name="org.springframework" level="info" additivity="false"> <appender-ref ref="STDOUT" /> </logger> <root level="info"> <appender-ref ref="STDOUT" /> </root> </configuration>
1.3、在class上加上@Slf4j
通过log.info打印日志(支持info、warn、error等)
@Slf4j @Controller @RequestMapping("/user") public class UserController { @PostMapping("/register") @ResponseBody public User register(@RequestBody User user){ log.info("用户:{} , 密码:{} , 生日:{}" , user.getUserName() , user.getPassword() , user.getBirthday()); System.out.println(user.getBirthday()); return user; } }
1.4、打印的效果
2023-05-04 09:51:59 INFO c.h.s.controller.UserController - 用户:zhangsan , 密码:xxxxxxxx , 生日:2023-05-05