简体   繁体   中英

Issue with filename in log4j2 rolling file appender

I have the following log4j2.xml configuration. I have a time as well as size based triggering policy. Since the size limit (10 MB) is breached first, the log file rolls over and while creating a new file, it appends wrong date to the filename. What I want is:

  • Roll over when a file's size reaches 10 MB (total size of all files should not exceed 1500 times 10 MB)
  • Roll over every 15 days

The file names should be abc.log, abc__2021-04-09__1.log, abc__2021-04-09__2.log. Basically it should have the current date appended to it.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <RollingFile name="RollingFileAppender" fileName="/var/log/abc/abc.log"
                     filePattern="/var/log/abc/abc__%d{yyyy-MM-dd}__%i.log" immediateFlush="false" append="true">
            <JsonLayout complete="true" compact="true" eventEol="true">
                <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}"/>
            </JsonLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="15"/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="1500"/>
        </RollingFile>
    </Appenders>
 
    <Loggers>
        <Root level="info" includeLocation="false">
            <AppenderRef ref="RollingFileAppender"/>
        </Root>
    </Loggers>
</Configuration>

Kindly let me know where am I wrong in my code. Thanks in advance.

NEW EDIT

Requirements - whichever of 1) or 2) happens earlier:

  1. When total size of all files exceeds 1500 times 10 MB ~ 15 GB (a file's size is 10 MB)
  2. Every 15 days from the start of service

If 1) happens earlier than 2), it should flush existing contents of files one by one as per the speed of logging and write to files with current date appended to the filename (not the date on which the service was started).

If 2) happens earlier than 1), it should delete all the existing files and start writing to files with current date appended to the filename (not the date on which the service was started).

Please help with the correct configuration.

you can use cron to rollover every 15 days

<CronTriggeringPolicy schedule="30 15 */15 * * "/> 

The RollingFileAppender uses the date pattern in the file pattern to determine how frequently to rollover. Since the granularity is 1 day that would normally indicate that it should rollover daily. You then added interval=15 and so have directed it to rollover every 15 days. The date of the rolled file will always be the date of the last time-based rollover. When you add a size-based triggering policy you are indicating that there can be further rollovers within that time window. You should think of the file as one time-based rollover that gets interrupted periodically by size-based rollovers, so all those do is increment the counter on the index.

The max parameter indicates that you want to allow 1500 files within that 15 day window, not a limit of 1500 files total.

If you want the date in the filename to change every day then you need to use an interval of 1.

If you want to limit the number of total files to 1500 then you need to add a Delete action to your rollover strategy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM