Paflow
Paflow

Reputation: 2377

Disable Apache Commons Logging in Spring project

I have a project with spring web mvc and use Log4J. But one Library (PDFBox / openhtml2pdf) does use Apache Commons Logging. I want to disable it (or better, narrow it down to Server log level). As written here and here I have to put

java.util.logging.Logger.getLogger("org.apache.pdfbox")
.setLevel(java.util.logging.Level.OFF);

somewhere in the main class (main function or static initializer block). Unfortunately I have no idea, what my main class is (do I necessarily have one?). There is no MainClass or start-class or something defined in pom.xml. So where do I have to put line in a Spring Web Mvc project to have it executed before everything starts?

-- Edit:

Another approach I tried is to exclude commons-logging in pom-xml

    <dependency>
        <!-- Required for PDF export. -->
        <groupId>com.openhtmltopdf</groupId>
        <artifactId>openhtmltopdf-pdfbox</artifactId>
        <version>${openhtml-version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

But it did not help.

-- Edit 2:

    <dependency>
        <!-- Required for PDF export. -->
        <groupId>com.openhtmltopdf</groupId>
        <artifactId>openhtmltopdf-pdfbox</artifactId>
        <version>${openhtml-version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>${pdfbox-version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

and

    <openhtml-version>0.0.1-RC12</openhtml-version>
    <pdfbox-version>2.0.8</pdfbox-version>

As we can see here, my version of opnehtml2pdf ships with pdfbox version 2.0.8.

-- Edit 3

I Use Spring Web MVC, not Spring Boot. I Start my application with

mvn tomcat7:run -f pom.xml

but the logging is especially annoying while testing with:

mvn -Dtest=TestClass test

Here are some parts of of my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                    http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>..</groupId>
<artifactId>..</artifactId>
<name>..</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java-version>1.8</java-version>
    <org.springframework-version>4.3.4.RELEASE</org.springframework-version>
    <org.springframework.security-version>4.2.0.RELEASE</org.springframework.security-version>

    ...

    <openhtml-version>0.0.1-RC12</openhtml-version>
    <jsoup-version>1.11.2</jsoup-version>
    <pdfbox-version>2.0.8</pdfbox-version>

</properties>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>but the errors but
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    ...

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>${log4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j-version}</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    ...

    <dependency>
        <!-- Required for PDF export. -->
        <groupId>com.openhtmltopdf</groupId>
        <artifactId>openhtmltopdf-pdfbox</artifactId>
        <version>${openhtml-version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>${pdfbox-version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    ...


</dependencies>


<repositories>

    <!-- maven central repo -->
    <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
    </repository>

    <!-- spring main repo -->
    <repository>
        <id>springsource-repo</id>
        <name>SpringSource Repository</name>
        <url>http://repo.spring.io/release</url>
    </repository>
    <!-- For testing against latest Spring snapshots -->
    <!-- <repository> <id>org.springframework.maven.snapshot</id> <name>Spring 
        Maven Snapshot Repository</name> <url>http://maven.springframework.org/snapshot</url> 
        <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> 
        </snapshots> </repository> -->
    <!-- For developing against latest Spring milestones -->
    <!-- <repository> <id>org.springframework.maven.milestone</id> <name>Spring 
        Maven Milestone Repository</name> <url>http://maven.springframework.org/milestone</url> 
        <snapshots> <enabled>false</enabled> </snapshots> </repository> -->
    <!-- Hibernate -->
    <repository>
        <id>org.hibernate</id>
        <name>Hibernate Repository</name>
        <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <!-- MySQL Connector -->
    <repository>
        <id>mysql</id>
        <name>MySQL Connector</name>
        <url>http://mvnrepository.com/artifact/mysql/mysql-connector-java/</url>
    </repository>
    <!-- jitpack.io for pfdbox-layout -->
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>${maven-war-version}</version>
            <configuration>
                <warName>data</warName>
            </configuration>
            <executions>
                <execution>
                    <id>package</id>
                    <phase>package</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-version}</version>
            <executions>
                <execution>
                    <id>install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>sources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>${aspectj-maven-version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <complianceLevel>${java-version}</complianceLevel>
                <verbose>false</verbose>
                <Xlint>ignore</Xlint>
                <showWeaveInfo>false</showWeaveInfo>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <!-- AspectJ -->
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${org.aspectj-version}</version>
                </dependency>

                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${org.aspectj-version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <!-- same version as failsafe plugin -->
            <version>${maven-failsafe-version}</version>
            <configuration>
                <junitArtifactName>junit:junit</junitArtifactName>
                <includes>
                    <include>**/Test*.java</include>
                </includes>
                <excludes>
                    <exclude>**/ITest*.java</exclude>
                    <exclude>**/*_Roo_*</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-version}</version>
            <configuration>
                <includes>
                    <include>**/ITest*.java</include>
                </includes>
                <excludes>
                    <exclude>**/Test*.java</exclude>
                    <exclude>**/*_Roo_*</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>${maven-tomcat-version}</version>
            <configuration>
                <uriEncoding>UTF-8</uriEncoding>
                <path>/data</path>
            </configuration>
        </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>${maven-javadoc-version}</version>
        </plugin>
    </plugins>
</reporting>
</project>

The warnings I try to get get rid of are like these:

com.openhtmltopdf.css-parse WARNING:: (/#inline_style_1) Found : where ; or } was expected at line 1. Skipping declaration.
com.openhtmltopdf.load INFO:: TIME: parse stylesheets  131ms

-- Edit 3

I also tried with log4j-config, but as mentioned here this can not work:

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

-- Edit 4

Log4J main:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration>

<Appenders>
    <Console name="CONSOLE" target="SYSTEM_OUT">
        <PatternLayout pattern="%-5p[%d{DATE}]: %c - %m%n" />
    </Console>

    <RollingFile name="ROLLING"
                 fileName="...log"
                 filePattern="...log">
        <PatternLayout pattern="%-5p[%d{DATE}]: %c - %m%n" />
        <Policies>
            <SizeBasedTriggeringPolicy size="10240KB" />
        </Policies>
        <DefaultRolloverStrategy max="5" />
    </RollingFile>

    ... other files ...

</Appenders>
<Loggers>
    <logger name="org.springframework.web" level="info" />

    <logger name="de.my.package" level="info" />
    <logger name="de.my.package.controller" level="info" />
    <logger name="de.my.package.service" level="info" />
    <logger name="de.my.package.dao" level="info" />
    <logger name="de.my.package.service.IIPService" level="off" />

    <logger name="org.elasticsearch" level="info" />
    <logger name="com.zaxxer.hikari" level="info" />

    <Logger name="Profiling" additivity="false">
        <AppenderRef ref="..." />
    </Logger>

    <Logger name="DataIntegrityLogger" additivity="false">
        <AppenderRef ref="..." />
    </Logger>

    <Root level="info">
        <AppenderRef ref="ROLLING" />
    </Root>

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

</Loggers>
</Configuration>

log4jsonfig in test

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration>

<Appenders>
    <Console name="CONSOLE" target="SYSTEM_OUT">
        <PatternLayout pattern="%-5p: %c - %m%n" />
    </Console>
</Appenders>



<Loggers>
    <Root level="info">
    </Root>

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

Upvotes: 0

Views: 12261

Answers (3)

Amir Shaikh
Amir Shaikh

Reputation: 207

Remove Apache Commons Logging ......

<dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.24</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Exclude Any Dependencies from It's Parent

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>                                               <!--Exclude Dependencies-->
        <exclusion>
                <groupId>org.springframework.boot</groupId>         <!-- Group ID -->
                <artifactId>spring-boot-starter-logging</artifactId> <!--Name Of Exclude Starter -->
        </exclusion>
    </exclusions>
</dependency>

Exclude Dependencies From Child's Child ......

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>                                   <!-- Exclude dependency from parent  -->
        <exclusion>                                     <!-- Exclude Log4J-->
        <groupId>org.apache.logging.log4j</groupId>         <!-- group name of whose dependency want to exclude-->
        <artifactId>log4j-to-slf4j</artifactId>             <!-- Jar Name-->
        </exclusion>

        <exclusion>                                     <!-- Exclude LogBack-->
            <groupId>ch.qos.logback</groupId>               <!-- group name of whose dependency want to exclude-->
            <artifactId>logback-classic</artifactId>        <!-- Jar Name-->
        </exclusion>
    </exclusions>
</dependency>

Upvotes: 0

LeslieM
LeslieM

Reputation: 2303

I was getting over 2400 DEBUG log lines in a Spring Boot Test from org.apache.commons.beanutils.converters.

For me adding a logback-test.xml file into the resources folder suppressed all these annoying messages.

o.a.c.b.converters.BooleanConverter : Setting default value: false

o.a.c.b.converters.BooleanConverter : Converting 'Boolean' value 'false' to type 'Boolean' o.a.c.b.converters.BooleanConverter : No conversion required, value is already a Boolean o.a.c.b.converters.ByteConverter : Setting default value: 0

etc

Here is the logback-test.xml I used

<?xml version="1.0" encoding="UTF-8"?> 
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml" />
  <logger name="org.springframework" level="DEBUG"/>
  <logger name="org.apache.commons.beanutils.converters" level="OFF"/>
</configuration>

Upvotes: 2

kaliatech
kaliatech

Reputation: 17887

EDIT

Per newer comments and edits to question, you are configuring org.apache.pdfbox logger, however, in example you gave of logging statements that you are trying to disable, you are showing output from logger com.openhtmltopdf. Instead of, or in addition to, the current log4j config, you would need to add <logger name="com.openhtmltopdf" level="ERROR"/>.

ORIGINAL

To find your main class you might try searching recursively for void main( or main (.

But per suggestion from @nathan-hughes in comments, I think would be better to exclude commons-logging.

And then per your more recent comment, I think you need to exclude from the pdfbox library, not the openhtmltopdf-pdfbox library which includes pdfbox, that then transitively includes commons-logging. Exclusions aren't transitive.

<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>pdfbox</artifactId>
  <exclusions>
     <exclusion>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
     </exclusion>
  </exclusions>
</dependency>

Upvotes: 1

Related Questions