log4j2 RollingFile用法

时间:2025-03-22 22:53:16

一、按天记录日志,日志保留7天

        <RollingFile name="rollingFile" fileName="log/" filePattern="log/$${date:yyyy-MM}/main-%d{yyyy-MM-dd}.">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="log" maxDepth="2">
                    <IfFileName glob="*/*." />
                    <IfLastModified age="7d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

二、按小时记录日志,日志保留6天

        <RollingFile name="rollingFile" fileName="log/" filePattern="log/$${date:yyyy-MM}/main-%d{yyyy-MM-dd-HH}.">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="log" maxDepth="2">
                    <IfFileName glob="*/*." />
                    <IfLastModified age="6d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile> 

三、按大小记录日志,满20MB记录一个日志,日志保留7天;同一天最多生成10个20MB日志文件,由filePattern中的%i和DefaultRolloverStrategy中max的值决定

        <RollingFile name="rollingFile" fileName="log/" filePattern="log/$${date:yyyy-MM}/main-%d{yyyy-MM-dd}-%">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
            <DefaultRolloverStrategy max="10">
                <Delete basePath="log" maxDepth="2">
                    <IfFileName glob="*/*." />
                    <IfLastModified age="7d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

关键点在于 filePattern后的日期格式,以及TimeBasedTriggeringPolicy的interval;日期格式精确到哪一位,interval也精确到哪一个单位
 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" nmonitorInterval="30">
    <Appenders>
        <RollingFile name="rollingFile" fileName="log/" filePattern="log/$${date:yyyy-MM}/main-%d{yyyy-MM-dd}.">
                <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %l - %m%n"/>
                <Policies>
                    <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                </Policies>
                <DefaultRolloverStrategy>
                    <Delete basePath="log" maxDepth="2">
                        <IfFileName glob="*/*." />
                        <IfLastModified age="7d" />
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="rollingFile"/>
        </Root>
    </Loggers>
</Configuration>

官方参考文档:/log4j//manual/