memran/marwa-logger

Lightweight PSR-3 JSON logger for PHP with redaction, rotation, and retention.

Maintainers

Package info

github.com/memran/marwa-logger

pkg:composer/memran/marwa-logger

Statistics

Installs: 155

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-03-28 14:18 UTC

This package is auto-updated.

Last update: 2026-03-28 14:24:33 UTC


README

Packagist Version Packagist Downloads PHP PSR-3 CI PHPStan PHPUnit

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.