memran / marwa-logger
Lightweight PSR-3 JSON logger for PHP with redaction, rotation, and retention.
Requires
- php: >=8.1
- psr/log: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5.62
README
Lightweight PSR-3 JSON logger for PHP 8.1+ with sensitive-data redaction, file rotation, and retention cleanup.
Features
- โ
PSR-3 compatible
SimpleLogger - ๐งพ NDJSON output for easy ingestion by log processors
- ๐ Recursive sensitive-data filtering
- ๐ฆ Production gating by log level and request origin
- ๐๏ธ File rotation by size
- โป๏ธ Optional retention by age and file count
- ๐งช Composer scripts, PHPUnit, PHPStan, and CI included
๐ฆ Installation
composer require memran/marwa-logger
๐ Quick Start
<?php require __DIR__ . '/vendor/autoload.php'; use Marwa\Logger\Logger; $logger = Logger::boot( channel: 'payments', env: 'prod', path: __DIR__ . '/storage/logs', size: '10MB', ); $logger->info('checkout_started', ['user_id' => 42]); $logger->error('payment_failed', ['_origin' => 'system', 'order_id' => 991]);
Logger::boot() defaults to APP_ENV and storage/logs when env and path are omitted.
โ๏ธ Advanced File Sink Configuration
Use StorageFactory when you need retention controls:
use Marwa\Logger\SimpleLogger; use Marwa\Logger\Storage\StorageFactory; use Marwa\Logger\Support\SensitiveDataFilter; use Psr\Log\LogLevel; $sink = StorageFactory::make([ 'driver' => 'file', 'path' => __DIR__ . '/storage/logs', 'prefix' => 'myapp', 'max_bytes' => '10MB', 'retention_days' => 7, 'max_files' => 30, ]); $logger = new SimpleLogger( appName: 'myapp', env: 'production', sink: $sink, filter: new SensitiveDataFilter(), logging: true, productionMinLevel: LogLevel::ERROR, );
๐ง Runtime Behavior
In production or prod, user-origin logs are skipped unless ['_origin' => 'system'] is set. Levels below productionMinLevel are ignored. Sensitive keys such as password, token, authorization, and client_secret are redacted recursively.
Log records include timestamps, request metadata, PHP runtime details, message text, request IDs, and scrubbed context. Invalid UTF-8 is sanitized before encoding so log writes do not silently fail.
๐๏ธ File Rotation and Retention
Active files use the pattern prefix-YYYY-MM-DD.log. When max_bytes is exceeded, the current file is renamed to prefix-YYYY-MM-DD_HHMMSS-<token>.log and a fresh active file is created. If configured, retention_days deletes expired matching files and max_files keeps only the newest matching log files.
๐จ Error Handling
The file sink throws RuntimeException when it cannot create the log directory, rotate a file, or append a record. Treat these as operational failures and surface them in deployment monitoring.
๐ ๏ธ Development
composer lint
composer analyze
composer test
composer check
composer check runs syntax checks, PHPStan 2.x, and PHPUnit.