Split log by date in log4j2 of spring boot

1、application.yml

logging:
  config: classpath:log4j2.xml

2、log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
    <Properties>
        <!--System variable ${sys:logPath:-/var/logs} Retrieve the value of the system variable logPath, or set the default value if it is empty./var/logs-->
       <Property name="LOG_HOME">${sys:catalina.home:-.}/logs</Property>
        <Property name="LOG_BACK_HOME">${sys:catalina.home:-.}/logs/backup</Property>
        <Property name="LOG_PATTERN_LAYOUT">%date %highlight{[%-5.5level]}{STYLE=Logback} [%-10.10thread] [%X{employeeCode}] [%X{X-B3-TraceId}/%X{X-B3-SpanId}] %cyan{[%-50.50class:%-4.4line]} - %msg%xEx%n </Property>
        <Property name="DEFAULT_CHARSET">UTF-8</Property>
        <Property name="ERROR_FILE_NAME">error</Property>
        <Property name="INFO_FILE_NAME">info</Property>
    </Properties>

    <Appenders>
        <!-- Configure daily logs -->
        <RollingFile name="${INFO_FILE_NAME}" fileName="${LOG_HOME}/${INFO_FILE_NAME}.log" filePattern="${LOG_BACK_HOME}/$${date:yyyy-MM}/${INFO_FILE_NAME}-%d{yyyy-MM-dd}.log.gz" append="true">
            <PatternLayout charset="${DEFAULT_CHARSET}" pattern="${LOG_PATTERN_LAYOUT}"/>
            <!-- Setting Policy -->
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
            <DefaultRolloverStrategy >
                <!--Delete log from 30 days ago-->
                <Delete basePath="${LOG_BACK_HOME}" maxDepth="2">
                    <IfFileName glob="*/*.log.gz" />
                    <IfLastModified age="30d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <!-- Configuration error log -->
        <RollingFile name="${ERROR_FILE_NAME}" fileName="${LOG_HOME}/${ERROR_FILE_NAME}.log" filePattern="${LOG_BACK_HOME}/$${date:yyyy-MM}/${ERROR_FILE_NAME}-%d{yyyy-MM-dd}.log.gz" append="true">
            <PatternLayout charset="${DEFAULT_CHARSET}" pattern="${LOG_PATTERN_LAYOUT}"/>
            <!-- Setting Policy -->
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
            <Filters>
                <!--Logs with levels greater than or equal to WARN can be written-->
                <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="org.springframework" level="INFO" additivity = "false">
            <AppenderRef ref="${INFO_FILE_NAME}"/>
            <AppenderRef ref="${ERROR_FILE_NAME}"/>
        </Logger>
        <Logger name="com.picc" level="DEBUG" additivity = "false">
            <AppenderRef ref="${INFO_FILE_NAME}"/>
            <AppenderRef ref="${ERROR_FILE_NAME}"/>
        </Logger>
        <Root level="INFO">
            <AppenderRef ref="${INFO_FILE_NAME}"/>
            <AppenderRef ref="${ERROR_FILE_NAME}"/>
        </Root>
    </Loggers>
</Configuration>

3. Use result display

cd backup/

You’ll find monthly backups

 cd 2020-06/

Log is compressed, self decompression view

Read More: