使用 Springboot org.apache.commons.logging

我找不到一种方法来阻止我在 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 中类似问题的答案,但一无所获。


叮当猫咪
浏览 85回答 2
2回答

有只小跳蛙

这是我最终使用的配置文件。我没有包含任何与日志记录相关的依赖项,也没有添加任何排除到 pdfbox 依赖项,只是将此文件添加到包含 application.properties 文件的文件夹中。文件名是 logback-spring.xml洪水记录器是从如何更改java中第3方库的日志级别复制的<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"/>

largeQ

这是我在“旧”log4j 文件中使用的内容log4j.properties(您应该迁移到 log4j2):log4j.logger.org.springframework=WARNlog4j.logger.org.apache.pdfbox.pdmodel.font.PDCIDFontType2=FATAL在“新”中log4j2.xml我有这个(附加器被命名为STDOUT和A1):<?xml version="1.0" encoding="UTF-8"?><!-- https://logging.apache.org/log4j/2.x/manual/configuration.html&nbsp;&nbsp; &nbsp; &nbsp;https://logging.apache.org/log4j/2.x/manual/appenders.html--><Configuration>&nbsp; &nbsp; <Appenders>&nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; </Appenders>&nbsp; &nbsp; <Loggers>&nbsp; &nbsp; &nbsp; &nbsp; <Logger name="org.springframework" level="warn" additivity="false">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="STDOUT"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="A1"/>&nbsp; &nbsp; &nbsp; &nbsp; </Logger>&nbsp; &nbsp; &nbsp; &nbsp; <Logger name="org.apache.pdfbox.pdmodel.font.PDCIDFontType2" level="fatal" additivity="false">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="STDOUT"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="A1"/>&nbsp; &nbsp; &nbsp; &nbsp; </Logger>&nbsp; &nbsp; &nbsp; &nbsp; <Root level="info">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="STDOUT"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="A1"/>&nbsp; &nbsp; &nbsp; &nbsp; </Root>&nbsp; &nbsp; </Loggers></Configuration>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java