gpt4 book ai didi

java - 使用 Springboot org.apache.commons.logging 禁用 PDFBox 日志记录

转载 作者:行者123 更新时间:2023-12-02 09:28:15 26 4
gpt4 key购买 nike

我找不到一种方法来阻止我在 psring 启动应用程序中使用的 PDFBox 发出的警告。例如:

2019-10-01 16:53:51.021  WARN 24564 --- [nio-8443-exec-2] o.a.pdfbox.pdmodel.font.PDType0Font      : No Unicode mapping for CID+4 (4) in font Calibri-Bold

2019-10-01 16:53:51.022 WARN 24564 --- [nio-8443-exec-2] o.a.pdfbox.pdmodel.font.PDCIDFontType2 : Failed to find a character mapping for 4 in Calibri-Bold

2019-10-01 16:53:51.022 WARN 24564 --- [nio-8443-exec-2] o.a.pdfbox.pdmodel.font.PDCIDFontType2 : Failed to find a character mapping for 4 in Calibri-Bold

我尝试过:在应用程序文件中:

static {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("org.apache.pdfbox").setLevel(java.util.logging.Level.OFF);

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",
"o.a.pdfbox.pdmodel.font.PDCIDFontType2",
"org.apache.pdfbox.pdmodel.font.PDCIDFontType2",
"o.a.pdfbox.pdmodel.font.PDType0Font",
"org.apache.pdfbox.pdmodel.font.PDType0Font",
"org.apache.pdfbox.pdmodel.font.PDType1Font"
};
for (String logger: loggers) {
org.apache.log4j.Logger logpdfengine = org.apache.log4j.Logger
.getLogger(logger);
logpdfengine.setLevel(org.apache.log4j.Level.OFF);
}

}

作为运行jar时的参数:

-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog
-Dorg.slf4j.simpleLogger.defaultLogLevel=off

在 PDFBox 代码中,使用以下命令设置日志:

(import org.apache.commons.logging.LogFactory;)

private static final Log LOG = LogFactory.getLog(PDCIDFontType0.class);

LOG.warn("Found PFB but expected embedded CFF font " + fd.getFontName());

我花了很长时间尝试了很多事情,并浏览了 SO 中类似问题的答案,但一无所获。

最佳答案

这是我最终使用的配置文件。我没有包含任何与日志记录相关的依赖项,也没有添加任何排除到 pdfbox 依赖项,只是将此文件添加到包含 application.properties 文件的文件夹中。

文件名是 logback-spring.xml

洪水记录器复制自how to change log levels of 3rd party library in java

<property name="LOGS" value="./logs" />

<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>

<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-logger.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>

<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>

<!-- LOG "com.baeldung*" at TRACE level -->
<logger name="org.apache" level="ERROR" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>

<logger name="flooding logger" level="ERROR" additivity="false">
<appender-ref ref="Console"/>

关于java - 使用 Springboot org.apache.commons.logging 禁用 PDFBox 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58180179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com