Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable PDFBox warn logging

I have a simple java console application. pdfbox is utilized to extract text from PDF files. 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:

<?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. But still not work. I am doubting if the info has something with logback.xml. Because when I remove STDOUT, the pdfbox warn info still print in the console.

Anybody know this case? Thank you in advance.

like image 954
neal Avatar asked Mar 08 '23 11:03

neal


2 Answers

If the logging was being emitted by Logback then the approach you have tried, for example ...

  • Adding <logger name="org.apache.pdfbox" level="ERROR"/>
  • Removing the STDOUT appender

... would work.

However, PDFBox doesn't use Logback, instead it uses Apache Commons Logging (http://commons.apache.org/logging/). There are several ways of disabling 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:

    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:

    -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):

    java.util.logging.Logger.getLogger("org.apache.pdfbox")
        .setLevel(java.util.logging.Level.OFF);
    
like image 138
glytching Avatar answered Mar 16 '23 12:03

glytching


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:

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.

like image 36
avandeursen Avatar answered Mar 16 '23 13:03

avandeursen