A busy application can fill a disk even when the system logs already have a retention policy. On Ubuntu, logrotate can archive an application log on a schedule and recreate the active file with the same ownership and permissions.

An application that opens the log path for each write can use the replacement file after rotation. A process that holds the file open needs the application's documented log-reopen mechanism to switch from the archive to the replacement file.

Ubuntu's scheduled logrotate job loads rules from /etc/logrotate.d/ through /etc/logrotate.conf. The scheduled job needs to be active with Ubuntu's packaged global rotation settings. The application needs an existing log with no rotation policy of its own or through another rule.

Steps to configure logrotate for an application log in Linux:

  1. Check your active log, its parent directory, and any existing .1 archive.

    The paths are examples. Shell paths containing spaces need single quotes. The first column shows each inode number. A missing .1 is expected if there is no archive yet. Stop unless the active log's type is regular file. The application account should own the log and directory, and only that account should have write permission on the directory.

    $ sudo stat -c '%i %F %a %U:%G %n' \
        /var/log/myapp /var/log/myapp/app.log /var/log/myapp/app.log.1
    3065430 directory 750 myapp:myapp /var/log/myapp
    3065431 regular file 640 myapp:myapp /var/log/myapp/app.log
    3065482 regular file 640 myapp:myapp /var/log/myapp/app.log.1

    An unattended timer run can rotate an eligible log and remove old archives as soon as the rule is installed. The edit and checks need a quiet maintenance window after the last scheduled run has finished and before the next one starts. The application must make no log writes during the forced test rotation. If the oldest archive must be kept, back it up before installing the rule.

  2. Open a new rule file in /etc/logrotate.d with sudoedit.

    The log must be in a location writable by Ubuntu's packaged logrotate.service, such as under /var/log/ or /srv/, because ProtectSystem=full makes /usr/ read-only for the service. myapp stands for your application name. sudoedit opens your usual editor. If the rule file already exists, stop here and check whether it already covers your log.

    $ sudoedit /etc/logrotate.d/myapp
  3. Enter the rotation policy for your active log in the rule file.

    Substitute your log path, application account and group, and the existing log's mode, written with a leading zero. A log path containing spaces needs double quotes in the rule.

    /var/log/myapp/app.log {
        su myapp myapp
        daily
        rotate 7
        notifempty
        nodateext
        compress
        delaycompress
        create 0640 myapp myapp
    }

    The su line rotates the file as the application account. The create line gives the replacement file its mode, owner, and group. The rule skips empty logs and keeps up to seven numbered archives. The newest archive is .1, with compression deferred until the next rotation.

  4. Save the file.
  5. Close the editor.

    sudoedit writes the edit to /etc/logrotate.d/ when the editor closes.

  6. Check that the system configuration includes your rule without errors.
    $ sudo logrotate --debug /etc/logrotate.conf
    ##### snipped #####
    reading config file myapp
    ##### snipped #####
    considering log /var/log/myapp/app.log
    ##### snipped #####

    Debug mode leaves files unchanged. The output should show your rule and active log. If an error or warning names your new rule, remove the new rule file from /etc/logrotate.d/ before stopping so the next scheduled run skips it. If debug reports any other error, stop before forcing rotation.

  7. Force one rotation using the path of your new rule file.

    The test requires a nonempty log. Forcing rotation moves the active log to .1 immediately.

    $ sudo logrotate --force --verbose /etc/logrotate.d/myapp
    ##### snipped #####
    renaming /var/log/myapp/app.log to /var/log/myapp/app.log.1
    creating new /var/log/myapp/app.log mode = 0640 uid = 1001 gid = 1001
    ##### snipped #####

    Passing only your rule file limits the forced run to this log. If the rename succeeds but the replacement is missing, move the rule outside /etc/logrotate.d/ and restore the archived file to the active path while the writer is idle.

  8. Confirm the rotation from the inodes, ownership, and permissions of the active log and its .1 archive.
    $ sudo stat -c '%i %a %U:%G %n' \
        /var/log/myapp/app.log /var/log/myapp/app.log.1
    3065482 640 myapp:myapp /var/log/myapp/app.log
    3065431 640 myapp:myapp /var/log/myapp/app.log.1

    Both files should have the expected ownership and permissions. The active log must have a different inode from the one listed before rotation, and .1 must now have the old active log's inode. An unchanged active inode or no new .1 means rotation was skipped. Stop before testing application writes if either check fails. Logrotate can skip a file that has multiple hard links.

  9. Trigger a harmless application action whose log entry you can identify.

    A logged health check is one option. Its request ID, timestamp, or another unique value lets you identify the entry among other traffic.

  10. Verify that your action's entry appears in the recent lines of your active log.
    $ sudo tail -n 20 /var/log/myapp/app.log
    INFO request_id=b722 health check OK
    INFO request_id=a431 health check OK
    INFO request_id=c953 health check OK

    The test request in this output is a431. The write check passes when the active log shows the identifying value from your action. If traffic is heavy, increase -n so the output covers the time of your action. If the matching entry appears only in the .1 archive, the application still has the old file open, so reopen its log or restart the application using its documented method, then confirm a fresh test entry in the active log before relying on the rule.

Share this guide