关于java:SpringBoot核心③日志

8次阅读

共计 5341 个字符,预计需要花费 14 分钟才能阅读完成。

市面上的日志框架;

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j….

日志门面(日志的形象层) 日志实现
JCL(Jakarta Commons Logging)SLF4j(Simple Logging Facade for Java)jboss-logging Log4j JUL(java.util.logging)Log4j2 Logback

右边选一个门面(形象层)、左边来选一个实现;

日志门面:SLF4J;

日志实现:Logback;
右边选一个门面(形象层)、左边来选一个实现;

SpringBoot:底层是 Spring 框架,Spring 框架默认是用 JCL;

==SpringBoot 选用 SLF4j 和 logback;==

2、SLF4j 应用

1、如何在零碎中应用 SLF4j https://www.slf4j.org

当前开发的时候,日志记录办法的调用,不应该来间接调用日志的实现类,而是调用日志形象层外面的办法;

给零碎外面导入 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");
  }
 }

图示;

`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");
  }
}`Copy to clipboardErrorCopied

每一个日志的实现框架都有本人的配置文件。应用 slf4j 当前, 配置文件还是做成日志实现框架本人自身的配置文件;

遗留问题


我的项目中依赖的框架可能应用不同的日志:

Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx

当我的项目是应用多种日志 API 时,能够对立适配到 SLF4J,两头应用 SLF4J 或者第三方提供的日志适配器适配到 SLF4J,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)、两头替换包

 

@SuppressWarnings("rawtypes")
 public abstract class LogFactory {
 ​
  static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";
 ​
  static LogFactory logFactory = new SLF4JLogFactory();

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 的形式记录日志,引入其余框架的时候,只须要把这个框架依赖的日志框架排除掉即可;==

日志应用;

1、默认配置

SpringBoot 默认帮咱们配置好了日志;

 

 // 记录器
  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 日志...");
 ​
 ​
  }

 

 日志输入格局:%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
 ​
 ​
 #logging.path=
 # 不指定门路在以后我的项目下生成 springboot.log 日志
 # 能够指定残缺的门路;#logging.file=G:/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) my.log 输入日志到 my.log 文件
(none) 指定目录 /var/log 输入到指定目录的 spring.log 文件中

指定配置

给类门路下放上每个日志框架本人的配置文件即可;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>` Copy to clipboardErrorCopied

如:

`<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>
        </layout>
    </appender>`Copy to clipboardErrorCopied

如果应用 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>
正文完
 0