简体   繁体   English

如何禁用 PDFBox 警告日志记录

[英]How to disable PDFBox warn logging

I have a simple java console application.我有一个简单的 java 控制台应用程序。 pdfbox is utilized to extract text from PDF files. pdfbox 用于从 PDF 文件中提取文本。 But there is continuous info printed in console:但是控制台中打印了连续的信息:

十一月 29, 2017 9:28:27 下午 org.apache.pdfbox.pdmodel.font.PDSimpleFont toUnicode
警告: No Unicode mapping for 14 (145) in font GGNHDZ+SimSun  
十一月 29, 2017 9:28:27 下午 org.apache.pdfbox.pdmodel.font.PDSimpleFont toUnicode
警告: No Unicode mapping for 28 (249) in font LNKLJH+SimSun
十一月 29, 2017 9:28:27 下午 org.apache.pdfbox.pdmodel.font.PDSimpleFont toUnicode

I really want to remove this information from the console.我真的很想从控制台中删除这些信息。 And I use logback for logging, the logback.xml is just like:我使用 logback 进行日志记录,logback.xml 就像:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="org.apache.pdfbox" level="ERROR"/>
<timestamp key="timestamp-by-second" datePattern="yyyyMMdd'T'HHmmss"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoder 默认配置为PatternLayoutEncoder -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>logs/test-${timestamp-by-second}.log</file>
    <append>true</append>
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
        </pattern>
    </encoder>
</appender>
<root level="ERROR">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</root>

I have find some answer say that should change the Level.我找到了一些答案说应该改变级别。 I have changed the level to ERROR.我已将级别更改为 ERROR。 But still not work.但还是不行。 I am doubting if the info has something with logback.xml.我怀疑该信息是否与 logback.xml 相关。 Because when I remove STDOUT, the pdfbox warn info still print in the console.因为当我删除 STDOUT 时,pdfbox 警告信息仍会打印在控制台中。

Anybody know this case?有人知道这个案子吗? Thank you in advance.先感谢您。

If the logging was being emitted by Logback then the approach you have tried, for example ...如果日志是由 Logback 发出的,那么您尝试过的方法,例如......

  • Adding <logger name="org.apache.pdfbox" level="ERROR"/>添加<logger name="org.apache.pdfbox" level="ERROR"/>
  • Removing the STDOUT appender删除STDOUT附加程序

... would work. ... 会工作。

However, PDFBox doesn't use Logback, instead it uses Apache Commons Logging ( http://commons.apache.org/logging/ ).但是,PDFBox 不使用 Logback,而是使用 Apache Commons Logging ( http://commons.apache.org/logging/ )。 There are several ways of disabling Commons Logging:有几种方法可以禁用 Commons Logging:

  • Disable Commons Logging entirely by adding the following to your Main class' static initialiser block, this must be executed before PDFBOX creates a Log instance:通过将以下内容添加到 Main 类的静态初始化程序块来完全禁用 Commons Logging,这必须在 PDFBOX 创建Log实例之前执行:

     static { System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); }
  • Disable Commons Logging by passing the following JVM parameter when you start your application:通过在启动应用程序时传递以下 JVM 参数来禁用 Commons Logging:

     -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog`
  • Disable Commons Logging for the PDFBOX namespace by adding the following to your Main class' static initialiser block, this *must** be executed before PDFBOX creates a Log instance (note: you could alternatively use Level.SEVERE , depending on how much tolerance you have for PDFBOX's log output):通过将以下内容添加到 Main 类的静态初始化程序块,禁用 PDFBOX 命名空间的 Commons Logging,这 * 必须** 在 PDFBOX 创建Log实例之前执行(注意:您也可以使用Level.SEVERE ,具体取决于您的容忍度有 PDFBOX 的日志输出):

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

One more option is to add a file called commons-logging.properties to your class path (in maven on src/main/resources , for example) with the contents: commons-logging.properties一种选择是将一个名为commons-logging.properties的文件添加到您的类路径(例如,在src/main/resources上的 maven 中),内容如下:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog

The other options worked for me as well, but this keeps the code a bit cleaner.其他选项也适用于我,但这使代码更简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM