使用log4j作为日志记录工具,配合logback使用。

huangapple go评论67阅读模式
英文:

Using log4j for logging with logback

问题

我想要使用log4j(而不是slf4j)来进行日志记录,同时还想使用logback.jar。有没有什么办法可以做到这一点?我正在开发一个使用log4j的网络服务,我打算同时使用logback.jar。

英文:

I want to use log4j(not slf4j) for logging along with logback.jar. Is there anyway doing that? I am working on a web service that consumes log4j and I am intended to use logback.jar along with that.

答案1

得分: 4

slf4j项目为处理传统日志记录提供了一些“桥接”jar包。根据http://www.slf4j.org/legacy.html#log4j-over-slf4j:

log4j-over-slf4j

SLF4J附带了一个名为log4j-over-slf4j的模块。它允许log4j用户将现有应用迁移到SLF4J,而无需更改任何一行代码,只需通过用log4j-over-slf4j.jar替换log4j.jar文件来实现,如下所述。

它是如何工作的?

log4j-over-slf4j模块包含了大多数广泛使用的log4j类的替代类,即org.apache.log4j.Category、org.apache.log4j.Logger、org.apache.log4j.Priority、org.apache.log4j.Level、org.apache.log4j.MDC和org.apache.log4j.BasicConfigurator。这些替代类将所有工作重定向到它们相应的SLF4J类。

要在您自己的应用程序中使用log4j-over-slf4j,第一步是找到并替换log4j.jar为log4j-over-slf4j.jar。请注意,为了使log4j-over-slf4j正常工作,您仍然需要一个SLF4J绑定及其依赖项。

在大多数情况下,只需替换一个jar文件即可从log4j迁移到SLF4J。

请注意,作为此迁移的结果,将不再加载log4j配置文件。如果您需要将log4j.properties文件迁移到logback,log4j转换器可能会有帮助。有关配置logback,请参阅其手册。

什么情况下不起作用?

当应用程序调用桥接中不存在的log4j组件时,log4j-over-slf4j模块将无法工作。例如,当应用程序代码直接引用log4j appenders、filters或PropertyConfigurator时,log4j-over-slf4j对于log4j来说是一个不足够的替代品。然而,当通过配置文件配置log4j,无论是log4j.properties还是log4j.xml,log4j-over-slf4j模块应该能够正常工作。

在您的情况下,您需要做的是将log4j-over-slf4jlog4j12logback-classiclogback-core JAR包一起添加。您还需要在logback配置中重新实现log4j日志配置文件的相关部分。

英文:

The slf4j project provides some "bridge" jars for dealing with legacy logging. According to http://www.slf4j.org/legacy.html#log4j-over-slf4j:

> log4j-over-slf4j
> ----------------
>
> SLF4J ship with a module called log4j-over-slf4j. It allows log4j users to migrate existing applications to SLF4J without changing a single line of code but simply by replacing the log4j.jar file with log4j-over-slf4j.jar, as described below.
>
> How does it work?
> -----------------
>
> The log4j-over-slf4j module contains replacements of most widely used log4j classes, namely org.apache.log4j.Category, org.apache.log4j.Logger, org.apache.log4j.Priority, org.apache.log4j.Level, org.apache.log4j.MDC, and org.apache.log4j.BasicConfigurator. These replacement classes redirect all work to their corresponding SLF4J classes.
>
> To use log4j-over-slf4j in your own application, the first step is to locate and then to replace log4j.jar with log4j-over-slf4j.jar. Note that you still need an SLF4J binding and its dependencies for log4j-over-slf4j to work properly.
>
> In most situations, replacing a jar file is all it takes in order to migrate from log4j to SLF4J.
>
> Note that as a result of this migration, log4j configuration files will no longer be picked up. If you need to migrate your log4j.properties file to logback, the log4j translator might be of help. For configuring logback, please refer to its manual.
>
> When does it not work?
> -------------------
>
> The log4j-over-slf4j module will not work when the application calls log4j components that are not present in the bridge. For example, when application code directly references log4j appenders, filters or the PropertyConfigurator, then log4j-over-slf4j would be an insufficient replacement for log4j. However, when log4j is configured through a configuration file, be it log4j.properties or log4j.xml, the log4j-over-slf4j module should just work fine.

In your case, what you would need to do is to add log4j-over-slf4j together with the log4j12, logback-classic and logback-core JARs. You would also need to reimplement the relevant parts of the log4j logging config file in the logback configs.

答案2

得分: 0

你可以这样做。在你的情况下,这将是log4j和SLF4j的组合。

SLF4j是一个门面库,它为log4j、logback、java.util.logger等日志库提供了抽象,并且不执行实际的日志记录。

因此,基本上SLF4j充当了各种日志框架的简单门面或抽象,允许最终用户在部署时插入所需的日志框架。

关于实现,你可以参考:https://medium.com/@krishankantsinghal/logback-slf4j-log4j2-understanding-them-and-learn-how-to-use-d33deedd0c46

英文:

You can do that. In your case it will be combination of log4j and SLF4j.
SLF4j is a facade library which provide abstraction for logging libraries like log4j, logback, java.util.logger and don't do actual logging.
So, basically SLF4j serves as a simple facade or abstraction for various logging frameworks allowing the end user to plug in the desired logging framework at deployment time.
<br />For implementation you can refer: https://medium.com/@krishankantsinghal/logback-slf4j-log4j2-understanding-them-and-learn-how-to-use-d33deedd0c46

huangapple
  • 本文由 发表于 2020年8月28日 21:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63634695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定