调整 apache commons 日志记录的日志记录级别?
我有一个简单的控制台应用程序,它使用 apache 的 PDFBox 库,而该库又使用公共日志记录。我的控制台中收到很多垃圾消息,我想将其抑制:
2011 年 2 月 15 日下午 3:56:40 org.apache.pdfbox.util.PDFStreamEngine processOperator 信息:不支持/禁用的操作:EI
在我的代码中,我尝试重置日志级别但无济于事:
Logger.getLogger("org.apache.pdfbox.util.PDFStreamEngine").setLevel(Level.OFF);
Logger.getLogger("org.apache.pdfbox.util").setLevel(Level.OFF);
Logger.getLogger("org.apache.pdfbox").setLevel(Level.OFF);
尽管进行了这些设置,消息仍然显示在控制台上。从 Commons 日志记录中检索日志对象也没有帮助,因为它似乎没有办法设置级别。
有没有办法以编程方式抑制这些消息?或者我需要添加配置文件吗?
I have a simple console app which uses apache's PDFBox library, which in turn uses commons logging. I'm getting a lot of junk messages in my console which I'd like to suppress:
Feb 15, 2011 3:56:40 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: EI
In my code, I've tried to reset the log levels to no avail:
Logger.getLogger("org.apache.pdfbox.util.PDFStreamEngine").setLevel(Level.OFF);
Logger.getLogger("org.apache.pdfbox.util").setLevel(Level.OFF);
Logger.getLogger("org.apache.pdfbox").setLevel(Level.OFF);
Despite these settings, the messages are still showing up on the console. Retrieving the log object from Commons logging doesn't help either, since it doesn't seem to have a way to set the level.
Is there a way to suppress these messages programmatically? Or do I need to add a config file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Commons-logging 只是一个日志记录外观,这意味着它不提供实际将日志数据写入磁盘等的代码。您需要更改的是实际日志记录实现的配置(例如
logback
、log4j
等)。如果没有找到这样的库,则默认为java.util.logging
。我建议将例如 log4j 放在类路径中并添加 log4j.xml 配置文件。在这种情况下,类路径中仅存在
log4j
就足以对其进行初始化。 Log4j 还可以以编程方式进行配置。Commons-logging is only a logging-facade, meaning it doesn't provide the code which actually writes the logdata to e.g., disk. What you need to change is the configuration for the actual logging implementation (such as
logback
,log4j
, etc.). If no such library is found it defaults tojava.util.logging
.I would recommend putting e.g., log4j in the classpath and add a log4j.xml configuration file in your classpath. The mere presence of
log4j
in the classpath is in this case enough to initialize it. Log4j can also be configured programmatically.这对我有用:
This works for me:
如果您没有时间弄清楚 Apache commons-logging 下面使用的是哪个 Logging 实现,请尝试禁用类路径中的所有日志实现。以下代码测试了三种实现。
如果任何行无法编译,请勿添加依赖项!只需删除该线即可。
In case you have no time to figure out which Logging implementation is used underneath by the Apache commons-logging, try to disable all that are in you classpath. Following code tests three implementations.
If any of the lines doesn't compile, do not add the dependency! Just remove the line.
Apache commons-logging 在底层使用一些其他日志记录框架(
java.util.logging
或 Log4J),您必须调查它在您的项目中使用哪一个并在那里设置适当的日志记录级别。Apache commons-logging uses some other logging framework underneath (
java.util.logging
or Log4J), you have to investigate which one it uses on your project and set proper logging levels there.