如何将 java.util.logging 发送到 log4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/869945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:27:33  来源:igfitidea点击:

How to send java.util.logging to log4j?

javalogginglog4japache-commonsslf4j

提问by matt b

I have an existing application which does all of its logging against log4j. We use a number of other libraries that either also use log4j, or log against Commons Logging, which ends up using log4j under the covers in our environment. One of our dependencies even logs against slf4j, which also works fine since it eventually delegates to log4j as well.

我有一个现有的应用程序,它针对 log4j 执行所有日志记录。我们使用了许多其他库,这些库也使用 log4j,或者针对 Commons Logging 进行登录,最终在我们的环境中使用 log4j。我们的依赖项之一甚至针对 slf4j 进行日志记录,这也可以正常工作,因为它最终也委托给 log4j。

Now, I'd like to add ehcache to this application for some caching needs. Previous versions of ehcache used commons-logging, which would have worked perfectly in this scenario, but as of version 1.6-beta1they have removed the dependency on commons-logging and replaced it with java.util.logging instead.

现在,我想将 ehcache 添加到此应用程序中以满足某些缓存需求。ehcache 的早期版本使用了 commons-logging,这在这种情况下可以完美运行,但是从1.6-beta1 版本开始,他们已经删除了对 commons-logging 的依赖,并用 java.util.logging 代替它。

Not really being familiar with the built-in JDK logging available with java.util.logging, is there an easy way to have any log messages sent to JUL logged against log4j, so I can use my existing configuration and set up for any logging coming from ehcache?

不太熟悉 java.util.logging 提供的内置 JDK 日志记录,是否有一种简单的方法可以将发送到 JUL 的任何日志消息针对 log4j 进行记录,以便我可以使用我现有的配置并为即将到来的任何日志记录进行设置从ehcache?

Looking at the javadocs for JUL, it looks like I could set up a bunch of environment variables to change which LogManagerimplementation is used, and perhaps use that to wrap log4j Loggers in the JUL Loggerclass. Is this the correct approach?

查看 JUL 的 javadocs,看起来我可以设置一堆环境变量来更改使用的LogManager实现,并且可能使用它来将 log4j 包装Logger在 JULLogger类中。这是正确的方法吗?

Kind of ironic that a library's use of built-in JDK logging would cause such a headache when (most of) the rest of the world is using 3rd party libraries instead.

具有讽刺意味的是,当(大多数)世界其他地方使用 3rd 方库时,库使用内置 JDK 日志记录会导致如此头痛。

采纳答案by overthink

One approach I have used successfully is to use slf4jas my primary logging API. I then have slf4j bind to log4j. 3rd party dependencies using other frameworks (like JUL) can be bridged to slf4j.

我成功使用的一种方法是使用slf4j作为我的主要日志记录 API。然后我将 slf4j 绑定到 log4j。使用其他框架(如 JUL)的第 3 方依赖项可以接到 slf4j。

回答by araqnid

The slf4j site I believe has a bridge for passing java.util.logging events via slf4j (and hence to log4j).

我相信 slf4j 站点有一个通过 slf4j 传递 java.util.logging 事件的桥梁(从而传递到 log4j)。

Yes, the SLF4J download contains jul-to-slf4j which I believe does just that. It contains a JUL handler to pass records to SLF4J.

是的,SLF4J 下载包含 jul-to-slf4j,我相信它就是这样做的。它包含一个 JUL 处理程序,用于将记录传递给 SLF4J。

回答by Jim Ferrans

We use SLF4Jon our current project and it's worked very well for us. SLF4J is written by Ceki Gülcü, the creator of Log4J, and he's done a really great job. In our code we use the SLF4J logging APIs directly, and we configure SLF4J so that calls from the Jakarta Commons Logging(JCL), java.util.logging (JUL), and Log4JAPIs are all bridged to the SLF4J APIs. We need to do that because like you we use third party (open source) libraries that have chosen different logging APIs.

我们在当前项目中使用SLF4J,它对我们非常有效。SLF4J 由 Log4J 的创建者 Ceki Gülcü 编写,他做得非常出色。在我们的代码中,我们直接使用 SLF4J 日志 API,并配置 SLF4J,以便来自Jakarta Commons Logging(JCL)、java.util.logging (JUL) 和Log4JAPI 的调用都桥接到 SLF4J API。我们需要这样做,因为像您一样,我们使用选择了不同日志记录 API 的第三方(开源)库。

On the bottom of SLF4J, you configure it to use a particular logger implementation. It comes with an internal, or "simple" logger, and you can override this with Log4J, JUL, or Logback. Configuration is all done simply by dropping in different jar files in your classpath.

在 SLF4J 的底部,您将其配置为使用特定的记录器实现。它带有一个内部的或“简单的”记录器,您可以使用 Log4J、JUL 或Logback覆盖它。只需在类路径中放入不同的 jar 文件即可完成配置。

