Disabling logging on PDFBox

2019-04-05 15:22发布

问题:

We are using PDFBox to do some PDF reading and manipulations. But during the parsing, I get a bunch of messages like this one:

Changing font on <m> from <Arial Bold> to the default font

Now how can I disable these? Because a message like this is output on EVERY character of the input if the font is embedded and the log files therefore become pretty unusable.

Now changing the overall log level is not an option, because I need the statements from other components.

I am using Tomcat 5.5, log4j 1.2.16 and pdfbox-app 1.6.0

And here is my log4j config file:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=/home/PDF/WS/PDF.log
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p [%c{2}]: %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p [%c{2}]: %m%n

EDIT

After modifying my log4j file, this is how it looks:

# Root logger option
log4j.rootLogger=INFO, file, stdout

log4j.rootLogger.org.apache.pdfbox=ERROR

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=/home/PDF/WS/PDF.log
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p [%c{2}]: %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p [%c{2}]: %m%n

No matter where I put the log4j.rootLogger.org.apache.pdfbox=ERROR line, errors still keep popping up like this in the log files:

2012-07-16 15:36:46,652 WARN  [font.PDSimpleFont]: Changing font on <r> from <Arial Bold> to the default font
2012-07-16 15:36:46,652 WARN  [font.PDSimpleFont]: Changing font on <o> from <Arial Bold> to the default font
2012-07-16 15:36:46,667 WARN  [font.PDSimpleFont]: Changing font on <c> from <Arial Bold> to the default font
2012-07-16 15:36:46,667 WARN  [font.PDSimpleFont]: Changing font on <e> from <Arial Bold> to the default font
2012-07-16 15:36:46,667 WARN  [font.PDSimpleFont]: Changing font on <s> from <Arial Bold> to the default font
2012-07-16 15:36:46,667 WARN  [font.PDSimpleFont]: Changing font on <u> from <Arial Bold> to the default font
2012-07-16 15:36:46,667 WARN  [font.PDSimpleFont]: Changing font on < > from <Arial Bold> to the default font
2012-07-16 15:36:46,667 WARN  [font.PDSimpleFont]: Changing font on <P> from <Arial Bold> to the default font

EDIT 2

After consulting log4j: package-specific logging I discovered the right syntax:

log4j.logger.org.apache.pdfbox=ERROR

回答1:

pdfbox does alot logging on error like you stated before, if you add the following line to the log4j.properties it should clean thing up

log4j.rootLogger.org.apache.pdfbox.pdmodel.font.PDFont=fatal


回答2:

Simple way to disable all logging:

java.util.logging.Logger
    .getLogger("org.apache.pdfbox").setLevel(java.util.logging.Level.OFF);

Or if you want to only see the severe messages:

java.util.logging.Logger
    .getLogger("org.apache.pdfbox").setLevel(java.util.logging.Level.SEVERE);


回答3:

This works for me: - please note that there are multiple questions like this were this answer applies and I was already asked to mark things as duplicate. So if someone finds the time for this - just go ahead and merge ...

 // you might want to switch off logging here to improve performance
        String[] loggers = { "org.apache.pdfbox.util.PDFStreamEngine",
            "org.apache.pdfbox.pdmodel.font.PDSimpleFont",
            "org.apache.pdfbox.pdmodel.font.PDFont",
            "org.apache.pdfbox.pdmodel.font.FontManager",
            "org.apache.pdfbox.pdfparser.PDFObjectStreamParser" };
        for (String logger : loggers) {
          org.apache.log4j.Logger logpdfengine = org.apache.log4j.Logger
              .getLogger(logger);
          logpdfengine.setLevel(org.apache.log4j.Level.OFF);
        }