log4j2生效

做时间的好朋友發表於2024-04-16

1.引入依賴

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 <!-- log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!-- 為了支援非同步日誌,新增Log4j2的非同步日誌處理器依賴 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jcl</artifactId>
        </dependency>

2.配置配置檔案

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration scan="true" scanPeriod="120 seconds">
    <properties>
        <property name="log.base" value="${mvn.log.dir}"/>
        <property name="log.STDOUT" value="${mvn.log.STDOUT}"/>
        <property name="log.level" value="${mvn.log.level}"/>
    </properties>

    <Appenders>
        <!-- 定義主日誌檔案 -->
        <RollingFile name="MainLog" fileName="${log.base}/main.log"
                     filePattern="${log.base}/$${date:yyyy-MM}/main-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50MB" />
            </Policies>
            <DefaultRolloverStrategy max="20"/>
            <Filters>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>

        <!-- 定義錯誤日誌檔案 -->
        <RollingFile name="ErrorLog" fileName="${log.base}/error.log"
                     filePattern="${log.base}/$${date:yyyy-MM}/error-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50MB" />
            </Policies>
            <DefaultRolloverStrategy max="20"/>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>

        <!-- 定義其他日誌檔案 -->
        <RollingFile name="RootLog" fileName="${log.base}/root.log"
                     filePattern="logs/$${date:yyyy-MM}/root-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>

        <!-- 非同步配置 -->
        <Async name="AsyncMainLog">
            <AppenderRef ref="MainLog"/>
        </Async>
        <Async name="AsyncErrorLog">
            <AppenderRef ref="ErrorLog"/>
        </Async>
        <Async name="AsyncRootLog">
            <AppenderRef ref="RootLog"/>
        </Async>

        <!-- 控制檯輸出 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="AsyncRootLog"/>
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="com.jdl.sys.app" level="info" additivity="false">
            <AppenderRef ref="AsyncMainLog"/>
        </Logger>
        <Logger name="om.jdl.sys.app.error" level="error" additivity="false">
            <AppenderRef ref="AsyncErrorLog"/>
        </Logger>
    </Loggers>
</Configuration>

3.構建配置,如果沒有新增構建配置,log4j不一定能生效

3.1 配置構建中profile環境下的構建引數

<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <mvn.log.dir>./target/logs</mvn.log.dir>
                <mvn.log.STDOUT>true</mvn.log.STDOUT>
                <mvn.log.level>INFO</mvn.log.level>
            </properties>
        </profile>
    </profiles>

3.2 構建外掛

maven-resource-plugin這個最重要,一定要加上,加上log4j2.xml中的變數才會生效

<build>
        <plugins>
            <!-- spring-boot自帶的打包外掛,設定mainClass方便本地啟動類main方法啟動的時候,可以動態匹配到profile -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jdl.sys.app.main.AppMainApplication</mainClass>
                    <layout>DIR</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 資原始檔打包需要關注,log4j2.xml中可以正確引導到相關變數 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters><!--  這是重點-->
                </configuration>
            </plugin>
        </plugins>


        <!-- 新增這段配置是因為logback-spring.xml檔案中引用了${mvn.log.dir}變數,需要動態從pom檔案中載入生效 -->
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </testResource>
        </testResources>
    </build>

相關文章