Log Rotation: How It Works, Rotation Strategies, and Configuration for Mobile Projects

Author: IT Sectr Published: 2026-05-28 Reading time: 8 min

Log Rotation is an automatic log file management mechanism that prevents disk overflow through archiving, compression, and deletion of old records. In mobile applications, logs accumulate on the user's device, and without rotation they can take up gigabytes of memory within weeks of use. According to Redis Documentation, proper log rotation configuration reduces the risk of system failure due to a full disk by 99% compared to uncontrolled log growth. The main rotation strategies are: by file size, by time, and by number of files — each is chosen depending on the use case: logrotate on Linux, CocoaLumberjack on iOS, and Timber on Android all support all three approaches.

Key Takeaways

  • Log Rotation — automatic switching of the active log file when a specified threshold is reached, with archiving or deletion of old files
  • Size-based rotation — creates a new file when the current one reaches the limit (typically 10–100 MB), the old one is compressed to .gz
  • Time-based rotation — file switch every N hours or once a day regardless of size, convenient for daily dumps
  • logrotate — the standard Linux utility for automatic rotation of system and application logs
  • Disk quota — limits the total volume of all logs on the device; when exceeded, the oldest files are deleted

What Is Log Rotation

Log Rotation is the process of periodically switching the active log file to a new one while simultaneously archiving, compressing, or deleting the old one. Without rotation, a single log file grows indefinitely until it fills the entire disk partition, leading to application failure and data loss.

A typical scenario: an application writes logs to the app.log file. When app.log reaches 100 MB, the system renames it to app.log.1, compresses it to app.log.1.gz, and creates a new empty app.log. On the next fill-up, app.log.1 becomes app.log.2, app.log.1.gz becomes app.log.2.gz, and the old app.log.2.gz is deleted. This mechanism is called rotation with keep count — the number of archive copies is fixed.

According to Splunk (2023), incorrect rotation configuration is the cause of 40% of incidents related to disk space exhaustion on application servers. For mobile devices, rotation is even more critical because users cannot and should not manage logs manually.

Log Rotation Strategies

Log Rotation supports three basic strategies that can be combined. The choice of strategy depends on the application type: server systems more often use time-based rotation, mobile use size-based, and embedded systems use file-count-based.

StrategyTriggerWhen to Use
By SizeFile reached N bytesHigh-load systems with unpredictable log volume
By TimeN hours/days passedDaily dumps, compliance requirements
By File CountN files createdMobile devices with limited disk space

Size-based rotation — the most common

Size-based rotation ensures that no single log file exceeds a set limit. The limit is chosen based on available disk space and logging frequency. For a server, the typical limit is 100–500 MB per file, for a mobile device — 1–10 MB. If the application logs aggressively, the limit should be lowered, otherwise rotation will occur every few minutes.

Time-based rotation — for compliance

Time-based rotation is independent of log volume — the file is switched strictly on schedule. Convenient for systems where logs must be stored for a fixed number of days: daily rotation with keep count = 30 means 30 days of storage. The downside — a single file can grow to a gigabyte per day under heavy load.

logrotate on Linux: Configuration and Examples

logrotate is the standard Linux utility for automatic log rotation. It runs via cron and processes configuration files from /etc/logrotate.d/. Each service (nginx, postgresql, application) creates its own config specifying log paths, rotation strategy, and post-rotation actions.

cpp
# /etc/logrotate.d/myapp — application log rotation
/var/log/myapp/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data www-data
    postrotate
        kill -HUP $(cat /var/run/myapp.pid)
    endscript
}

This config rotates logs daily, keeps 7 archive copies, compresses old files with gzip (except the last one — delaycompress), does not error if logs are missing (missingok), does not rotate empty files (notifempty), and recreates the file with 0640 permissions. After rotation, it sends a HUP signal to the application process via a postrotate script.

logrotate Parameters

size — rotation upon reaching a size (size 100M). rotate — number of archive copies (rotate 7). compress — gzip compression. dateext — appends the date to the filename instead of a sequence number. sharedscripts — runs postrotate once for all files, not for each individually. maxage — deletes archives older than N days.

Log Rotation in Mobile Applications

On mobile devices, Log Rotation is critical because the user does not manage the file system and does not expect the application to take up gigabytes with logs. iOS and Android have built-in mechanisms: os_log on iOS uses a fixed-size ring buffer (rotation by overwrite), Android Logcat has a limited kernel buffer.

For custom file logs on iOS, CocoaLumberjack is used with the DDFileLogger class, which supports size-based and time-based rotation. On Android — Logback or custom implementations via RollingFileAppender. Both tools allow setting maximum file size and number of archives.

swift
// CocoaLumberjack — file rotation on iOS
import CocoaLumberjack

let fileLogger = DDFileLogger()
fileLogger.maximumFileSize = 1024 * 1024 // 1 MB
fileLogger.logFileManager.maximumNumberOfLogFiles = 5
DDLog.add(fileLogger)

iOS: os_log does not require rotation — messages are overwritten in the ring buffer. But if the application writes custom file logs (for debugging or server upload), rotation must be configured manually. CocoaLumberjack is the standard choice for iOS teams — it automatically compresses archives to .gz and deletes old files when the limit is exceeded.

