Log4j2 + Spring MVC5 configuration

Keywords: Spring xml log4j Maven

Article directory

demand

Want to use Log4j2's dynamic loading configuration file feature, build a spring MVC project casually, try the effect

Trial process

pom.xml file

The pom.xml file is as follows
spring uses version 5.0.1.RELEASE
slf4j2 version 2.11.0

<?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">
    <artifactId>springmvc</artifactId>
    <groupId>cn.com.learnlog</groupId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <springframework.version>5.0.1.RELEASE</springframework.version>
        <!--The following two are springAOP Need to use -->
        <log4j2-version>2.11.0</log4j2-version>
        <spring.version>5.0.1.RELEASE</spring.version>
        <slf4j.version>1.7.26</slf4j.version>
        <log4j.version>2.11.2</log4j.version>
        <swagger.version>2.8.0</swagger.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- pring IOC Basic implementation, including access to configuration files, creation and management bean etc. -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
            <scope>runtime</scope>
        </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>
        <!--For and sfl4j Bridge maintenance-->
        <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-web</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                </configuration>
            </plugin>
            <!-- build-helper-maven-plugin, Setting up multiple source folders -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>

                                <source>src/main/java</source>
                                <!-- We can add more than one here. source Node to add any number of source folders -->
                            </sources>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>


    </build>
</project>

Configuration of log4j2 in web.xml

Baidu copied a set of ways to configure Log4j2 in web.xml

	<!-- Log record -->
	<context-param>
		<param-name>log4jConfiguration</param-name>
		<param-value>classpath:log4j2.xml</param-value>
	</context-param>
	<!--Dynamic modification log4j2.xml: Containers scan every 60 seconds log4j Configuration file-->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>

log4j2 file path

log4j2.xml file path: src main resources log4j2.xml

Test whether the log4j2.xml modification takes effect during project operation

Running configuration is effective, no problem, try to modify log4j2.xml file to test the effect of dynamic modification, because the configuration is checked once in 60 seconds, so wait a minute. Discover no effect. Question mark face, start Baidu, Google trip.
The reasons are as follows:
Spring 4.21 + starts, and the configuration file location and refresh time cycle in web.xml are marked as expired.
In addition:
Also note whether the version of the servlet used by webapp is 2.5 and 3.0 or above.
There are also tomcat versions less than or equal to 7.0.43 because the log4j*.jar package has not been loaded due to performance problems (additional configuration files need to be modified)!! (Recommended higher version than 7.0.43 or tomcat 8 and later versions, this problem has been fixed)
Details can be referred to. Official website

Solve the problem

  1. In view of Spring 5 used in this project, two sections of log4j2 configuration in web.xml have been deleted directly (that is, the log4j2 configuration file in web.xml posted above has been deleted altogether). Because the new version is based on servlet 3, log4j2.xml under the project classpath can be automatically identified as a configuration file or log4j2-xxxxx.xml file (not tested).
  2. Then modify the log4j2.xml configuration file, and finally add the monitor Interval= "10" configuration (note that the 10 units of configuration here are seconds!!!), every 10 seconds to detect changes in the configuration file.
<?xml version="1.0" encoding="UTF-8"?>
<!-- status=debug You can check it. log4j Assembly process -->
<configuration status="off" monitorInterval="10">
...
</configuration>

Successful retest

Restart the project, modify the configuration in log4j2.xml (I tested to modify the log output path), 10 seconds later, the regeneration log will be written to the log path just modified.

Posted by morleypotter on Mon, 22 Apr 2019 18:51:35 -0700