欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

简单讲解 JDK 日志(jdk-logging)、Log4j 及 Logback:无需深入源码也能理解的日志框架介绍

最编程 2024-02-23 22:52:58
...
  • 获取底层使用的LoggerContextFactory:

    同样LogManager的类加载会去寻找log4j-api定义的LoggerContextFactory接口的底层实现,获取方式有三种:

    • 第一种: 尝试从jar中寻找log4j2.component.properties文件,如果配置了log4j2.loggerContextFactory则使用该LoggerContextFactory
    • 第二种:如果没找到,尝试从jar包中寻找META-INF/log4j-provider.properties文件,如log4j-core-2.2中就有该文件,如下图所示: log4j-core提供的LoggerContextFactory

    如果找到多个,取优先级最高的(该文件中指定了LoggerContextFactory,同时指定了优先级FactoryPriority),如log4j-core-2.2中log4j-provider.properties的文件内容如下:

    LoggerContextFactory = org.apache.logging.log4j.core.impl.Log4jContextFactory
    Log4jAPIVersion = 2.1.0
    FactoryPriority= 10
    
    • 第三种情况:上述方式还没找到,就使用默认的SimpleLoggerContextFactory
  • 使用LoggerContextFactory获取LoggerContext

  • 根据LoggerContext获取Logger

    以log4j-core为例:

    • 会首先判断LoggerContext是否被初始化过了,没有则进行初始化
    • 获取ConfigurationFactory,从配置中获取和插件中获取(log4j-core核心包中有三个YamlConfigurationFactory、JsonConfigurationFactory、XmlConfigurationFactory)
    • 以上文的案例中,会使用XmlConfigurationFactory来加载log4j2.xml配置文件
    • LoggerContext初始化后,就可以获取或者创建Logger了

##4.4 主要对象总结

  • LogManager: 它的类加载会去寻找LoggerContextFactory接口的底层实现,会从jar包中的配置文件中寻找,如上面所述

  • LoggerContextFactory : 用于创建LoggerContext,不同的日志实现系统会有不同的实现,如log4j-core中的实现为Log4jContextFactory

  • PropertyConfigurator: 用于解析log4j.properties文件

  • LoggerContext : 它包含了配置信息,并能创建log4j-api定义的Logger接口实例,并缓存这些实例

  • ConfigurationFactory:上述LoggerContext解析配置文件,需要用到ConfigurationFactory,目前有三个YamlConfigurationFactory、JsonConfigurationFactory、XmlConfigurationFactory,分别解析yuml json xml形式的配置文件

#5 logback

##5.1 使用案例

###5.1.1 需要的jar包

  • logback-core
  • logback-classic
  • slf4j-api

对应的maven依赖为:

<dependency> 
	<groupId>ch.qos.logback</groupId> 
	<artifactId>logback-core</artifactId> 
	<version>1.1.3</version> 
</dependency> 
<dependency> 
    <groupId>ch.qos.logback</groupId> 
    <artifactId>logback-classic</artifactId> 
    <version>1.1.3</version> 
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.12</version>
</dependency>

###5.1.2 使用方式

private static final Logger logger=LoggerFactory.getLogger(LogbackTest.class);

public static void main(String[] args){
	if(logger.isDebugEnabled()){
		logger.debug("slf4j-logback debug message");
	}
	if(logger.isInfoEnabled()){
		logger.debug("slf4j-logback info message");
	}
	if(logger.isTraceEnabled()){
		logger.debug("slf4j-logback trace message");
	}
	
	LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    StatusPrinter.print(lc);
}

补充:

  • 官方使用方式,其实就和slf4j集成了起来

    上述的Logger、LoggerFactory都是slf4j自己的接口与类

  • 没有配置文件的情况下,使用的是默认配置。搜寻配置文件的过程如下:

    1. Logback tries to find a file called logback.groovy in the classpath.
    1. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
    1. If no such file is found, it checks for the file logback.xml in the classpath..
    1. If no such file is found, and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of com.qos.logback.classic.spi.Configurator. The first implementation found will be used. See ServiceLoader documentation for more details.
    1. If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

    The fourth and last step is meant to provide a default (but very basic) logging functionality in the absence of a configuration file.

也可以在类路径下加上一个类似如下的logback.xml的配置文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">          
    <appender-ref ref="STDOUT" />
  </root>  
  
</configuration>

logback则会去解析对应的配置文件。

##5.3 使用过程简单分析