java 使用 SL4J、Jakarta Commons 日志、第三方库的 log4j 和我自己的代码进行日志记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/462566/
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
Logging using SL4J, Jakarta Commons logging, log4j for third party libraries and my own code
提问by
I have some questions about logging, more specifically about setting it up and making sure it works.
我有一些关于日志记录的问题,更具体地说是关于设置它并确保它正常工作。
The project I'm doing will use Wicket, Springand Hibernate. I know that Wicket and Hibernate uses Simple Logging Facade for Java (SL4J) and that Spring is using the logging component from Apache Commons.
我正在做的项目将使用Wicket、Spring和Hibernate。我知道 Wicket 和 Hibernate 使用 Simple Logging Facade for Java ( SL4J) 并且 Spring 使用来自 Apache Commons的日志组件。
Will they co-exist happily? I thought I would use log4jtogether with both SL4J and the logging component from Apache commons, do you think that's a good idea?
他们会幸福地共存吗?我想我会将log4j与 SL4J 和来自 Apache commons 的日志组件一起使用,您认为这是个好主意吗?
Can I set up them all to output logging data into a common file? Or should I use separate files? Or should I store the logging messages in the database? (I'd rather not, as I find grepping etc on text files quite convenient.)
我可以将它们全部设置为将日志数据输出到一个公共文件中吗?还是应该使用单独的文件?还是应该将日志消息存储在数据库中?(我宁愿不这样做,因为我发现对文本文件进行 grepping 等操作非常方便。)
For Spring I guess I need some kind of configuration file for the Apache Commons logging component as well where I direct it to use log4j?
对于 Spring,我想我需要某种用于 Apache Commons 日志记录组件的配置文件,以及在其中指导它使用 log4j 的地方?
When I've set these up I guess to see that everything works I set the logging level to INFO as it's fairly certain that all three of the frameworks output some information in that mode? Or is there an even better way to make sure?
当我设置完这些后,我想会看到一切正常,我将日志记录级别设置为 INFO,因为可以肯定所有三个框架都在该模式下输出一些信息?或者有没有更好的方法来确定?
And my last question. In the project I'm starting, do you recommend that I use SL4J for my own logging purposes? (I thought I would use log4j directly, but that was before I learned a little bit more about logging and a lot of respectable libraries seem to choose the path of a bridge/facade for their logging needs. And if it gets us flexibility without added cost there's no reason not to do it that way.)
还有我的最后一个问题。在我开始的项目中,您是否建议我将 SL4J 用于我自己的日志记录目的?(我以为我会直接使用 log4j,但那是在我了解更多关于日志记录之前,许多受人尊敬的库似乎选择了桥/门面的路径来满足他们的日志记录需求。如果它让我们在不添加的情况下获得灵活性成本没有理由不这样做。)
I'm looking forward to hearing more from you about how you are doing your logging. It's a new area for me which I'm eager to improve myself in.
我期待听到更多关于您如何进行日志记录的信息。这对我来说是一个新领域,我渴望在其中提高自己。
采纳答案by Loki
Well SLF4J is just a facade, like commons logging, which means they still need something else to work. They permit library authors to not force users into having multiple logging library and configuration. Log4j and logback are regular logging libs.
好吧,SLF4J 只是一个外观,就像公共日志记录一样,这意味着他们仍然需要其他东西才能工作。它们允许库作者不强迫用户拥有多个日志库和配置。Log4j 和 logback 是常规日志库。
See herefor more info.
请参阅此处了解更多信息。
SLF4J has a commons logging bridgethat you can use to replace the commons logging library. I think the schema there explain the situation very well.
SLF4J 有一个公共日志桥接器,您可以使用它来替换公共日志库。我认为那里的模式很好地解释了这种情况。
Now, you just need to use slf4j-logj12.jar to have commons logging and slf4j use log4j (or anything else you chose; btw, logback doesn't need an additional library to be used with slf4j) as a backing engine.
现在,您只需要使用 slf4j-logj12.jar 来进行公共日志记录,而 slf4j 使用 log4j(或您选择的任何其他东西;顺便说一句,logback 不需要与 slf4j 一起使用的额外库)作为支持引擎。
You application will thus have
您的应用程序将因此具有
- jcl104-over-slf4j.jar (to bridge jakarta commons logging to slf4j)
- slf4j.jar (for hibernate and others to use slf4j)
- slf4j-logj12.jar (for slf4j to use log4j as a backend)
- log4j.jar (for your application to use. all config will also be done here)
- jcl104-over-slf4j.jar(将jakarta commons 连接到slf4j)
- slf4j.jar(用于休眠和其他人使用 slf4j)
- slf4j-logj12.jar(slf4j 使用 log4j 作为后端)
- log4j.jar(供您的应用程序使用。所有配置也将在此处完成)
回答by Olivier
Here is how to redirect everything to SLF4J:
以下是将所有内容重定向到 SLF4J 的方法:
remove
commons-logging.jarfrom your classpath. If you are using Maven and have trouble getting rid of commons-logging, see this.put
jcl-over-slf4j.jarin your classpath (it comes in the SLF4J distribution). This is a drop-in replacement that mimics JCL's classes, but calls SLF4J internally. This will take care of Spring, and any other framework that uses JCL.
commons-logging.jar从您的类路径中删除。如果您正在使用 Maven 并且无法摆脱 commons-logging,请参阅此。放入
jcl-over-slf4j.jar您的类路径(它来自 SLF4J 发行版)。这是一个模仿 JCL 类的直接替代品,但在内部调用 SLF4J。这将处理 Spring 和任何其他使用 JCL 的框架。
Connect SLF4J to your favorite backend (Log4J, Logback...) by putting slf4j-xxx.jarin the classpath. Configure the backend to log all categories to one file, and you're done.
通过放入slf4j-xxx.jar类路径将 SLF4J 连接到您最喜欢的后端(Log4J、Logback...)。配置后端以将所有类别记录到一个文件中,您就完成了。
As for using SLF4J in your application, it is not strictly necessary. Libraries like JCL and SLF4J were originally designed for people who write libraries and do no want to lock their clients into a particular logging framework.
至于在您的应用程序中使用 SLF4J,并不是绝对必要的。像 JCL 和 SLF4J 这样的库最初是为编写库并且不想将其客户端锁定在特定日志记录框架中的人设计的。
PS: by the way, JCL = Jakarta Commons Logging
PS:顺便说一下,JCL = Jakarta Commons Logging

