==============Springboot的日志管理=============
springboot无需引入日志的包,springboot默认已经依赖了slf4j、logback、log4j等日志。我习惯用slf4j,下面就用slf4j做配置。
如果你导入了spring-boot-starter-web,这个会自动依赖上述日志。如下依赖:
0.日志测试类:
package daoTest; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import cn.qlq.MySpringBootApplication; @RunWith(SpringRunner.class) @SpringBootTest(classes = MySpringBootApplication.class) public class PlainTest { private static final Logger logger = LoggerFactory.getLogger(PlainTest.class); @Test public void findAll() { logger.error("error , msg->{} ", "错误"); logger.info("info , msg->{} ", "信息"); } }
1.springboot默认的日志级别是debug
2.如果需要修改日志的相关配置可以修改applications.properties文件
############################################################
#
# 日志相关配置(默认集成的有slf4j,Logback等)
#
############################################################
#指定配置文件的位置,只能是xml或者groovy结尾
#logging.config=classpath:logback.xml
#默认的日志级别
logging.level.root=INFO
# mapper 接口所在的包设置为 debug
logging.level.cn.qlq.mapper=DEBUG
#生成日志文件的位置
logging.file=G:/springboot.log
#生成日志文件的目录,名称默认为spring.log
#logging.path=e:/
#指定日志的格式
#logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %clr(%logger){cyan} %clr(%msg%n){green}
#logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
解释:上面logging.level.root可以指定所以包默认的日志级别,logging.level.cn.qlq.mapper是对单独的子包设定日志级别,其级别可低于上面的root,也可以高于root
logging.file是指定文件日志的输出位置以及名称,logging.path是指定日志文件的位置,默认名称是spring.log(如果两者都配置以logging.file生效)
最后面是指定控制台和输出文件的日志格式。
logging.config是指定配置文件的位置,只能是xml或者groovy结尾。
扫描二维码关注公众号,回复:
5257801 查看本文章
关于日志级别等大致相同,参考:https://www.cnblogs.com/qlqwjy/p/9275415.html
==============Springboot整合Junit测试=============
Springboot中我们也可以像在普通的SSM环境中进行SpringJunit测试。
1.引入测试需要的模块
<!--springboot单元测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2.建立测试目录,一般在src/test/java下面和src/test/resources目录下
mapper文件和配置文件要复制到src/test/resources目录下。
3.建立测试类进行测试
SpringBootTest的classes是springboot项目启动的运行类,也就是带有@SpringBootApplication的类。
package daoTest; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import cn.qlq.MySpringBootApplication; import cn.qlq.bean.User; import cn.qlq.mapper.UserMapper; @RunWith(SpringRunner.class) @SpringBootTest(classes = MySpringBootApplication.class) public class PlainTest { @Autowired private UserMapper userMapper; @Test public void findAll() { List<User> findAll = userMapper.findAll(); System.out.println(findAll); } }