如何禁用 Tess4j 中的日志记录?

How do you disable logging in Tess4j?

我不熟悉使用 Tess4J 库。我已经将 Tess4j 作为依赖项包含在 Maven 中,如下所示:

    <dependency>
        <groupId>net.sourceforge.tess4j</groupId>
        <artifactId>tess4j</artifactId>
        <version>4.5.1</version>
    </dependency>

到目前为止效果很好,但我不知道如何禁用日志记录。日志似乎来自 PDFBox 和 Fontbox,它们是 Tess4j 的依赖项。以下是此处记录的一些内容:

19:54:46.712 [main] DEBUG org.apache.fontbox.util.autodetect.FontFileFinder - checkFontfile found C:\WINDOWS\FONTS\YuGothR.ttc
19:54:46.757 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded Arial-BoldMT from C:\WINDOWS\FONTS\arialbd.ttf
19:54:46.860 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded Arial-BoldItalicMT from C:\WINDOWS\FONTS\arialbi.ttf
19:54:46.899 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded ArialMT from C:\WINDOWS\FONTS\arial.ttf
19:54:46.955 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded Arial-ItalicMT from C:\WINDOWS\FONTS\ariali.ttf
19:54:47.477 [Finalizer] DEBUG org.apache.pdfbox.io.ScratchFileBuffer - ScratchFileBuffer not closed!

我尝试在调用 doOCR 的代码之前设置日志记录级别,但它似乎仍在记录此信息。这是我的代码片段:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Logger;
import net.sourceforge.tess4j.*;

public static void main(String[] args) throws TesseractException, FileNotFoundException, IOException { 
    Tesseract tesseract = getTesseract("C:\Program Files\Tesseract-OCR\tessdata");

    // My attempts to disable logging
    Logger.getLogger("org.apache.fontbox").setLevel(java.util.logging.Level.OFF);
    Logger.getLogger("org.apache.pdfbox").setLevel(java.util.logging.Level.OFF);

    // Example 1
    File file = new File("test.pdf");
    String result = tesseract.doOCR(file);
    System.out.println(result);
  }

tess4jdependencies include logback.
Therefore I assume the logs are produced by logback, not by standard java logging (java.util.logging package).
It means you should configure logback 禁用不必要的日志记录。
在 Maven 项目中,创建一个包含如下内容的文件 src/main/resources/logback.xml 就足够了:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="org.apache.fontbox" level="ERROR"/>
  <logger name="org.apache.pdfbox" level="ERROR"/>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>

</configuration>