使用slf4j/logback时如何打包webapp?

发布于 2024-10-04 04:10:30 字数 522 浏览 0 评论 0原文

我有很多 Web 应用程序需要构建/打包,我想分享它们的公共库。

关于日志记录,我想将 slf4j-api 与每个应用程序捆绑在一起,但考虑我的容器(当前是 tomcat)提供的实现

为此,我将 logback-classic 和 logback-core 这两个 jar 复制到 $CATALINA 中\lib 目录。

不幸的是,在运行时,slf4j 及其实现之间的绑定失败,并显示以下错误消息:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

我设法使其工作的唯一方法是将 jar 捆绑到 war 文件中。

有什么想法吗?

I have lots of web applications to build/package, and I would like to share their common libraries.

Regarding logging, I would like to bundle slf4j-api with each application, but consider the implementation as provided by my container (which is tomcat currently)

To do so, I copied the two jars, logback-classic and logback-core into $CATALINA\lib directory.

Unfortunately, at runtime, the binding between slf4j and its implementation fails with the following error message:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

The only way I managed to make it work is when bundling the jar in the war files.

Any ideas?

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

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

发布评论

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

评论(3

浅紫色的梦幻 2024-10-11 04:10:31

这不会按预期工作。日志框架使用大量静态变量(也称为全局变量)。

因此,每次加载日志配置时,它都会替换部署到容器的所有应用程序。这通常不是您想要的。

将日志实现与 WAR 捆绑在一起,以便 Web 容器可以确保每个 Web 应用程序获得自己的一组全局变量。

[编辑] 如果您确实想使用相同的日志配置控制所有应用程序,则必须将所有日志类移至容器中。这包括 slf4j。

我强烈建议将日志配置放入 JAR 中并放在那里。或者部署一个虚拟 Web 应用程序,该应用程序只包含尽可能少的代码(这样 Tomcat 将加载它)以及 logback.xml

否则,应用程序的启动顺序将决定当您到了必须更改日志配置的地步。

This will not work as expected. Logging frameworks use a lot of static variables (a.k.a. global variables).

So every time you load a logging config, it will replace it for all applications deployed to your container. This is usually not what you want.

Bundle the log implementation with your WAR so the web container can make sure each web app gets its own set of global variables.

[EDIT] If you really want to control all apps with the same log config, you must move all log classes into the container. This includes slf4j.

I strongly advise to put the log config into a JAR and put it there as well. Or deploy a single dummy web app which just contains as few code as possible (so Tomcat will load it) plus the logback.xml

Otherwise, the start order of applications will determine what the logging will be when you get to a point where the log config has to change.

淡写薰衣草的香 2024-10-11 04:10:31

你尊重吗

将 slf4j-nop.jar、slf4j-simple.jar、slf4j-log4j12.jar、slf4j-jdk14.jar 或 logback-classic.jar 中的一个(且仅一个)放在类路径上应该可以解决问题。

如果您使用例如 hibernate,它可能会在您的类路径中引入 slf4j-log4j12.jar,因此您的类路径中可能有多个 slf4j 实现,

对我来说,它可以与 slf4j 和 log4j 一起使用

did you respect

Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

?

if you use e.g. hibernate it might introduce slf4j-log4j12.jar in your classpath, so there might be more than one slf4j implementation in your classpath

for me it works with slf4j and log4j

你げ笑在眉眼 2024-10-11 04:10:31

我们把 Slf4j & 的所有 jar 都放在重新登录到 Tomcat 的共享/lib 中。然后,我们将全局 logback.xml 放入共享/类中。

但我仍然不知道这是否是一个好的做法。
我们必须按用户管理日志,因此我们设置 MDC 以便使用 SiftingAppender。
我有一个问题正在等待有关此主题的答案:(

We put all jar for Slf4j & Logback into shared/lib of Tomcat. Then, we put a global logback.xml into shared/classes.

But i still don't know if it is a good practice.
We have to manage a log by user, so we set MDC in order to use SiftingAppender.
I have a question which is waiting for an answer on this subject :(

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