0

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>
Paflow
  • 2,030
  • 3
  • 30
  • 50
  • 1
    Do Spring projects have a `main` class? I thought they executed a set of listeners at start up. `ApplicationContextListener` or something like that. – markspace Jan 07 '19 at 17:56
  • 1
    Good point. I just assumed OP was using something like spring boot or similar, in which there would be a main in app source. If they are running within a container, like tomcat, then there is whole other set of possibilities. Until OP provides more info (i.e. log4j config, pom.xml, console output), I'm not sure what else can be suggested. – kaliatech Jan 07 '19 at 20:53
  • Hi @markspace, you're right there is no main class apparently. I don't use spring boot, but spring WebMvc only. I will investigate in the direction of ApplicationContextListener then. – Paflow Jan 08 '19 at 10:00
  • Regarding your "Edit 3", the linked q/a does not mention use of SLF4J. Per your pom.xml, you _are_ using `slf4j` and `log4j-slf4j-impl`, and so the log4j config _would_ be relevant. Use of commons-logging, log4j, and tomcat 6 is very complex (search those terms here on SO), and it is possible the pdf library is initializing/logging before any custom config comes in to play. However, that would not be true for your `mvn test` example since in that case there is no tomcat. – kaliatech Jan 09 '19 at 13:24
  • @kaliatech Hm okay, I added log4j-config. – Paflow Jan 11 '19 at 13:33
  • As I said in last comment on my answer then, it looks like you are configuring the wrong logger. I edited my answer with that restated. – kaliatech Jan 11 '19 at 15:51
  • Added but still doesn't help. I think I give up, it's not that crucial... – Paflow Jan 11 '19 at 20:18

3 Answers3

1

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>
kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • 1
    It's not clear what you are doing and how you are starting your application. But if possible, look for commons-logging.jar in your /lib directory. If it's still there, then you might try deleting the file from your lib directory before starting just to be sure that is the problem. Separately, I assume you know you also have to configure log4j to disable the relevant logger. You might consider posting more information in your question (log4j config, output you are seeing, etc.). – kaliatech Jan 07 '19 at 17:02
  • Hi @kaliatech, I added a lot of information to the main post. The log4j-config seems not to be relevant (or connected) as it is pointed out in the first thread I linked. I tried to use it anyway for disabling apache logger but without success. – Paflow Jan 08 '19 at 09:59
  • You should post the log4j config you are trying to use and where you are putting it. In your first post you show that you are trying to disable ‘org.apache.pdfbox’ logger, but example outputs you gave are showing ‘com.openhtmlpdf’. You might also try running something like ‘mvn dependency:tree -Dincludes=commons-logging:commons-logging’ to see if something else is pulling in commons-logging. – kaliatech Jan 08 '19 at 14:45
0

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>
LeslieM
  • 2,105
  • 1
  • 17
  • 8
0

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>
Amir Shaikh
  • 159
  • 1
  • 4