Disable PDFBox logging with Springboot org.apache.commons.logging - java

I can't find a way to stop warnings from PDFBox I am using in a psring boot application. For example:
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
I have tried:
In Application file:
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);
}
}
As parameter when running jar:
-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog
-Dorg.slf4j.simpleLogger.defaultLogLevel=off
Within the PDFBox code, the log is set up using:
(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());
I've spent a long time trying a lot of things and trolled through the answers for similar questions in SO but not got anywhere.

This is the configuration file I ended up using. I didn't include any logging releated dependencies or add any exclusions to pdfbox dependency, just added this file to the folder containing the application.properties file.
Filename is logback-spring.xml
The flooding logger was copied from 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"/>

Here's what I have been using in the "old" log4j log4j.properties file (you should migrate to log4j2):
log4j.logger.org.springframework=WARN
log4j.logger.org.apache.pdfbox.pdmodel.font.PDCIDFontType2=FATAL
In the "new" log4j2.xml I have this (the appenders are named STDOUT and A1):
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://logging.apache.org/log4j/2.x/manual/configuration.html
https://logging.apache.org/log4j/2.x/manual/appenders.html
-->
<Configuration>
<Appenders>
....
....
</Appenders>
<Loggers>
<Logger name="org.springframework" level="warn" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="A1"/>
</Logger>
<Logger name="org.apache.pdfbox.pdmodel.font.PDCIDFontType2" level="fatal" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="A1"/>
</Logger>
<Root level="info">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="A1"/>
</Root>
</Loggers>
</Configuration>

I think this also should work for log4j2. I use this simple way of disabling logs from specific libraries in SpringBoot applications. You need just add it into the log4j2.xml file.
<Loggers>
<Root>
****** here is some AppenderRef *******
</Root>
<Logger name="o.a.pdfbox" level="off"/>
or
<Logger name="org.apache.pdfbox" level="off"/>
</Loggers>

Related

How to separate logging by severity to different files using springboot and logback

Im trying to send different logs to different files using logback.
I have 2 appenders configured (Console, RollingFile) and i want all
INFO messages -> Console appender
TRACE messages -> RollingFile appender:
logback-spring.xml
<root level="error">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<logger name="com.mypkg" level="trace" additivity="true">
<appender-ref ref="RollingFile" />
</logger>
<logger name="com.mypkg" level="info" additivity="true">
<appender-ref ref="Console" />
</logger>
The result of the above configuration has 2 problems :
all messages are duplicated (both appenders)
com.mypkg shows only INFO (not TRACE) ob both appenders
any idea what im doing wrong ? is there any default spring logback file the is somehow merged with this config in runtime (changing the additivity to false fix the duplication issue, but still no TRACE messages) ?
Thanks .
You can try logback filters. There is a filter called LevelFilter. The option to accept and ignore log level types is also available here.
Example :
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>
%-4relative [%thread] %-5level %logger{30} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
More information is in the below logback documentation.
https://logback.qos.ch/manual/filters.html#levelFilter

Logback can't write in console

I am using Logback in my spring boot application.
The problem is logback do not print my logger messages in 'eclipse' console for my both 2 packages dao and web.
The log file is written without any problem , and print my logger messages.
I am the root so probably I should see my logger messages in my console.
logger.info("Page X INFO");
logger.debug(" Page X Debug ");
Here is my logback.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<!-- Send debug messages to a file at "C:/logs/Log.log" -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C:/logs/Log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>C:/logs/Log.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
</logger>
<logger name="package.dao" level="DEBUG" >
<appender-ref ref="FILE" />
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
</logger>
You need to add the console appender.
<logger name="package.web" level="INFO" >
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
UPDATE: I just reread the logback config doku. Actually, those two should inherit both appenders from root. So, what you can try is to not specify any appender-ref on those and see what happens. If no output is written to the file then, neither - then there is something pretty strange. I'd expect that behavior if the additivity flag was set to false. But the default is appender accumulation.

Wildfly + Hibernate+ Log4J2 -> Not logging hibernate stuff

So I have a project where I'm using Wildfly 10 and Hibernate 5.2.9 and I'm trying to log the hibernate stuff, such as Queries and so on, using Log4J2, but for some reason it's not working
Log4J2 works when I log application stuff like log.info("SOMETHING")...
I post here my log42j.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Appenders>
<Appender type="FILE" name="FF" fileName="C:/logs/logfile.log">
<Layout type="type" pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | %C{2}
(%F:%L) - %m%n"
</Appender>
</Appenders>
<Loggers>
<Logger name="my.java.package" level="DEBUG">
<AppenderRef ref="FILE"></AppenderRef>
</Logger>
<Logger name="org.hibernate.type" level="TRACE">
<AppenderRef ref="FILE"></AppenderRef>
</Logger>
<Logger name="org.hibernate.SQL" level="TRACE">
<AppenderRef ref="FILE"></AppenderRef>
</Logger>
<Logger name="org.hibernate" level="TRACE">
<AppenderRef ref="FILE"></AppenderRef>
</Logger>
<Root level="ALL">
<AppenderRef ref="FILE"/>
</Root>
</Loggers>
</Configuration>
Any thoughts of how can I solve this?
Thanks!
Since Hibernate is a module provided by the server it uses the servers logging configuration, not the configuration supplied in your deployment.
Really you wouldn't want Hibernate using your log configuration anyway. If you had multiple deployments, each having their own logging configuration there is no guarantee which configuration would win when configuring the logging.

Disable Logger in a .jar java library

How to disable logger (print output) of a java library (jar) used in a java program ?
For example i use the jar joda-time-2.8.2.jar
What should I put in my logback.xml file to disable the console output of the jar
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n</pattern>
</encoder>
</appender>
<logger name="com.paper.white" additivity="false" level="TRACE">
<appender-ref ref="stdout" />
</logger>
<root level="WARN">
<appender-ref ref="stdout" />
</root>
</configuration>
Just create a logger for org.joda.time and set the level to OFF.
<logger name="org.joda.time" level="OFF" >
<appender-ref ref="stdout"/>
</logger>

log4j xml configuration, write some Logger to file and console

I am having following xml config for log4j with 3 appenders. And want to configure logger the following way:
All messages more than INFO are written to STDOUT and global log FILE appenders
But for some class Oauth I want to have extra log file OAUTHFILE
Some classes are filtered and write messages to LOG only on error level this can be achieved by
<logger name="application" level="ERROR" />
some classes are filtered and write messages to its own log file. This is also solved with additivity equal false flag.
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILE" />
</logger>
But how can I solve the 2nd problem. The folowing configuration produces debug and error messages in STDOUT and! in OAUTHFILE
<configuration>
...
<appender name="STDOUT" ...></appender>
<appender name="FILE" ...>
</appender>
<appender name="OAUTHFILE" ...>
</appender>
...
<logger name="controllers.OAuth" level="DEBUG" additivity="false">
<appender-ref ref="OAUTHFILE" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Actually I have found the solution for second task. I should make 2 STDOUT and FILE appenders - one for general logging and the second one with filtering
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
<appender name="STDOUTERR" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %coloredLevel %cyan(%logger{15}) %message%n%xException{5}</pattern>
</encoder>
</appender>
And use the second as extra appender-ref for classes that should produce error log to general stdout like here:
<logger name="MapActor" level="DEBUG" additivity="false">
<appender-ref ref="MAPACTORFILEAPPENDER" />
<appender-ref ref="STDOUTERR" />
</logger>

Categories