Log4j2 and Spring 4

你。 提交于 2019-12-04 07:02:38

I have several Spring applications that log using Log4j 2. The recommended method, as you say is to leave the commons-logging jar in and use log4j-jcl to bridge it to Log4j 2. In that scenario the jcl-over-slf4j jar must not be present. In this case the logging calls will be commons-logging -> log4j-jcl -> log4j-api.

As you have said, the other way is to remove the commons-logging jar and replace it with jcl-over-slf4j. You would then need all the jars you have commented out. In this case the logging calls would be jcl-over-slf4j -> slf4j-api -> log4j-slf4j-impl -> log4j-api.

As you can see the second path is a bit longer.

BTW - Your log4j2 configuration is missing the wrapping <configuration>element.

https://www.slf4j.org/legacy.html

As you can see, you need add jcl-over-slf4j.jar and exclude commons-logging.jar (both has identical file structure inside, check it) After that, include slf4j-api.jar, bridge slf4j-log4j.jar and actual logging implementation - log4j.jar

Check for commons-logging.jar in resulted war file, it shouldn't present.

Make sure JCL is implemented by Log4j2, not by bridged to other logging frameworks, avoid dependencies like "org.slf4j:jcl-over-slf4j".

To enforce this (i.e. to fail-fast if wrong dependency is added to a project ever again) you can add a rule for the maven-enforcer-plugin:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
        <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <bannedDependencies>
                        <excludes>
                            <!-- Prohibit routing Apache Commons Logging API via SLF4J, use more direct route to Log4j2. -->
                            <exclude>org.slf4j:jcl-over-slf4j</exclude>
                            <!-- Prohibit routing "java.util.logging" API via SLF4J, use more direct route to Log4j2. -->
                            <exclude>org.slf4j:jul-to-slf4j</exclude>
                            <!-- Prohibit routing log4j-1.2 via SLF4J, use more direct route to Log4j2. -->
                            <exclude>org.slf4j:log4j-over-slf4j</exclude>
                        </excludes>
                    </bannedDependencies>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

As rgoers correctly said in his reply, if we choose to use log4j-jcl bridge, we MUST exclude every dependency on jcl-over-slf4j. In my case it was brought in by spring-data-jpa. The following is my final pom with that exclusion. I can finally see spring and spring security logs.

The next step will be try to bring in correctly slf4j, in order to obtain logs also for spring-data-jpa and thymeleaf, for example.

Thanks again to rgoers who pointed out the solution.

<properties>
    <spring.version>4.3.4.RELEASE</spring.version>
    <spring.security.version>4.2.0.RELEASE</spring.security.version>
    <spring.data.jpa.version>1.7.2.RELEASE</spring.data.jpa.version>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <log4j.version>2.3</log4j.version>
    <hibernate.validator.version>5.3.2.Final</hibernate.validator.version>
    <hibernate.entitymanager.version>4.2.21.Final</hibernate.entitymanager.version> <!-- LAST VERSION SUPPORTING JPA 2.0 (JPA 2.1 is not supported by WebLogic 12.1.1) -->
    <javax.transaction.version>1.1</javax.transaction.version>
    <oracle.jdbc.version>12.1.0.2</oracle.jdbc.version>
    <querydsl.jpa.version>3.6.9</querydsl.jpa.version>
    <google.guava.version>18.0</google.guava.version>
    <quartz.version>2.2.1</quartz.version>
    <pdfbox.version>2.0.4</pdfbox.version>
</properties>

<dependencies>

    <!-- SPRING SECURITY -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <!-- WEB AND VALIDATION -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>

    <!-- THYMELEAF -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>${thymeleaf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>${thymeleaf.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.mxab.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-data-attribute</artifactId>
        <version>2.0.1</version>
    </dependency>

    <!-- LOGGING -->

    <!-- Using JCL -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.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-jcl</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <!-- Using SLF4J -->
    <!--
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.22</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.22</version>
    </dependency>
    &lt;!&ndash;<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>&ndash;&gt;

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    &lt;!&ndash;<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j.version}</version>
    </dependency>&ndash;&gt;
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    -->

    <!-- PERSISTENCE LIBRARIES -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.entitymanager.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>${javax.transaction.version}</version>
    </dependency>

    <!-- SPRING FRAMEWORK COMPONENTS -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring.data.jpa.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- ORACLE JDBC -->
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>${oracle.jdbc.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
        <!--<exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>-->
    </dependency>

    <!-- SPRING-DATA JPA DATATABLES -->
    <dependency>
        <groupId>com.github.darrachequesne</groupId>
        <artifactId>spring-data-jpa-datatables</artifactId>
        <version>3.0</version>
    </dependency>

    <!-- QUERYDSL -->
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl.jpa.version}</version>
    </dependency>

    <!-- GOOGLE GUAVA UTILS -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${google.guava.version}</version>
    </dependency>

    <!-- APACHE PDF BOX -->
    <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>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>fontbox</artifactId>
        <version>${pdfbox.version}</version>
        <!--<exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>-->
    </dependency>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox-tools</artifactId>
        <version>${pdfbox.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.1</version>
    </dependency>

    <!-- ESAPI security features (prevent sql injection during password change -->
    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>2.1.0.1</version>
        <exclusions>
            <!--<exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>-->
            <!--<exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>-->
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>${quartz.version}</version>
    </dependency>


</dependencies>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!