Search by

dmlab / module-log-archive

dmlab

Scheduled, lossless archiving of Magento var/log: zips every *.log, empties the originals without losing lines written during the run, prunes old archives and var/report files. Default off — installing changes nothing.

Package info

github.com/dmlab-work/module-log-archive

Type:magento2-module

pkg:composer/dmlab/module-log-archive

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.0.1 2026-11-30 10:14 UTC

This package is auto-updated.

Last update: 2026-11-30 10:27:16 UTC


README

Scheduled, lossless archiving of Magento's var/log and cleanup of var/report.

CI codecov Packagist Version Downloads PHP License Magento

Magento never rotates var/log: system.log, exception.log and third-party logs grow until the disk fills, and var/report collects thousands of error reports. On a schedule you set in the admin, this module zips every *.log into var/log/archive/logs-<Ymd-His>.zip (UTC), empties the originals keeping lines written during the run, and prunes old archives and reports. Pairs with dmlab/module-disable-log to stop noisy logs at the source.

Log Archive configuration

Install

composer require dmlab/module-log-archive
bin/magento module:enable DmLab_LogArchive
bin/magento setup:upgrade

Requirements

Magento 2.4.7+ · PHP 8.2–8.5 · ext-zip

Configuration

Stores → Configuration → DMLab → Log Archive (default scope, ACL DmLab_LogArchive::config). Scheduled archiving is off by default; installing changes nothing until you enable it.

Field Default Meaning
Enable Scheduled Archiving No Runs the cron job; the CLI works regardless
Schedule (Cron Expression) 0 0 1 * * 5-field cron expression (monthly by default); invalid ones are rejected on save
Exclude Files empty One glob per line, relative to var/log (debug.log, payment/*.log); * also matches /
Keep Archives (Days) 365 Older archives are deleted; 0 = keep forever
Max Total Archive Size (MB) 0 Oldest archives are deleted until the total fits; 0 = no cap
Keep var/report Files (Days) 30 Older error reports are deleted; 0 = keep forever

CLI

bin/magento dmlab:log:archive [--dry-run]

--dry-run prints what would be archived and deleted and changes nothing. With a size cap it counts the new archive as 0 bytes (its compressed size is unknown), so a real run may delete more:

Would archive 3 files (1.2 GB) into /var/www/html/var/log/archive/logs-20261201-000000.zip
+-------------------+----------+
| File              | Size     |
+-------------------+----------+
| debug/payment.log | 12.4 MB  |
| exception.log     | 310.2 MB |
| system.log        | 905.7 MB |
+-------------------+----------+
Would delete archive logs-20251101-000000.zip (88.3 MB, by age)
Would delete 1842 var/report files (54.1 MB)

Exit code 0 on success, 1 on failure or when another run holds the lock.

How it stays safe

  • Nothing is touched unless the zip is verified: it is written as .part, reopened with a consistency check, every entry size compared with the scanned size, then renamed. Not enough free space → logs untouched; old archives and reports are still pruned.
  • Truncate, not delete: each log is emptied in place and bytes appended after the scan are appended back. The inode stays the same, so queue consumers and long cron processes keep logging to the live file.
  • Only logs-*.zip are pruned; other files in var/log/archive are never touched. The newest archive is never deleted, by age or by the size cap. A .part left by a killed run is removed on the next run.
  • One run at a time: a lock through Magento's lock manager (DB-backed by default, so it covers multiple nodes).

Limitations

  • Writers don't lock logs (like logrotate's copytruncate): a line written in the microseconds between reading the tail and truncating is lost; a writer that opens logs without append mode may overwrite the tail.
  • Symlinked, unreadable and empty logs are skipped. A log that cannot be truncated is archived again next run.
  • A single archive bigger than the size cap is kept, with a warning.
  • A scheduled run archives only the var/log of the node that picks up the cron job. Nodes with their own var/log need it shared, or bin/magento dmlab:log:archive in each node's crontab at staggered times (the lock is shared).

Tests

vendor/bin/phpunit -c Test/Unit/phpunit.xml

Architecture

The cron job dmlab_log_archive (skipped while disabled) and the CLI both call Model\Runner:

  1. take the dmlab_log_archive lock;
  2. Archive\Scanner lists *.log under var/log (except archive/) and records each file's size: the snapshot;
  3. check free space against the snapshot total;
  4. Archive\ZipWriter adds each file limited to its snapshot size, so lines appended meanwhile stay out of the entry; the zip is verified as .part, then renamed;
  5. Archive\Truncator locks each file, reads the bytes past the snapshot, truncates it to zero and appends them back;
  6. Retention\ArchivePruner and Retention\ReportPruner delete by age and size;
  7. one summary line goes to system.log, then the lock is released.

A dry run takes the same path and stops before every write. The schedule is read through the job's config_path (dmlab_log_archive/general/cron_expr); Model\Config\Backend\CronExpr validates it with core's cron matcher on save.

License

OSL-3.0