在PDFBox的禁用日志记录(Disabling logging on PDFBox)

2019-07-30 09:36发布

我们使用PDFBox的做一些PDF阅读和操作。 但分析过程中,我得到了一堆像这样的消息:

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

现在,我怎么可以禁用这些? 因为这样的消息被输出在输入的每个字符是否字体被嵌入,因此,日志文件变得相当不可用。

现在改变总日志级别是不是一种选择,因为我需要从其他组件的语句。

我使用Tomcat 5.5,log4j的1.2.16和PDFBOX,应用1.6.0

这里是我的log4j的配置文件:

# 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

编辑

修改我的log4j的文件后,这是它的外观:

# 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

无论身在何处,我把log4j.rootLogger.org.apache.pdfbox=ERROR行,误差仍保持在日志文件中弹出这样的:

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

编辑2

协商后的log4j:包特定的日志记录 ,我发现了正确的语法:

log4j.logger.org.apache.pdfbox=ERROR

Answer 1:

PDFBOX确实对很多错误日志就像你前面所说的,如果添加以下行log4j.properties它应该清理的东西了

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


Answer 2:

简单的方法来禁用所有日志记录:

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

或者,如果你想只看到严重的消息:

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


Answer 3:

这个工作对我来说: - 请注意,有这样多的问题是这个答案适用,我已经要求以纪念事情重复。 所以,如果有人发现这样做的时候 - 先走一步,合并...

 // 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);
        }


文章来源: Disabling logging on PDFBox