log4j2生效

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>
posted @ 2024-04-16 13:30  SpecialSpeculator  阅读(23)  评论(0编辑  收藏  举报