Why Rotation Matters on Android

Android does not restrict applications from writing logs to their own directory. If a developer writes debug logs to a file without rotation, over a month of active use they can take up 500 MB to 1 GB. The user will discover the problem when the system shows a low-storage warning and will delete the app. Logback with RollingFileAppender solves this problem: a 5 MB limit with 3 archives guarantees logs never exceed 20 MB.

Rotation Implementation Examples on iOS and Android

Below are examples of log rotation configuration on both platforms. On iOS, CocoaLumberjack is used; on Android — Logback with XML configuration.

kotlin
// Logback on Android — rotation configuration in logback.xml
// File size 5MB, 3 archive copies
@file:Suppress("unused")

// In logback.xml:
// <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
//   <file>${DATA_DIR}/logs/app.log</file>
//   <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
//     <fileNamePattern>app.%i.log.gz</fileNamePattern>
//     <minIndex>1</minIndex>
//     <maxIndex>3</maxIndex>
//   </rollingPolicy>
//   <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
//     <maxFileSize>5MB</maxFileSize>
//   </triggeringPolicy>
// </appender>

CocoaLumberjack on iOS supports not only size-based rotation but also deletion of old logs by date using logFileManager.maximumLogFiles. If maximumLogFiles = 0 is set, the limit is removed — logs will accumulate indefinitely, which is dangerous for production.

swift
// Custom rotation with total volume check
class SizeAwareLogger {
    let maxTotalSize: Int64 = 20 * 1024 * 1024

    func enforceQuota(at logDirectory: URL) {
        let files = (try? FileManager.default
            .contentsOfDirectory(
                at: logDirectory,
                includingPropertiesForKeys: [.fileSize]
            )) ?? []
        let total = files.reduce(0) {
            $0 + (try? $1.resourceValues(forKeys: [.fileSize])
                .fileSize).map(Int64.init) ?? 0
        }
        if total > maxTotalSize {
            // Delete the oldest file
            files.sorted { $0.path < $1.path }.first.map {
                try? FileManager.default.removeItem(at: $0)
            }
        }
    }
}

Monitoring and Alerts for Rotation

Log Rotation is not only about automatic archiving but also a health indicator. If logs rotate too frequently (every few minutes), it signals excessive logging or an error log loop. Set up alerts for rotation frequency: more than 10 rotations per hour warrants investigation.

Monitoring systems (Prometheus, Grafana, Datadog) can track rotation metrics through file system exporters. Prometheus node_exporter provides file size and modification time metrics. On mobile devices, rotation monitoring is usually built into the SDK: CocoaLumberjack logs the rotation event via DDLog, and Logback sends status through an appender.

Alerts: if there are more archives than expected (rotate count exceeded the limit) or the total log volume exceeds the quota — the system should notify the administrator. For servers, the standard threshold is 80% of the partition size; for mobile devices — an alert when 50 MB per application is exceeded.

Frequently Asked Questions

What is the optimal log file size for rotation?

For servers — 100–500 MB, for mobile applications — 1–10 MB. A limit that is too small (under 1 MB) causes frequent rotation and unnecessary I/O operations. A limit that is too large (over 500 MB) increases file open and search time.

How many archive log copies should be kept?

For production — at least 7 days (daily rotation) or 3–5 archives (size-based rotation). For compliance requirements — 30–90 days, but use a separate storage with compression and retention policy rather than rotation on the same partition.

How does logrotate work on mobile devices?

logrotate is a Linux utility — it is not available on iOS or Android. On mobile devices, rotation is implemented by libraries: CocoaLumberjack for iOS and Logback for Android. They do not require root access and work within the application's sandbox environment.

What should I do if logs rotate every minute?

Check for circular logging — when error handling itself generates a new error. Add protection: a counter for repeated loggings of the same type with a threshold (no more than 100 identical messages per minute) and a temporary lock after exceeding it.

Is it mandatory to compress log archives?

Not mandatory, but recommended. gzip compresses text logs 10–20 times without data loss. On mobile devices, compression reduces storage usage from 50 MB to 3–5 MB. The only downside is that archives cannot be read without decompression, but for analysis usually only the current file is needed.

Summary

  • Log Rotation — automatic log file management with new file creation upon reaching a limit and archiving of old files to prevent disk overflow
  • Three strategies — by file size (most common), by time (for dumps), and by file count (for mobile devices with limited space)
  • logrotate — standard Linux utility for server-side rotation with flexible parameters: daily, size, compress, rotate, postrotate scripts
  • Mobile libraries — CocoaLumberjack on iOS and Logback on Android support size-based rotation with compression and archive count limits
  • Disk quota — total limit on all logs: 20 MB for mobile applications and 80% of partition for servers with alert on exceedance
  • Monitoring — too frequent rotation (more than 10 times per hour) signals circular error logging or excessive log volume
  • gzip compression — reduces archive size by 10–20 times, recommended for all platforms; delaycompress leaves the last archive uncompressed for quick reading

We will develop a mobile application turnkey

IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.

Discuss the project

Read also