Java Websphere 多个 slf4j logback 绑定解决
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22293550/
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
Websphere Multiple slf4j logback bindings work around
提问by coderatchet
I am running an application on Websphere v8.5.5.0 and am attempting to use logback as my logging framework.
我正在 Websphere v8.5.5.0 上运行一个应用程序,并尝试使用 logback 作为我的日志记录框架。
When i try to start the application, I am greeted with an error similar to this one:
当我尝试启动应用程序时,我遇到了一个类似于以下的错误:
[10/03/14 13:19:00:900 EST] 00000097 SystemErr R SLF4J: Class path contains multiple SLF4J bindings.
[10/03/14 13:19:00:900 EST] 00000097 SystemErr R SLF4J: Found binding in [bundleresource://266.fwk1755217229:1/org/slf4j/impl/StaticLoggerBinder.class]
[10/03/14 13:19:00:900 EST] 00000097 SystemErr R SLF4J: Found binding in [wsjar:file:/C:/Program%20Files%20(x86)/IBM/WebSphere/AppServer_1/profiles/AppSrv01/installedApps/AUSSYDCVTLJ007Node02Cell/myapp.ear/lib/logback-classic-1.1.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[10/03/14 13:19:00:900 EST] 00000097 SystemErr R SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[10/03/14 13:19:01:313 EST] 00000097 SystemErr R SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
As the answerexplains, The ibm libraries contain an implementation of the logback-classic library on the classpath already. I want to stay up to date with the latest logback so was wondering whether someone could show me how to manually choose the binding to use (without using a parent-last classloader!).
正如答案所解释的那样,ibm 库已经在类路径上包含了 logback-classic 库的实现。我想了解最新的 logback,所以想知道是否有人可以告诉我如何手动选择要使用的绑定(不使用父级最后一个类加载器!)。
采纳答案by John DeeBee
Actually the error messages you see in your case is a known bug of IBM WebSphere Application Server v8.5.5.0. The issue is that WAS is using is SLF4j internally but this implementation is supposed to be unknown (and unusable) from applications. Unfortunately, these error messages you see have not been blocked by the WAS framework but you can ignore them (except if you have not well used SLF4j). The good news is that this issue has been fixed in FixPack 3 (WAS 8.5.5.3). The bug is referenced by IBM Support here. So, update your WAS installation. If, after applying the fix, the error messages still occur, then, you probably have to review your SLF4J configuration and usage.
实际上,您在案例中看到的错误消息是 IBM WebSphere Application Server v8.5.5.0 的一个已知错误。问题是 WAS 在内部使用的是 SLF4j,但是这个实现应该是应用程序未知的(并且无法使用)。不幸的是,您看到的这些错误消息并未被 WAS 框架阻止,但您可以忽略它们(除非您没有很好地使用 SLF4j)。好消息是此问题已在 FixPack 3 (WAS 8.5.5.3) 中得到修复。IBM 支持人员在此处引用了该错误。因此,更新您的 WAS 安装。如果在应用修复后,错误消息仍然出现,那么您可能需要检查您的 SLF4J 配置和使用情况。
John
约翰
回答by coderatchet
After reading the documentation, I have resolved that it is not good to declare a dependency on the binding being used.
阅读文档后,我决定声明对正在使用的绑定的依赖是不好的。
here is an excerpt from the documentation
这是文档的摘录
Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose. When you come across an embedded component declaring a compile-time dependency on any SLF4J binding, please take the time to contact the authors of said component/library and kindly ask them to mend their ways.
嵌入式组件(如库或框架)不应声明对任何 SLF4J 绑定的依赖,而应仅依赖于 slf4j-api。当库声明对 SLF4J 绑定的编译时依赖时,它会将绑定强加给最终用户,从而否定 SLF4J 的目的。当您遇到一个嵌入式组件声明对任何 SLF4J 绑定的编译时依赖时,请花时间联系该组件/库的作者,并恳请他们改正。
The only work around here is to accept their version of logback or implement a parent-last classloader. The former seems more inviting to me :P
这里唯一的解决方法是接受他们的 logback 版本或实现父级最后一个类加载器。前者似乎更吸引我:P
回答by FGreg
According to the SLF4J documentation, applications shouldpick which binding they want to use and add it to their classpath:
根据 SLF4J 文档,应用程序应该选择他们想要使用的绑定并将其添加到他们的类路径中:
Declaring project dependencies for logging
Given Maven's transitive dependency rules, for "regular" projects (not libraries or frameworks) declaring logging dependencies can be accomplished with a single dependency declaration.
鉴于 Maven 的传递依赖规则,对于“常规”项目(不是库或框架),声明日志依赖项可以通过单个依赖项声明来完成。
However, it is strongly recommended that libraries or frameworksdo not declare a dependency on a specific binding:
但是,强烈建议库或框架不要声明对特定绑定的依赖:
BASIC RULE Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a transitive dependency on a specific binding, that binding is imposed on the end-user negating the purpose of SLF4J. Note that declaring a non-transitive dependency on a binding, for example for testing, does not affect the end-user.
基本规则 嵌入式组件(如库或框架)不应声明对任何 SLF4J 绑定的依赖,而应仅依赖于 slf4j-api。当库声明对特定绑定的传递依赖时,该绑定被强加给最终用户,否定了 SLF4J 的目的。请注意,声明对绑定的非传递依赖项(例如用于测试)不会影响最终用户。
The problem here has more to do with Logging Separationthan with dependency management. The subject of Logging Separation is discussed at lengthin the logback manual. The suggested "easiest approach" from the logback manual is to actually include both the SLF4J api and logback in your web application and use parent-last class loading:
这里的问题更多地与日志分离而不是依赖管理有关。日志分离的主题在 logback 手册中有详细的讨论。logback 手册中建议的“最简单方法”是在您的 Web 应用程序中实际包含 SLF4J api 和 logback,并使用 parent-last 类加载:
Assuming your container supports child-first class loading, separation of logging can be accomplished by embedding a copy of slf4j and logback jar files in each of your applications. For web-applications, placing slf4j and logback jar files under the WEB-INF/lib directory of the web-application is sufficient to endow each web-application with a separate logging environment.
假设您的容器支持 child-first class 加载,可以通过在每个应用程序中嵌入 slf4j 和 logback jar 文件的副本来实现日志记录的分离。对于 web 应用,将 slf4j 和 logback jar 文件放在 web 应用的 WEB-INF/lib 目录下就足以赋予每个 web 应用一个单独的日志环境。
Basically, Websphere has decided to use SLF4J and logback for container-level logging. This is fine but that decision should not be exposed to applications unless the applications want to participate in the container-managed logging framework.
基本上,Websphere 已决定使用 SLF4J 和 logback 进行容器级日志记录。这很好,但是除非应用程序想要参与容器管理的日志记录框架,否则不应向应用程序公开该决定。
Until Websphere adds the ability to choose whether or not an application should participate in container-managed logging, the only workable solution is to use parent-last class loading and include SLF4J and your chosen binding in the WEB-INF/lib or equivalent directory.
在 Websphere 添加选择应用程序是否应参与容器管理日志记录的功能之前,唯一可行的解决方案是使用父级最后一个类加载并将 SLF4J 和您选择的绑定包含在 WEB-INF/lib 或等效目录中。