java 将 slf4j 与 log4j2 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25100047/
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-11-02 07:20:12  来源:igfitidea点击:

using slf4j with log4j2

javalogginglog4jslf4jlog4j2

提问by

I have been trying log4j2 recently. Now, I would like to test it with slf4j and wondering if all the functionality in log4j2 (e.g. async logging via disruptor) will be available if I try slf4j with log4j2. I have tried adding the slf4j jar files and log4j2 & disruptor jar files to a test project (I added the log4j-slf4j jar but not completely sure if its only meant for log4j or for log4j2 as well). When I use log4j2, I can log out statements to the log file. When I try slf4j, it does not work (and I'm not sure if its my config or the fact that slf4j is not setup to work with log4js disruptor logging? Any suggestions would be welcome. I will update the post with more info and sample code as well.

我最近一直在尝试 log4j2。现在,我想用 slf4j 测试它,并想知道如果我用 log4j2 尝试 slf4j,是否可以使用 log4j2 中的所有功能(例如,通过干扰器进行异步日志记录)。我曾尝试将 slf4j jar 文件和 log4j2 和干扰器 jar 文件添加到测试项目中(我添加了 log4j-slf4j jar,但不完全确定它是否仅适用于 log4j 或 log4j2)。当我使用 log4j2 时,我可以将语句注销到日志文件中。当我尝试 slf4j 时,它不起作用(我不确定是我的配置还是 slf4j 未设置为使用 log4js 干扰器日志记录的事实?欢迎提供任何建议。我将更新帖子以提供更多信息和示例代码也是如此。

采纳答案by Remko Popma

Yes, async loggers via the disruptor should work whether your app uses the log4j2 api, the slf4j api or the log4j-1.2 api. See the log4j2 FAQ page for which jars to include. You'll need a jar for the slf4j api, and in addition you'll need the log4j-api-2.x, log4j-core-2.x and log4j-slf4j-impl-2.x jars. The same log4j2.xml config file should work.

是的,无论您的应用程序使用 log4j2 api、slf4j api 还是 log4j-1.2 api,通过干扰器的异步记录器都应该可以工作。请参阅 log4j2 FAQ 页面以了解要包含哪些 jar。您需要一个用于 slf4j api 的 jar,此外您还需要 log4j-api-2.x、log4j-core-2.x 和 log4j-slf4j-impl-2.x jar。相同的 log4j2.xml 配置文件应该可以工作。

Update: not all Log4j2 functionality is available via the SLF4J API.

更新:并非所有 Log4j2 功能都可通过 SLF4J API 使用

Update 2: Is it safe to program directly to the Log4j2 API?

更新 2:直接编程到 Log4j2 API 是否安全?