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

<?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="ENV" value="${sys:env:-dev}"/> <!-- 默认为prod -->
        <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}" />
    </properties>

    <Appenders>
        <!-- 控制台Appender,仅在开发环境中启用 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${PATTERN_LAYOUT}" charset="UTF-8"/>
            <Filters>
                <ThresholdFilter level="${LOG_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </Console>
        <!-- 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>
        <!-- 根据环境变量决定是否启用控制台日志 -->
        <Root level="${LOG_LEVEL}" includeLocation="${INCLUDE_LOCATION}">
            <AppenderRef ref="globalAppender"/>
            <!-- 如果环境变量ENV为dev,则添加控制台输出 -->
            <If condition="'${ENV}' == 'dev'">
                <AppenderRef ref="Console"/>
            </If>
        </Root>
    </Loggers>
</Configuration>

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  阅读(68)  评论(0编辑  收藏  举报