dmlab / module-log-archive
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
Requires
- php: ~8.2.0||~8.3.0||~8.4.0||~8.5.0
- ext-zip: *
- magento/framework: ^103.0.7
- magento/module-config: ^101.2.7
- magento/module-cron: ^100.4.7
- psr/log: ^2.0||^3.0
- symfony/console: ^6.4||^7.4
Requires (Dev)
- phpunit/phpunit: ^11.5||^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Scheduled, lossless archiving of Magento's
var/logand cleanup ofvar/report.
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.
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-*.zipare pruned; other files invar/log/archiveare never touched. The newest archive is never deleted, by age or by the size cap. A.partleft 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/logof the node that picks up the cron job. Nodes with their ownvar/logneed it shared, orbin/magento dmlab:log:archivein 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:
- take the
dmlab_log_archivelock; Archive\Scannerlists*.logundervar/log(exceptarchive/) and records each file's size: the snapshot;- check free space against the snapshot total;
Archive\ZipWriteradds each file limited to its snapshot size, so lines appended meanwhile stay out of the entry; the zip is verified as.part, then renamed;Archive\Truncatorlocks each file, reads the bytes past the snapshot, truncates it to zero and appends them back;Retention\ArchivePrunerandRetention\ReportPrunerdelete by age and size;- 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.
