【解决方案】Springboot整合log4j2

1、添加log4j2包,并排除springboot自带的log组件

implementation 'org.springframework.boot:spring-boot-starter-log4j2'

configurations.all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    }

2、在资源文件下(resources/application.properties),配置

logging.config=classpath:log4j2.xml

 3、编写log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="error">
    <Properties>
        <Property name="baseDir">/var/log/test</Property>
    </Properties>
    <Appenders>

        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只输出info及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <!--默认打印出debug以上的信息(具体打印级别可进行页面配置),每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="info" fileName="${baseDir}/info.log"
                     filePattern="${baseDir}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd-HH}-%i.log.gz" filePermissions="rw-r--rw-">
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t:%r] - [%p]%c{1} : %line [%t] %m%n"/>
            <Policies>
                <!-- 每天分割 -->
                <TimeBasedTriggeringPolicy/>
                <!-- 文件每100M分割 -->
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <!-- 每天文件个数50个 -->
            <DefaultRolloverStrategy max="50">
                <!-- info日志保存五天 -->
                <Delete basePath="${baseDir}" maxDepth="3">
                    <IfFileName glob="*/info-*.log.gz">
                        <IfLastModified age="5d">
                            <IfAny>
                                <IfAccumulatedFileSize exceeds="1 GB"/>
                                <IfAccumulatedFileCount exceeds="50"/>
                            </IfAny>
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

        <!--文件只会记录error信息-->
        <RollingFile name="error" fileName="${baseDir}/error.log"
                     filePattern="${baseDir}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd-HH}-%i.log.gz"
                     filePermissions="rw-r--rw-">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t:%r] - [%p]%c{1} : %line [%t] %m%n"/>
            <Policies>
                <!-- 每天分割 -->
                <TimeBasedTriggeringPolicy/>
                <!-- 文件每10M分割 -->
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy>
                <!-- error日志保存三十天 -->
                <Delete basePath="${baseDir}" maxDepth="3">
                    <IfFileName glob="*/error-*.log.gz"/>
                    <IfLastModified age="30d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

    </Appenders>
    <Loggers>
        <!--建立一个默认的root的logger-->
        <Root level="trace">
            <AppenderRef ref="info"/>
            <AppenderRef ref="Console"/>
            <AppenderRef ref="error"/>
        </Root>
    </Loggers>
</Configuration>
发布了147 篇原创文章 · 获赞 88 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/zpwangshisuifeng/article/details/103943732