title: springboot03—日志
date: 2019-08-16 14:48:49
categories:
- 后端
tags: - 后端
日志
-
日志分类
- 日志门面:jcl slf4j
- 日志实现:log4j log4j2 logback
-
springboot默认使用的SLF4j+logback实现
-
开发的时候,日志记录的方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里的的方法,给系统里面导入slf4j的jar和logback的实现jar
-
示例 import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloWorld { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(HelloWorld.class); logger.info("Hello World"); } }
-
每一个日志的实现框架都有自己的配置文件,使用slf4j以后,配置文件还是做成日志实现框架自己本身的文件
-
如何让系统中所有的日志都统一到slf4j:
- 将系统中其他日志框架先排除出去
- 用中间包来替换原有的日志框架
- 导入slf4j其他的实现
-
-
通过这个图片我们可以看到spring boot给所有的日志都进行了偷梁换柱,最终使用的它底层的方式进行日志输出
-
如果我们使用其他引入的框架,一定要把这个框架的日志依赖移除掉,commons-logging,springboot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架时候,解除默认框架的依赖即可
-
测试代码
package com.atguigu.springboot; 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; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot03LoggingApplicationTests { //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test public void contextLoads() { //System.out.println(); //日志的级别; //由低到高 trace<debug<info<warn<error //可以调整输出的日志级别;日志就只会在这个级别以以后的高级别生效 logger.trace("这是trace日志..."); logger.debug("这是debug日志..."); //SpringBoot默认给我们使用的是info级别的,没有指定级别的就用SpringBoot默认规定的级别;root级别 logger.info("这是info日志..."); logger.warn("这是warn日志..."); logger.error("这是error日志..."); } }
日志使用
-
默认配置文件
logging.level.com.atguigu=trace #spring.profiles.active=dev #logging.path= # 不指定路径在当前项目下生成springboot.log日志 # 可以指定完整的路径; #logging.file=G:/springboot.log,logging.file的作用大于logging.path起的作用 # 在当前磁盘的根路径下创建spring文件夹和里面的log文件夹;使用?spring.log 作为默认文件 logging.path=/spring/log # 在控制台输出的日志的格式 logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n # 指定文件中日志输出的格式 logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
-
指定配置文件,给类路径下放上每个日志框架自己的配置文件即可;springboot就不使用他默认配置了
图中这些会覆盖spring自己的配置
logback.xml:直接就配日志框架识别
logback-spring.xml:日志框架就不直接加载日志的配置项目,由springboot解析日志配置,可以使用spring boot高级功能,否则报错
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev, staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
如果错误:控制台显示noapplicableactionfor[springProfile]
-
假如切换使用log4j
-
切换到log4j2
<!-- 忽略自带的日志框架. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- log4j. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <version>1.5.18.RELEASE</version> </dependency>