How to configure deletion policy in log4j2?

In log4j2, you can configure log file deletion operations using the Delete strategy. Here are some common examples of delete strategy configurations:

  1. Delete older log files based on the maximum storage space of the log file.
<DefaultRolloverStrategy>
    <Delete basePath="logs" maxDepth="2">
        <IfFileName glob="logs/app-*.log" />
        <IfLastModified age="7d" />
    </Delete>
</DefaultRolloverStrategy>

The above settings will delete app-*.log files in the logs folder that have a last modified date older than 7 days.

  1. Delete old log files based on the number of log files.
<DefaultRolloverStrategy>
    <Delete basePath="logs" maxDepth="1">
        <IfFileName glob="logs/app-*.log" />
        <IfAccumulatedFileSize exceeds="100 MB" />
    </Delete>
</DefaultRolloverStrategy>

The above configuration will remove the app-*.log files in the logs folder until the total file size exceeds 100 MB.

  1. Delete old log files based on custom conditions.
<DefaultRolloverStrategy>
    <Delete basePath="logs" maxDepth="1">
        <IfFileName glob="logs/app-*.log" />
        <If condition="java.nio.file.Files.isRegularFile(java.nio.file.Paths.get($${basePath}), java.nio.file.LinkOption.NOFOLLOW_LINKS) && java.nio.file.Files.getLastModifiedTime(java.nio.file.Paths.get($${basePath}), java.nio.file.LinkOption.NOFOLLOW_LINKS).toMillis() <= 0" />
    </Delete>
</DefaultRolloverStrategy>

The above settings will delete the app-*.log files in the logs folder if the file exists and the last modification time is less than or equal to 0.

Here are some common examples of log4j2 deletion policy configurations that can be adjusted and expanded according to actual needs.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds