1 package com.study.spring_boot_log; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration; 6 import org.springframework.context.ConfigurableApplicationContext; 7 8 import com.study.spring_boot_log.dao.UserDao; 9 import com.study.spring_boot_log.service.UserService;36 @SpringBootApplication(exclude=WebSocketAutoConfiguration.class)37 public class App {38 public static void main(String[] args) {39 ConfigurableApplicationContext context = SpringApplication.run(App.class,args);40 context.getBean(UserDao.class).log();41 System.out.println("===================");42 context.getBean(UserService.class).log();43 44 context.close();45 }46 }
pom.xml:
13 4.0.0 4 5com.study.springboot 6spring-boot-log 71.0.0 8jar 9 10spring-boot-log 11http://maven.apache.org 12 1314 16 17UTF-8 1518 28 2919 2720 26org.springframework.boot 21spring-boot-dependencies 221.5.3.RELEASE 23import 24pom 2530 3531 34org.springframework.boot 32spring-boot-starter 33
1 package com.study.spring_boot_log.dao; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.stereotype.Component; 6 7 @Component 8 public class UserDao { 9 private Logger log = LoggerFactory.getLogger(UserDao.class);10 public void log() {11 log.debug("user dao debug log");12 log.info("user dao info log");13 log.warn("user dao warn log");14 log.error("user dao error log");15 }16 }
1 package com.study.spring_boot_log.service; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.stereotype.Component; 6 7 @Component 8 public class UserService { 9 private Logger log = LoggerFactory.getLogger(UserService.class);10 public void log() {11 log.debug("user service debug log");12 log.info("user service info log");13 log.warn("user service warn log");14 log.error("user service error log");15 }16 }
springboot默认 的日志级别是info
可以通过logging.level.*=debug配置项设置,*可以是包,也可以是某个类。也可以在Run Configurations中配置 --debug,或者SpringApplication.run(App.class,“--debug=true”)。这两种debug只会是springboot默认,自定义的类不会debug。
日志级别有:TRACE,DEBUG,INFO,WARN,ERROR,FATA,OFF日志级别配置成OFF,表示关闭日志输出 logging.file 指定日志文件路径名字logging.path 指定日志目录(此时的日志名字为spring.log)日志文件输出,文件的大小10M之后,就会分割logging.pattern.console 配置控制台输出日志的patternlogging.file.console 配置日志文件输出日志的patternapplication.properties
1 logging.level.com.study.spring_boot_log.dao.UserDao=off2 logging.level.com.study.spring_boot_log.service.UserService=DEBUG3 4 logging.file=D:/ivy/mylog5 logging.path=D:/ivy/mylogs6 7 logging.pattern.console=%-20(%d{yyyy-MM-dd} [%thread]) %-5level %logger{80} - %msg%n8 logging.file.console=%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n
springboot 默认支持logback
也就是说,只需要在classpath下放一个logback.xml,logback-spring.xml的文件,即可定制日志的输出logback-spring.xml或logback.xml:
1 23 4 8 9 105 7%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n 611 1312
使用其他的日志组件的步骤:
1.排除默认的日志组件:spring-boot-starter-logging2.加入新的日志路径依赖3.把相应的配置文件放在classpath下log4j2.xml:
1 23 4 85 76 9 1310 1211