调整 apache commons 日志记录的日志记录级别?

发布于 2024-10-17 09:52:24 字数 586 浏览 6 评论 0原文

我有一个简单的控制台应用程序,它使用 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 技术交流群。

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

发布评论

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

评论(4

蓝眼泪 2024-10-24 09:52:24

Commons-logging 只是一个日志记录外观,这意味着它不提供实际将日志数据写入磁盘等的代码。您需要更改的是实际日志记录实现的配置(例如 logbacklog4j 等)。如果没有找到这样的库,则默认为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 to java.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.

岁月如刀 2024-10-24 09:52:24

这对我有用:

String[] loggers = { "org.apache.pdfbox.util.PDFStreamEngine",
        "org.apache.pdfbox.pdmodel.font.PDSimpleFont" };
for (String logger : loggers) {
  org.apache.log4j.Logger logpdfengine = org.apache.log4j.Logger
         .getLogger(logger);
  logpdfengine.setLevel(org.apache.log4j.Level.OFF);
}

This works for me:

String[] loggers = { "org.apache.pdfbox.util.PDFStreamEngine",
        "org.apache.pdfbox.pdmodel.font.PDSimpleFont" };
for (String logger : loggers) {
  org.apache.log4j.Logger logpdfengine = org.apache.log4j.Logger
         .getLogger(logger);
  logpdfengine.setLevel(org.apache.log4j.Level.OFF);
}
勿挽旧人 2024-10-24 09:52:24

如果您没有时间弄清楚 Apache commons-logging 下面使用的是哪个 Logging 实现,请尝试禁用类路径中的所有日志实现。以下代码测试了三种实现。

如果任何行无法编译,请勿添加依赖项!只需删除该线即可。

String[] loggers = { "org.apache.pdfbox.util.PDFStreamEngine",
    "org.apache.pdfbox.pdmodel.font.PDSimpleFont", "httpclient.wire.header" , "httpclient.wire.content"
for (String ln : names) {
  // Try java.util.logging as backend
  java.util.logging.Logger.getLogger(ln).setLevel(java.util.logging.Level.WARNING);

  // Try Log4J as backend
  org.apache.log4j.Logger.getLogger(ln).setLevel(org.apache.log4j.Level.WARN);

  // Try another backend
  Log4JLoggerFactory.getInstance().getLogger(ln).setLevel(java.util.logging.Level.WARNING);
 }

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.

String[] loggers = { "org.apache.pdfbox.util.PDFStreamEngine",
    "org.apache.pdfbox.pdmodel.font.PDSimpleFont", "httpclient.wire.header" , "httpclient.wire.content"
for (String ln : names) {
  // Try java.util.logging as backend
  java.util.logging.Logger.getLogger(ln).setLevel(java.util.logging.Level.WARNING);

  // Try Log4J as backend
  org.apache.log4j.Logger.getLogger(ln).setLevel(org.apache.log4j.Level.WARN);

  // Try another backend
  Log4JLoggerFactory.getInstance().getLogger(ln).setLevel(java.util.logging.Level.WARNING);
 }
撩人痒 2024-10-24 09:52:24

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.

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