V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tunzao
V2EX  ›  Java

Java 的那些日志框架们

  •  
  •   tunzao · 2020-01-12 00:29:32 +08:00 · 4009 次点击
    这是一个创建于 1559 天前的主题,其中的信息可能已经有所发展或是发生改变。
    7 条回复    2020-01-13 10:03:01 +08:00
    ericgui
        1
    ericgui  
       2020-01-12 08:52:29 +08:00
    阿里有日志的库吗?
    xy2401
        2
    xy2401  
       2020-01-12 10:17:16 +08:00
    内容有点少啊。看个开头发现下面没有了
    hantsy
        3
    hantsy  
       2020-01-12 13:19:46 +08:00
    大部分时间调用的 API 只有 Sf4l,JUL。Backend 比较多的有是 Logback,JUL,也有用过 Log4j,JBoss Logging(JBossr 的一个 Logging 套)。

    Spring 核心由于历史原因,本来依赖外部 JCL。

    Spring 5.0 内部重写那些接口(不需要依赖 JCL 了),将调用 JCL 操作 Delegate 到运行时 Logging,Backend 可以是 sf4l,log4j 1/2,logback 等。
    tunzao
        4
    tunzao  
    OP
       2020-01-12 15:10:26 +08:00
    @xy2401 因为不是要详细介绍各个日志框架,所以没有写的太细,扩展阅读可以看下文末的参考
    tunzao
        5
    tunzao  
    OP
       2020-01-12 15:20:12 +08:00
    @hantsy 如果我没有理解错,其实 spring 5 是把 JCL 替换成了 slf4j,依托 slf4j 已经实现的诸多 adapter,从而实现了对目前常用日志实现框架的支持
    hantsy
        6
    hantsy  
       2020-01-12 21:03:36 +08:00
    @tunzao 不是。为了保持兼容,用 JCL 一样的接口,但自己另外实现了。

    https://github.com/spring-projects/spring-framework/blob/master/spring-jcl/src/main/java/org/apache/commons/logging/LogFactory.java

    其中主要调用 LogAdaptor,看看其源代码就明白了。https://github.com/spring-projects/spring-framework/blob/master/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java

    static {
    if (isPresent(LOG4J_SPI)) {
    if (isPresent(LOG4J_SLF4J_PROVIDER) && isPresent(SLF4J_SPI)) {
    // log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;
    // however, we still prefer Log4j over the plain SLF4J API since
    // the latter does not have location awareness support.
    logApi = LogApi.SLF4J_LAL;
    }
    else {
    // Use Log4j 2.x directly, including location awareness support
    logApi = LogApi.LOG4J;
    }
    }
    else if (isPresent(SLF4J_SPI)) {
    // Full SLF4J SPI including location awareness support
    logApi = LogApi.SLF4J_LAL;
    }
    else if (isPresent(SLF4J_API)) {
    // Minimal SLF4J API without location awareness support
    logApi = LogApi.SLF4J;
    }
    else {
    // java.util.logging as default
    logApi = LogApi.JUL;
    }
    }
    tunzao
        7
    tunzao  
    OP
       2020-01-13 10:03:01 +08:00
    @hantsy 👍
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2488 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:43 · PVG 23:43 · LAX 08:43 · JFK 11:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.