25

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?

centic
  • 15,565
  • 9
  • 68
  • 125
user364902
  • 3,146
  • 6
  • 23
  • 23

4 Answers4

10

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.

justarandomguy
  • 331
  • 4
  • 15
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
  • I am using java logging (i.e. I have no other logging library present). And yet it seems to be ignoring the specification of level on the Java.util.logger. – user364902 Feb 15 '11 at 21:32
  • You can with a [properties](http://www.java-tips.org/java-se-tips/java.util.logging/how-to-configure-a-logger-default-values-with-a-properties.html) file, although I generally advice [against](http://java.sys-con.com/node/48541) java.util.logging. – Johan Sjöberg Feb 15 '11 at 21:37
  • 1
    `slf4j` is also a logging facade, not a logging implementation. – William Armiros Apr 29 '20 at 15:21
3

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);
}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
3

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);
 }
alfonx
  • 6,936
  • 2
  • 49
  • 58
2

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I am using java logging (i.e. I have no other logging library present). And yet it seems to be ignoring the specification of level on the Java.util.logger. – user364902 Feb 15 '11 at 21:32