一、日志框架
市场上存在十分多的日志框架:
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j... ...
日志门面 (日志的形象层) | 日志实现 |
---|---|
SLF4j(Simple Logging Facade for Java) | Log4j JUL(java.util.logging)<br/>Log4j2 <br/>Logback |
Spring框架默认是用JCL,SpringBoot的底层是Spring框架。Spring Boot在框架内容部应用JCL,spring-boot-starter-logging采纳了 SLF4j+Logback的模式,Spring Boot也能主动适配(JUL、log4j2、logback) 并简化配置。
二、SLF4j
2.1 应用SLF4j
开发的时候,日志记录办法的调用,不应该来间接调用日志的实现类,而是调用日志形象层外面的办法。
查看官网文档,给零碎外面导入slf4j-api.jar
和 logback的实现jar包。
每一个日志的实现框架都有本人的配置文件。应用slf4j当前,配置文件还是做成日志实现框架本人自身的配置文件。
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"); }}
2.2 常见问题
例如开发应用(slf4j+logback),但还应用了框架 Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx。则须要对立日志记录,即便应用别的框架都对立应用slf4j进行输入。
如何让零碎中所有的日志都对立到slf4j
==1、将零碎中其余日志框架先排除进来==
==2、用两头包来替换原有的日志框架==
==3、导入slf4j其余的实现==
三、SpringBoot日志关系
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
SpringBoot应用它来做日志性能:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
底层依赖关系
总结:
(1)SpringBoot底层也是应用slf4j+logback的形式进行日志记录
(2)SpringBoot也把其余的日志都替换成了slf4j
(3)两头替换包
例如org.apache.log4j.Logger:
void differentiatedLog(Marker marker, String fqcn, int level, Object message, Throwable t) { String m = this.convertToString(message); if (this.locationAwareLogger != null) { this.locationAwareLogger.log(marker, fqcn, level, m, (Object[])null, t); } else { switch(level) { case 0: this.slf4jLogger.trace(marker, m); break; case 10: this.slf4jLogger.debug(marker, m); break; case 20: this.slf4jLogger.info(marker, m); break; case 30: this.slf4jLogger.warn(marker, m); break; case 40: this.slf4jLogger.error(marker, m); } }
(4)如果咱们要引入其余框架,肯定要把这个框架的默认日志依赖移除掉?
Spring框架用的是commons-logging;
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
==SpringBoot能主动适配所有的日志,而且底层应用slf4j+logback的形式记录日志,引入其余框架的时候,只须要把这个框架依赖的日志框架排除掉即可。==
四、日志应用
4.1 默认配置
SpringBoot默认帮咱们配置好了日志:
//记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test public void contextLoads() { //日志的级别 //由高到低 error>warn>info>debug>trace //能够调整输入的日志级别;日志就只会在这个级别及之上的高级别失效 logger.trace("这是trace日志..."); logger.debug("这是debug日志..."); //SpringBoot默认给咱们应用的是info级别的,没有指定级别的就用SpringBoot默认规定的root级别。 logger.info("这是info日志..."); logger.warn("这是warn日志..."); logger.error("这是error日志..."); }
日志输入格局:
%d
——示意日期工夫。%thread
——示意线程名。%-5level
——级别从左显示5个字符宽度。%logger{50}——
示意logger名字最长50个字符,否则依照句点宰割。%msg
——日志音讯。%n
——换行符。
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
SpringBoot批改日志的默认配置:
logging.level.com.atguigu=trace#spring.profiles.active=dev#logging.path=# 不指定门路在以后我的项目下生成springboot.log日志# 能够指定残缺的门路;#logging.file=D:/springboot.log# 在以后磁盘的根门路下创立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
logging.file | logging.path | Example | Description |
---|---|---|---|
(none) | (none) | (none) | 只在控制台输入 |
指定文件名 | (none) | my.log | 输入日志到my.log文件 |
(none) | 指定目录 | /var/log | 输入到指定目录的spring.log文件中 |
4.2 指定配置
给类门路下放上每个日志框架本人的配置文件即可,SpringBoot就不应用他默认配置的了。
Logging System | Customization |
---|---|
Logback | logback-spring.xml , logback-spring.groovy , logback.xml or logback.groovy |
Log4j2 | log4j2-spring.xml or log4j2.xml |
JDK (Java Util Logging) | logging.properties |
logback.xml:间接就被日志框架辨认。
logback-spring.xml:日志框架就不间接加载日志的配置项,由SpringBoot解析日志配置,能够应用SpringBoot的高级Profile性能。
<springProfile name="staging"> <!-- configuration to be enabled when the "staging" profile is active --> 能够指定某段配置只在某个环境下失效</springProfile>
例如:
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <!-- 日志输入格局: %d示意日期工夫, %thread示意线程名, %-5level:级别从左显示5个字符宽度 %logger{50} 示意logger名字最长50个字符,否则依照句点宰割。 %msg:日志音讯, %n是换行符 --> <layout class="ch.qos.logback.classic.PatternLayout"> <springProfile name="dev"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n</pattern> </springProfile> <springProfile name="!dev"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern> </springProfile> </layou> </appender>
如果应用logback.xml作为日志配置文件,还要应用profile性能,会有以下谬误:
no applicable action for [springProfile]
五、切换日志框架
能够依照slf4j的日志适配图,进行相干的切换:
slf4j+log4j的形式:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>logback-classic</artifactId> <groupId>ch.qos.logback</groupId> </exclusion> <exclusion> <artifactId>log4j-over-slf4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency>
切换为log4j2:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-logging</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
参考
视频
SLF4J user manual