springboot(2.3.4)替换默认的logback为log4j2

1. application.properties 配置logging.config=classpath:log4j2.xml,如果不配置默认读取的是resources下的log4j2.xml 

2.配置依赖

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

3.配置log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 第一全局参数信息 -->
    <Properties>
        <property name="LOG_PATTERN"
                  value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1}:%L - %msg%n"/>
        <!-- debug 日志文件位置及名称,在rollFile的时候老文件的生成规则 -->
        <property name="DEBUG_FILE_PATH_ING"
                  value="../logs/app_debug.log"/>
        <property name="DEBUG_FILE_PATH_ED"
                  value="../logs/app_debug-%d{yyyy-MM-dd}.%i.log"/>

        <!-- info 日志文件位置及名称,在rollFile的时候老文件的生成规则 -->
        <property name="INFO_FILE_PATH_ING"
                  value="../logs/app_info.log"/>
        <property name="INFO_FILE_PATH_ED"
                  value="../logs/app_info-%d{yyyy-MM-dd}.%i.log"/>

        <!-- error 日志文件位置及名称,在rollFile的时候老文件的生成规则 -->
        <property name="ERROR_FILE_PATH_ING"
                  value="../logs/app_error.log"/>
        <property name="ERROR_FILE_PATH_ED"
                  value="../logs/app_error-%d{yyyy-MM-dd}.%i.log"/>

        <!-- warn 日志文件位置及名称,在rollFile的时候老文件的生成规则 -->
        <property name="WARN_FILE_PATH_ING"
                  value="../logs/warn_error.log"/>
        <property name="WARN_FILE_PATH_ED"
                  value="../logs/warn_error-%d{yyyy-MM-dd}.%i.log"/>

        <!-- 日志文件最大文件大小全局配置,一天之内最大文件产生数量全局配置 -->
        <property name="MAX_FILE_SIZE" value="100MB"/>
        <property name="MAX_FILE_NUM" value="30"/>
    </Properties>

    <appenders>
        <!-- 控制台输出日志信息 -->
        <console name="Console_Log" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </console>

        <!-- debug日志文件输出日志信息 -->
        <RollingFile name="Debug_File"
                     fileName="${DEBUG_FILE_PATH_ING}" filePattern="${DEBUG_FILE_PATH_ED}">
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT"
                             onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy
                        size="${MAX_FILE_SIZE}"/>
                <TimeBasedTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy
                    max="${MAX_FILE_NUM}"/>
        </RollingFile>

        <!-- info日志文件输出日志信息 -->
        <RollingFile name="Info_File"
                     fileName="${INFO_FILE_PATH_ING}" filePattern="${INFO_FILE_PATH_ED}">
            <ThresholdFilter level="INFO" onMatch="ACCEPT"
                             onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy
                        size="${MAX_FILE_SIZE}"/>
                <TimeBasedTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy
                    max="${MAX_FILE_NUM}"/>
        </RollingFile>

        <!-- warn日志文件输出日志信息 -->
        <RollingFile name="Warn_File"
                     fileName="${ERROR_FILE_PATH_ING}" filePattern="${ERROR_FILE_PATH_ED}">
            <ThresholdFilter level="WARN" onMatch="ACCEPT"
                             onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy
                        size="${MAX_FILE_SIZE}"/>
                <TimeBasedTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy
                    max="${MAX_FILE_NUM}"/>
        </RollingFile>

        <!-- error日志文件输出日志信息 -->
        <RollingFile name="Error_File"
                     fileName="${ERROR_FILE_PATH_ING}" filePattern="${ERROR_FILE_PATH_ED}">
            <ThresholdFilter level="ERROR" onMatch="ACCEPT"
                             onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy
                        size="${MAX_FILE_SIZE}"/>
                <TimeBasedTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy
                    max="${MAX_FILE_NUM}"/>
        </RollingFile>

    </appenders>

    <!--配置总的日志监听级别 -->
    <loggers>
        <!-- 业务代码输出debug -->
        <Logger name="com.config" level="debug" additivity="true">
            <!--
            <AppenderRef ref="Debug_File"></AppenderRef>
            -->
        </Logger>
        <root level="INFO">
            <appender-ref ref="Console_Log"/>
            <!--
            <appender-ref ref="Debug_File"/>
            <appender-ref ref="Info_File"/>
            <appender-ref ref="Error_File"/>
            -->
        </root>
    </loggers>
</configuration>

 

posted @ 2021-09-08 22:00  zincredible  阅读(1239)  评论(0编辑  收藏  举报