默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台。在运行应用程序和其他例子时,你应该已经看到很多INFO级别的日志了。
1、添加依赖
maven依赖中添加了spring-boot-starter-logging:
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
|
但是呢,实际开发中我们不需要直接添加该依赖,你会发现spring-boot-starter其中包含了 spring-boot-starter-logging,该依赖内容就是 Spring Boot 默认的日志框架 logback。如果工程中有用到了Thymeleaf,而Thymeleaf依赖包含了spring-boot-starter,最终我只要引入Thymeleaf即可。
2、把日志写入文件
第一种方法:在application.properties 添加 logging.file=”文件路径” 或者 logging.path=”文件路径” 的属性
第二种方法:自定义logback.xml 文件
Spring Boot官方推荐优先使用带有-spring的文件名作为你的日志配置(如使用logback-spring.xml,而不是logback.xml),命名为logback- spring.xml的日志配置文件,spring boot可以为它添加一些spring boot特有的配置项,如下图。
3、我的logback-spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> </appender> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>e:/logs/info.log</file>
<encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>info.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> </appender> <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>e:/logs/error.log</file>
<encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>error.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> <appender-ref ref="file" /> <appender-ref ref="error" /> </root> </configuration>
|
4、java 代码调用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package top.lrshuai.helloword.controller; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.lrshuai.helloword.entity.User; import top.lrshuai.helloword.mapper.UserMapper; @RestController public class UserController {
private final Logger log = Logger.getLogger(this.getClass()); @Autowired private UserMapper userMapper; @RequestMapping("/getAll") public Object getAllList(){ List<User> ulist = userMapper.getAll(); log.info("......"); log.info("......"); log.info("......"); log.info("......"); log.info("......"); log.info("......"); log.info("......"); log.info("......"); log.warn("warn...................."); log.error("error...................."); log.debug("debug...................."); System.out.println("ulist="+ulist); return ulist; } }
|
我的Github 地址: https://github.com/rstyro/spring-boot/tree/master/springboot-log