本文介绍了如果我们将它与slf4j api一起使用,我们可以使用log4j2的所有功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我们已经迁移了所有代码以使用slf4 API来使用通用API,但是现在我们正在考虑从log4j 1.x升级到log4j 2.x.如果我们使用slf4j API和log4j2作为实现,我们能否使用log4j2的所有功能吗?We have migrated all our code to use the slf4 API to use generic APIs, however now we are thinking of upgrading from log4j 1.x to log4j 2.x. Will we be able to use all the features of log4j2 if we use the slf4j API and log4j2 as the implementation?推荐答案 Log4j2 API比SLF4J API更丰富,许多Log4j2 API功能不可通过SLF4J访问。请参阅下文了解详情。 The Log4j2 API is richer than the SLF4J API, and many Log4j2 API features are not accessible via SLF4J. See below for details. Log4j2实现的功能(如异步记录器,查找,过滤器,布局和附加程序)由配置控制,无论您在应用程序中使用何种日志API,都可以使用。 Features of the Log4j2 implementation, like Async Loggers, Lookups, Filters, Layouts and Appenders are controlled with configuration and are available regardless of the logging API you use in your application. 请参阅此回答不同但相关的问题为什么可以安全地编程到Log4j2 API 。 Please also see this answer to the different but related question of why it is safe to program to the Log4j2 API. 10 SLF4J中没有Log4j2 API功能 (1) Message API 允许应用程序除了文本之外还记录结构化对象。在内部,Log4j2将记录到消息的所有内容转换为API,并将其暴露给API,为应用程序与下游日志记录组件(过滤器,布局,追加器)进行交互提供了各种可能性。如果您将自定义组件开发为Log4j2的插件,以及使用内置组件时,这可能很有用。有关内置示例,请参阅 StructuredDataMessage 用于对 Rfc5424Layout 进行细粒度控制。 (1) The Message API allows applications to log structured objects in addition to just text. Internally Log4j2 converts everything that is logged to a Message, and exposing this to the API opens up all kinds of possibilities for applications to interact with downstream logging components (filters, layouts, appenders). This can be useful if you're developing custom components as plugins to Log4j2, as well as when you're using the built-in ones. For a built-in example, see how StructuredDataMessage is used for fine-grained control over Rfc5424Layout. (2)Java 8 lambda支持允许您懒惰地创建参数或日志消息,而无需显式检查是否启用了请求的日志级别。 (2) Java 8 lambda support allows you to lazily create parameters or log messages without explicitly checking if the requested log level is enabled. // Java-8 style optimization: no need to explicitly check the log level:// the lambda expression is not evaluated if the TRACE level is not enabledlogger.trace("Some long-running operation returned {}", () -> expensiveOperation());(3)使用String :: format %混合{} -style参数s%d - 样式参数。 {}样式具有更好的性能,可以与任何参数类型一起使用,但 printf 样式可以对格式进行细粒度控制。 Log4j2允许您轻松混合这些参数样式。例如:(3) Mixing {}-style parameters with String::format %s %d-style parameters. The {} style has better performance and can be used with any parameter type, but the printf style gives fine grained control over the formatting. Log4j2 allows you to easily mix these parameter styles. For example:logger.debug("Opening connection to {}...", someDataSource);logger.printf(Level.INFO, "Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());(4) CloseableThreadContext 为SLF4J中的普通ThreadContext(MDC)提供了一些额外的便利:它会在您完成后自动删除项目。例如:(4) CloseableThreadContext offers some extra convenience over the normal ThreadContext (MDC) in SLF4J: it automatically removes items when you're done. For example:// Add to the ThreadContext map for this try block only;try (final CloseableThreadContext.Instance ctc = CloseableThreadContext .put("id", UUID.randomUUID().toString()) .put("loginId", session.getAttribute("loginId"))) { logger.debug("Message 1"); // call some other code that also does logging ... logger.debug("Message 2"); ...} // "id" and "loginId" are now removed from the ThreadContext map(5)Log4j2的 ThreadContext ,除键值对外,还有 push 和 pop 支持堆栈功能的方法(以前在Log4j 1中称为NDC)。(5) Log4j2's ThreadContext, in addition to key-value pairs, also has push and pop methods to support stack functionality (what used to be called NDC in Log4j 1).(6)SLF4J不支持FATAL日志级别。 (6) SLF4J does not support the FATAL log level. (7)Log4j2支持自定义日志级别。这些方法可以与 log 方法一起使用,例如: logger.log(Level.getLevel(FINE),... msg ),或者您可以使用自定义日志级别的便捷方法生成自定义记录器包装器。 (7) Log4j2 has support for custom log levels. These can be used with the log methods, for example: logger.log(Level.getLevel("FINE"), "... msg"), or you can generate a custom logger wrapper with convenience methods for your custom log levels. (8)Log4j2 API接受任何Object,而不仅仅是字符串。这是允许Log4j2无垃圾,意味着它将避免分配新对象。如果它是Number,CharSequence或实现(Log4j2)StringBuilderFormattable接口,则会记录您的Object而不创建任何临时字符串。 (8) The Log4j2 API accepts any Object, not just Strings. This is one of the things that allow Log4j2 to be "garbage-free", meaning it will avoid allocating new Objects. Your Object is logged without creating any temporary Strings if it is a Number, a CharSequence or when it implements the (Log4j2) StringBuilderFormattable interface. 如果记录10个或更少的参数,Log4j2 API也将避免创建vararg数组。如果您记录的参数超过2个,SLF4J会创建vararg数组。 The Log4j2 API will also avoid creating vararg arrays if you log 10 parameters or less. SLF4J creates vararg arrays if you log more than 2 parameters. (9)以上是直接使用Log4j2 API免费获得的。最重要的是,如果你真的关心避免创建临时对象(比如一些交互式游戏和低延迟金融应用程序),你可以避免使用 Unbox 实用程序类。 (9) The above you get for free just by using the Log4j2 API directly. On top of that, if you really care about avoiding creating temporary objects (like some interactive games and low-latency financial applications do), you can avoid auto-boxing primitive parameters with the Unbox utility class. (10)SLF4J标记使用粗粒度同步可能会对多线程应用程序产生性能影响( SLF4J-240 )。请参阅此性能测试结果页面的高级过滤部分。 (10) SLF4J Markers' use of coarse-grained synchronization may have performance impact for multi-threaded applications (SLF4J-240). See the Advanced Filtering section of this performance test results page. 免责声明:我为Log4j2做出贡献。 这篇关于如果我们将它与slf4j api一起使用,我们可以使用log4j2的所有功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 04:29