Originally, we used the Logback implementation, also written by Ceki Gülcü. This is very powerful. However, we then decided to deploy our application to the Glassfish Java EE application server, whose log viewer expects JUL-formatted messages. So today I switched from Logback to JUL, and in just a few minutes I replaced two Logback jars with an SLF4J jar that connects it to the JUL implementation.

最初,我们使用了同样由 Ceki Gülcü 编写的 Logback 实现。这是非常强大的。但是,我们随后决定将我们的应用程序部署到 Glassfish Java EE 应用程序服务器,其日志查看器需要 JUL 格式的消息。所以今天我从 Logback 切换到 JUL,在短短几分钟内,我用一个 SLF4J jar 替换了两个 Logback jar,将它连接到 JUL 实现。

So like @overthink, I would heartily recommend using SLF4J in your setup.

所以就像@overthink 一样,我衷心推荐在您的设置中使用 SLF4J。

回答by Joshua Davis

@Yishai - Thanks for posting the link to my wiki. The example there redirects JUL to Log4J and I've had it running in a production system for a few years. JBoss 5.x already redirects JUL to Log4J, so I took it out when we upgraded. I have a newer one that redirects to SLF4J, which I use on a few things now. I'll post that when I get a chance.

@Yishai - 感谢您将链接发布到我的 wiki。那里的示例将 JUL 重定向到 Log4J,我已经在生产系统中运行了几年。JBoss 5.x 已经把 JUL 重定向到 Log4J,所以升级的时候把它拿出来了。我有一个更新的重定向到 SLF4J,我现在在一些事情上使用它。当我有机会时,我会发布它。

However, SLF4J already has it:

但是,SLF4J 已经有了:

http://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j

http://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j

回答by Emmanuel Bourg

There is a simpler alternative than SLF4J to bridge JUL with log4j, see http://people.apache.org/~psmith/logging.apache.org/sandbox/jul-log4j-bridge/examples.html

有比 SLF4J 更简单的替代方案来桥接 JUL 与 log4j,请参阅http://people.apache.org/~psmith/logging.apache.org/sandbox/jul-log4j-bridge/examples.html

You just have to put the jul-log4j-bridge on the classpath and add a system property:

您只需将 jul-log4j-bridge 放在类路径上并添加系统属性:

-Djava.util.logging.manager=org.apache.logging.julbridge.JULBridgeLogManager

jul-log4j-bridge is not in Maven Central and can be fetched from this repository:

jul-log4j-bridge 不在 Maven Central 中,可以从这个存储库中获取:

<repository>
  <id>psmith</id>
  <url>http://people.apache.org/~psmith/logging.apache.org/repo</url>
  <releases>
    <enabled>false</enabled>
  </releases>
</repository>

and then used with:

然后用于:

<dependency>
  <groupId>org.apache.logging</groupId>
  <artifactId>apache-jul-log4j-bridge</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>log4j</groupId>
      <artifactId>apache-log4j-component</artifactId>
    </exclusion>
  </exclusions>
</dependency>

It's also possible to rebuild it from sources with the following steps:

也可以通过以下步骤从源代码重建它:

  1. svn co http://svn.apache.org/repos/asf/logging/sandbox/jul-to-log4j-bridge/
  2. edit pom.xml, replace the dependency on log4j:log4j:1.2.15 with log4j:apache-log4j-extras:1.2.17 and remove the dependency on apache-log4j-component
  3. mvn package
  1. svn co http://svn.apache.org/repos/asf/logging/sandbox/jul-to-log4j-bridge/
  2. 编辑 pom.xml,将 log4j:log4j:1.2.15 的依赖替换为 log4j:apache-log4j-extras:1.2.17 并移除对 apache-log4j-component 的依赖
  3. mvn 包

回答by AdrianRM

OCTOBER 2014

2014 年 10 月

Since version 2.1 of log4j exists the component log4j-jul, which allows exactly this. Still, in case you are using log4j 1, it has to be possible to upgrade to log4j2 in order to use this approach.

由于 log4j 的 2.1 版存在组件 log4j-jul,它完全允许这样做。尽管如此,如果您使用的是 log4j 1,则必须可以升级到 log4j2 才能使用此方法。

JDK Logging Adapter

JDK 日志适配器

Class LogManager

类日志管理器

Migrate from log4j 1.x to log4j 2

从 log4j 1.x 迁移到 log4j 2

回答by jiahut

you should manually add blew at startup

你应该在启动时手动添加 blew

SLF4JBridgeHandler.removeHandlersForRootLogger()
SLF4JBridgeHandler.install()

demo -> https://gist.github.com/jiahut/654ecc75a13b0a1d8f3b4d5d2d69dc6d

演示 -> https://gist.github.com/jiahut/654ecc75a13b0a1d8f3b4d5d2d69dc6d