关于java:SpringBoot核心③日志

市面上的日志框架;

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>

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理