如何修改日志归档规则:仅每日一次或达到maxFileSize时归档
Got it, let's work through this log archiving setup issue. From what you described, your current server creates compressed log archives every time it restarts, and you want to switch to a setup that only archives logs once per day, and only when the log file exceeds your specified maxFileSize. I'll break this down using Logback (the most common logging framework for such scenarios), but the core ideas apply to others like Log4j2 too.
First: Stop Restart-Triggered Archiving
The reason your server archives logs on restart is likely because your current config creates a new archive file whenever the application boots. To fix this, make sure you set <append>true</append> in your appender—this tells the logger to add new logs to the existing file instead of archiving it and starting fresh.
Core Setup: Daily Archiving with Size Check
We'll use a RollingFileAppender combined with two triggering policies: one for daily timing, and one for size limits. Here's a complete, commented config example:
<appender name="APP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Path to your active, unarchived log file --> <file>/var/log/your-app/active.log</file> <!-- Critical: Append to existing log on restart (no auto-archive) --> <append>true</append> <!-- Manage rolling/archiving behavior --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- Naming pattern for archived logs: includes date, and optional index for multiple daily archives --> <!-- Remove `-%i` if you want strictly one archive per day, even if size is exceeded multiple times --> <fileNamePattern>/var/log/your-app/archives/app-%d{yyyy-MM-dd}-%i.gz</fileNamePattern> <!-- Keep archived logs for 30 days (adjust as needed) --> <maxHistory>30</maxHistory> <!-- Combine time and size triggers --> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- Your max file size threshold (e.g., 100MB, 1GB) --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!-- Don't clean/archive old logs on app start --> <cleanHistoryOnStart>false</cleanHistoryOnStart> </rollingPolicy> <!-- Customize your log format here --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender>
How This Works
Let's break down the key parts to match your requirements:
- Daily archiving: The
TimeBasedRollingPolicyuses%d{yyyy-MM-dd}in the filename pattern to trigger an archive at midnight every day. - Size threshold check: The
SizeAndTimeBasedFNATPpolicy ensures that archiving only happens if the active log file has grown beyond yourmaxFileSize. If the file is smaller than the threshold at midnight, no archive is created—logs keep writing to the active file. - No restart archives:
<append>true</append>and<cleanHistoryOnStart>false</cleanHistoryOnStart>prevent the app from creating an archive when it boots up.
Strictly One Archive Per Day (Even If Size Is Exceeded Multiple Times)
If you want to limit archiving to exactly once per day (even if the log file hits the size limit multiple times during the day), remove the -%i from the fileNamePattern and set maxIndex=1 in a FixedWindowRollingPolicy instead. Here's how that looks:
<appender name="APP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>/var/log/your-app/active.log</file> <append>true</append> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>/var/log/your-app/archives/app-%i.gz</fileNamePattern> <minIndex>1</minIndex> <maxIndex>1</maxIndex> <!-- Only one archive per day --> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.CompositeTriggeringPolicy"> <triggeringPolicies> <!-- Trigger at midnight daily --> <cronTriggeringPolicy class="ch.qos.logback.core.rolling.CronTriggeringPolicy"> <cron>0 0 0 * * ?</cron> </cronTriggeringPolicy> <!-- Only trigger if file size exceeds threshold --> <sizeBasedTriggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>100MB</maxFileSize> </sizeBasedTriggeringPolicy> </triggeringPolicies> </triggeringPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender>
This setup will only archive logs if both conditions are met: it's midnight, and the log file is larger than maxFileSize. No mid-day archives, no restart archives—exactly one archive per day when needed.
For Log4j2 Users
The logic is identical, just with different syntax:
- Use a
RollingFileAppenderwith aCompositeTriggeringPolicythat combinesTimeBasedTriggeringPolicyandSizeBasedTriggeringPolicy. - Set
append="true"to avoid restart-triggered archives.
内容的提问来源于stack exchange,提问作者Aymen Kanzari

