揭秘,门面日志如何发现具体日志组件

commons-logging

commons-logging是apache提供的一个通用的日志接口,是为了不和具体的日志方案直接耦合的一种实现。经过commons-logging用户能够本身选择log4j或者jdk自带的logging做为具体实现。html

使用commons-logging的代码以下java

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MyApplication {
  private Log log = LogFactory.getLog(this.getClass());
}

复制代码

Log是一个接口,LogFactory的内部回去装载具体的日志系统,并得到实现该Log接口的实现类,其具体流程以下apache

  1. 经过系统属性org.apache.commons.logging.LogFactory加载LogFactory的实现
  2. 若是未找到,则经过服务发现机制(SPI)在META-INF/services/org.apache.commons.logging.LogFactory寻找实现类进行加载
  3. 若是未找到,则从classpath下寻找commons-logging.properties文件,根据文件中org.apache.commons.logging.LogFactory配置进行加载
  4. 若是仍未找到,则使用默认配置:若是找到Log4j 则默认使用log4j 实现,若是仍没有则使用JDK14Logger 实现,再没有则使用commons-logging 内部提供的SimpleLog 实现

因此只要你引入了log4j的jar包以及对其进行了配置底层就会直接使用log4j来进行日志输出了,其实质就是在org.apache.commons.logging.impl.Log4JLogger(commons-logging包)的getLogger方法调用了log4j的Logger.getLogger来返回底层的Logger,当记录日志的时候就会经过这个Logger写日志。bash

Slf4j

slf4j全称为Simple Logging Facade for JAVA,java简单日志门面。其使用方式以下框架

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyApplication {
  private Logger logger = LoggerFactory.getLogger(this.getClass());
}

复制代码

slf4j和commons-logging不同的是,若是你没有具体的日志组件(logback,log4j等),它是没法打印日志的,若是你在一个maven项目里面只引入slf4j的jar包,而后记录日志maven

public class Main {

  public static Logger logger = LoggerFactory.getLogger(Main.class);

  public static void main(String[] args) {
      logger.info("slf4j");
      System.out.println("done");
  }

}


复制代码

就会出现下面的信息this

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
复制代码

若是你此时引入log4j或者logback的jar包就会打印出日志。spa

经过LoggerFactory.getLogger()查看其实现原理,其加载Logger核心源码以下:日志

static Set<URL> findPossibleStaticLoggerBinderPathSet() {
  
  Set<URL> staticLoggerBinderPathSet = new LinkedHashSet<URL>();
  try {
      ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();
      Enumeration<URL> paths;
      if (loggerFactoryClassLoader == null) {
          paths = ClassLoader.getSystemResources("org/slf4j/impl/StaticLoggerBinder.class");
      } else {
          paths = loggerFactoryClassLoader.getResources("org/slf4j/impl/StaticLoggerBinder.class");
      }
      while (paths.hasMoreElements()) {
          URL path = paths.nextElement();
          staticLoggerBinderPathSet.add(path);
      }
  } catch (IOException ioe) {
      Util.report("Error getting resources from path", ioe);
  }
  return staticLoggerBinderPathSet;
}

复制代码

其主要思想就是去classpath下找org/slf4j/impl/StaticLoggerBinder.class,即全部slf4j的实现,不管是log4j仍是logback都有实现类,同窗们能够看下本身项目中的jar包。code

若是引入了多个实现,编译器会在编译的时候选择其中一个实现类进行绑定,也会将具体绑定的哪一个日志框架告诉你

private static void reportActualBinding(Set<URL> binderPathSet) {
  
    if (binderPathSet != null && binderPathSet.size() > 1) {
        Util.report("Actual binding is of type [" + StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr() + "]");
    }
}

复制代码
相关文章
相关标签/搜索