log4j2接入springboot项目

1.引入依赖

版本描述如下:
<slf4j.version>1.7.31</slf4j.version>
<log4j2.version>2.12.4</log4j2.version>

<!-- log4j2 begin-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
           <version>1.7.31</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.31</version>
        </dependency> <!--通过桥接模块将spring内部使用的jcl委托给slf4j,slf4j最终再委托给log4j2适配层-->
        <!--适配层-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.4</version>
        </dependency> <!--绑定模块,将日志请求委托给log4j2实现,这个绑定jar包是log4j2为了迎合slf4j,由log4j2主动提供的-->
        <!--底层log4j2-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.12.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.12.4</version>
        </dependency>
        <!-- 为了支持异步日志,添加Log4j2的异步日志处理器依赖 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>
        <!-- log4j2 end-->

2.配置log4j2.xml

路径在resources/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <properties>
        <property name="LOG_HOME" value="${mvn.log.dir}"/>
        <property name="LOG_LEVEL" value="${mvn.log.level}"/>
        <property name="PATTERN_LAYOUT">[%d{yyyy-MM-dd HH:mm:ss.SSS}][%p][%thread][SECURITY][%X{PFTID}][%l] - %.-1000m - %ex{200}%n</property>
        <property name="PACKAGE_PREFIX" value="${mvn.package.prefix}" />
        <property name="INCLUDE_LOCATION" value="${mvn.log.includeLocation}" />  <!-- 生产环境提高性能,将include_location设置为false日志中不进行输出类名,行号上下文关键信息-->
    </properties>

    <Appenders>
        <!-- info -->
        <RollingFile  name="infoFile" fileName="${LOG_HOME}/main.log"
                      filePattern="${LOG_HOME}/main-%d{yyyy-MM-dd}-%i.log.gz"
                      bufferSize="8192" immediateFlush="false" append="true" >
            <Filters>
                <ThresholdFilter level="error"  onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn"  onMatch="ACCEPT" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info"  onMatch="ACCEPT" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="${PATTERN_LAYOUT}" charset="UTF-8" />
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                <SizeBasedTriggeringPolicy size="300MB" />
            </Policies>
            <DefaultRolloverStrategy max="40" />
        </RollingFile >
        <!-- error -->
        <RollingFile  name="errorFile" fileName="${LOG_HOME}/error.log"
                      filePattern="${LOG_HOME}/error-%d{yyyy-MM-dd}-%i.log.gz"
                      bufferSize="8192" immediateFlush="false" append="true" >
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="${PATTERN_LAYOUT}" charset="UTF-8" />
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                <SizeBasedTriggeringPolicy size="300MB" />
            </Policies>
            <DefaultRolloverStrategy max="40" />
        </RollingFile >

        <!-- 全局日志 -->
        <RollingFile name="globalAppender" fileName="${LOG_HOME}/global.log" filePattern="${LOG_HOME}/global-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="${PATTERN_LAYOUT}" charset="UTF-8"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                <SizeBasedTriggeringPolicy size="300MB"/>
            </Policies>
            <!-- 注意此处磁盘最大占比 -->
            <DefaultRolloverStrategy max="40"/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <!-- 分开打印info和error级别日志至不同的文件中 -->
        <Logger name="${PACKAGE_PREFIX}" level="info" additivity="false" includeLocation="${INCLUDE_LOCATION}">
            <AppenderRef ref="infoFile" />
            <AppenderRef ref="errorFile" />
        </Logger>
        <!-- jvm参数配置log4j2.contextSelector开启异步logger -->
        <AsyncRoot level="${LOG_LEVEL}" includeLocation="${INCLUDE_LOCATION}">
            <AppenderRef ref="globalAppender"/>
        </AsyncRoot>
    </Loggers>
</Configuration>

3.log4j2.xml中的变量的定义

pom.xml

<properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <mvn.package.prefix>${project.groupId}</mvn.package.prefix>
    </properties>


    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activeEnv>dev</activeEnv>
                <mvn.log.dir>./target/logs</mvn.log.dir>
                <mvn.log.STDOUT>true</mvn.log.STDOUT>
                <mvn.log.level>INFO</mvn.log.level>
                <mvn.log.includeLocation>true</mvn.log.includeLocation>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <activeEnv>test</activeEnv>
                <spring.profiles.active>uat</spring.profiles.active>
                <mvn.log.dir>/export/Logs/${project.artifactId}</mvn.log.dir>
                <mvn.log.STDOUT>true</mvn.log.STDOUT>
                <mvn.log.level>INFO</mvn.log.level>
                <mvn.log.includeLocation>true</mvn.log.includeLocation>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <activeEnv>uat</activeEnv>
                <spring.profiles.active>uat</spring.profiles.active>
                <mvn.log.dir>/export/Logs/${project.artifactId}</mvn.log.dir>
                <mvn.log.STDOUT>false</mvn.log.STDOUT>
                <mvn.log.level>INFO</mvn.log.level>
                <mvn.log.includeLocation>true</mvn.log.includeLocation>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activeEnv>prod</activeEnv>
                <spring.profiles.active>prod</spring.profiles.active>
                <mvn.log.dir>/export/Logs/${project.artifactId}</mvn.log.dir>
                <mvn.log.STDOUT>false</mvn.log.STDOUT>
                <mvn.log.level>ERROR</mvn.log.level>
                <mvn.log.includeLocation>false</mvn.log.includeLocation>
            </properties>
        </profile>
    </profiles>

4.pom中配置的变量如何构建的时候替换掉log4j2.xml

配置maven-resource-plugin插件,并指定生效的目录

 <!-- 资源文件打包需要关注,log4j2.xml中可以正确引导到相关变量,及脚本目录打包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters><!--  这是重点-->
                </configuration>
            </plugin>

 <!-- 添加这段配置是因为logback-spring.xml文件中引用了${mvn.log.dir}变量,需要动态从pom文件中加载生效 -->
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/bin</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.sh</include>
                </includes>
            </resource>
        </resources>
posted @ 2024-04-19 16:02  SpecialSpeculator  阅读(11)  评论(0编辑  收藏  举报