Adjust Logging level for apache commons logging?

前端 未结 4 2023
攒了一身酷
攒了一身酷 2020-12-14 17:54

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 sup

相关标签:
4条回答
  • 2020-12-14 18:03

    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, sl4fj 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.

    0 讨论(0)
  • 2020-12-14 18:06

    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.

    0 讨论(0)
  • 2020-12-14 18:06

    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);
     }
    
    0 讨论(0)
  • 2020-12-14 18:12

    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);
    }
    
    0 讨论(0)
提交回复
热议问题