java slf4j日志框架_Java日志框架:SLF4J详解

The Simple

Simple Logging Facade for Java (SLF4J)可用作各种日志框架的简单外观或抽象,例如 java.util.logging , logback 和 log4j 。 SLF4J 允许最终用户在

Binding with a logging framework at deployment time

The SLF4J SLF4J bindings , with each binding corresponding to a supported framework.

slf4j-log4j12-1.7.27.jar

Binding for log4j version 1.2 , a wlog4j.jar on your class path.

slf4j-jdk14-1.7.27.jar

Binding for java.util.logging , also referred to as JDK 1.4 logging

slf4j-nop-1.7.27.jar

Binding for NOP, silently discarding all logging.

slf4j-simple-1.7.27.jar

Binding for Simple implementation, which outputs all events to System.err. Only

slf4j-jcl-1.7.27.jar

Binding for Jakarta Commons Logging . This binding will delegate all SLF4J logging to JCL.

logback-classic-1.2.3.jar (requires logback-

There are also SLF4J bindings external to the SLF4J project, class is a direct implementation of SLF4J’s org.slf4j.Logger interface.

To switch logging frameworks, just replace slf4j bindings on your class path. For

要切换日志框架,只需替换类路径上的slf4j绑定。例如,要从java.util.logging切换到log4j,只需将slf4j-jdk14-1.7.27.jar替换为slf4j-log4j12-1.7.27.jar即可。

SLF4J does not rely on any special class loader machinery. In fact, each SLF4J binding is hardwired at compile time to use one and only one specific logging framework. For example, the slf4j-log4j12-1.7.27.jar binding is bound at compile time to use log4j. In your code, in addition to slf4j-api-1.7.27.jar, you simply drop one and only one binding of your choice onto the appropriate class path location. Do not place more than one binding on your class path.

655506f1b1dadf82ce3c60d9a5a9dd3f.png

Consolidate logging via SLF4J(通过SLF4J整合日志记录)

SLF4J caters for this common use-case by providing bridging modules for JCL, java.util.logging and log4j.

SLF4J通过为JCL,java.util.logging和log4j提供桥接模块来满足这种常见用例。

4e663f6bd3d88a0ab92593b379fd90e8.png

Mapped Diagnostic Context (MDC) support

Mapped Diagnostic Context is essentially a MDC data can also be highly helpful in filtering messages or

映射诊断上下文

SLF4J supports MDC , or mapped diagnostic context. If the underlying logging framework offers MDC functionality, then SLF4J will delegate to the underlying framework’s MDC. Note that at this time, only log4j and logback offer MDC functionality. If the underlying framework does not offer MDC , for example java.util.logging , then SLF4J will still store MDC data but the information therein will need to be retrieved by custom user code.

SLF4J支持MDC或映射诊断上下文。 如果底层日志记录框架提供了MDC功能,那么SLF4J将委托给底层框架的MDC。 请注意,目前只有log4j和logback提供MDC功能。 如果底层框架不提供MDC,例如java.util.logging,则SLF4J仍将存储MDC数据,但其中的信息需要由自定义用户代码检索。

56ea8342784e6bb8d5b70dab55f7fa89.png

通过上图可知 SLF4J 能够抽象各种具体日志框架,是由 StaticLoggerBinder 类完成。接下来重点看看 bind() 方法是如何查找 StaticLoggerBinder 类,

private

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值