SLF4J,通用日志记录,控制台输出

发布于 2024-10-16 08:26:30 字数 544 浏览 4 评论 0原文

我有这个第三方库,其中包含:

  • slf4j-api-1.5.5.jar
  • slf4j-jdk14-1.5.5.jar
  • jcl-over-slf4j-1.5.5.jar

我想针对该库编写一些测试并查看其日志输出,我不想添加任何更多的日志库(没有 log4j 或其他任何东西)。

我知道 SLF4J 和 Common Logging 都是日志抽象,所以我可能需要编写自己的简单具体记录器(或者可能不需要,因为 jcl-over-slf4j 包括 org.apache.commons.logging.impl.SimpleLog?)。如果是这样,我应该实现哪些接口,更重要的是,如何设置 SL4J/Common Logging 以在测试中使用我的记录器?我在 SLF4J 文档中读到我必须修改 StaticLoggerBinder 类...是这样吗真的意味着我实际上必须下载 SLF4J 源代码,修改类并重新编译它吗?

I have this 3rd party library that has:

  • slf4j-api-1.5.5.jar
  • slf4j-jdk14-1.5.5.jar
  • jcl-over-slf4j-1.5.5.jar

I want to write some tests against this library and see its log output, and I don't want to add any more logging libraries (no log4j or anything else).

I understand that SLF4J and Common Logging are both logging abstractions so I probably need to write my own simple concrete logger (or maybe not, since jcl-over-slf4j includes org.apache.commons.logging.impl.SimpleLog?). If so, what interfaces should I implement, and more importantly, how do I set up SL4J/Common Logging to use my logger in my test? I read in the SLF4J docs that I have to modify the StaticLoggerBinder class... does that really mean that I actually have to download SLF4J sources, modify the class and recompile it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海少女心 2024-10-23 08:26:30

我们使用SLF4J。它非常有用,但有几个罐子的名称令人困惑,并且一开始就不清楚哪些罐子不兼容。

SLF4J 是您用于登录代码的 API(例如 log.info("blah"))。但是,SLF4J 没有配置方面。在运行时,您只需添加一个 em> jar 到将 API 绑定到“真实”日志记录子系统的类路径 如果要使用 Log4J,请添加 slf4j-log4j.jar简单或 JDK,或 Logback ,您可以像平常一样配置这些日志记录实现,而不使用 SLF4J

。 /a> 可将使用 Log4J、Apache Commons Logging 和 java.util.logging 的 API 编写的现有日志记录语句重定向到 SLF4J 这允许您为所有这些分散的实现设置单个日志记录配置。 对于避免配置 Log4J 和 JUL 非常有用。)

(如果您有直接绑定到任何遗留日志框架的库,这 .org/legacy.html" rel="noreferrer">SLF4J 旧版页面 深入解释了这些概念。哎呀,甚至还有一个模块将 Sysout.out/err 重定向到 SFL4J 。

为了更直接地回答您的问题:确保您可以编写自己的日志记录实现来使用SLF4J;但这样做的唯一原因是因为你已经被锁定在一些本土的糟糕的日志框架中。

We use SLF4J. It's very useful but several of the jars have confusing names and it's not real clear starting out to know which ones are incompatible.

SLF4J is the API you use for logging in your code (e.g. log.info("blah"). However, SLF4J has no configuration aspect to it. At runtime you add exactly one jar to the classpath that binds the API to the 'real' logging subsystem. If you want to use Log4J, add slf4j-log4j.jar or the StaticBinder jar for Simple or JDK, or Logback. You configure any of these logging implementations as you normally would without SLF4J.

There are several SLF4J modules available to redirect existing logging statements written using the APIs of Log4J, Apache Commons Logging, and java.util.logging to SLF4J. This allows you to setup a single logging configuration for all these disperate implementations. (This is very useful to avoid configuring both, say, Log4J and JUL if you have libraries that bind directly to any legacy logging framework.)

The SLF4J legacy page explains these concepts in depth. Heck, there is even a module to redirect Sysout.out/err to SFL4J.

To more directly answer your question: sure you can write your own logging implementation to go under SLF4J; but the only reason to do so is because you are already locked into some homegrown craptastic logging framework.

逆流 2024-10-23 08:26:30

如果您想保持简单,请使用内置(从 jdk 1.4 开始)记录器

https://docs.oracle.com/en/java/javase/19/docs/api/java.logging/java/util/logging/ Logger.html

jdk绑定jar是slf4j自带的。您需要确保将 jar 部署到您的 web 应用程序 WEB-INF/lib 目录中,或者部署到您的类路径中。有关更多信息,请参阅 (slf4j.org/faq.html#where_is_binding) 和此 (slf4j.org/faq.html)。

您要添加到类路径中的 jar 是 slf4j-jdk14.jar。请注意,jdk 记录器已经可用,该 jar 是 slf4j 接口和所选记录器实现之间的链接。 jdk 日志记录绑定 jar 随 slf4j 发行版一起提供。这应该适合你。

If you want to keep it simple, use the built-in (as of jdk 1.4) logger

https://docs.oracle.com/en/java/javase/19/docs/api/java.logging/java/util/logging/Logger.html

the jdk binding jar comes with slf4j. you want to make sure the jar is deployed to your webapp WEB-INF/lib dir or just in your classpath otherwise. See (slf4j.org/faq.html#where_is_binding) and this (slf4j.org/faq.html) for more information.

the jar you want to add to your classpath is slf4j-jdk14.jar. Note that the jdk logger is already available, this jar is the link between the slf4j interfaces and the chosen logger implementation. the jdk logging binding jar comes with the slf4j distribution. this should do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文