mafin / simple-logger
A simple PSR-3 compliant logging utility for PHP.
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mafin/simple-logger
Requires
- php: ^8.4
- psr/log: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.89
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.4
- slevomat/coding-standard: ^8.24
- symplify/easy-coding-standard: ^12.6
This package is auto-updated.
Last update: 2025-12-05 11:42:03 UTC
README
A simple, lightweight, and extensible PSR-3 compliant logging library for PHP 8.4+, built with SOLID principles and Dependency Injection support.
Features
- โ PSR-3 Compliant - Fully implements PSR-3 LoggerInterface
- ๐๏ธ SOLID Architecture - Clean separation of concerns with interfaces
- ๐ Dependency Injection - Easily extensible with custom writers and formatters
- ๐ Thread-Safe - File locking for concurrent writes
- ๐ฏ Context Interpolation - Replace placeholders with context values
- ๐ฆ Zero Dependencies - Only requires
psr/log - โจ Type-Safe - Full PHP 8.3+ type declarations
- ๐งช Well Tested - 100% test coverage with PHPUnit
- ๐ Static Analysis - PHPStan level max compliant
Installation
Install via Composer:
composer require mafin/simple-logger
Requirements
- PHP 8.4 or higher
- psr/log ^3.0
Quick Start
Basic Usage
use Mafin\SimpleLogger\Logger; use Psr\Log\LogLevel; // Create a logger that writes to a file $logger = new Logger('app.log', LogLevel::DEBUG); // Log messages at different levels $logger->info('This is an info message.'); $logger->error('This is an error message.'); $logger->debug('Debug information');
Context Interpolation
$logger->info('User {username} logged in from {ip}', [ 'username' => 'john_doe', 'ip' => '192.168.1.1', ]); // Output: [2025-11-05 12:34:56] [info] User john_doe logged in from 192.168.1.1
Dependency Injection
use Mafin\SimpleLogger\Infrastructure\FileLogWriter; use Mafin\SimpleLogger\Infrastructure\DefaultLogFormatter; use Mafin\SimpleLogger\Logger; use Psr\Log\LogLevel; // Custom writer $writer = new FileLogWriter('logs/app.log'); // Custom formatter (optional) $formatter = new DefaultLogFormatter(); // Inject dependencies $logger = new Logger($writer, LogLevel::INFO, $formatter);
Custom Writer
Implement your own log writer (database, API, etc.):
use Mafin\SimpleLogger\Contract\LogWriterInterface; class DatabaseLogWriter implements LogWriterInterface { public function __construct(private PDO $pdo) {} public function write(string $message): void { $stmt = $this->pdo->prepare('INSERT INTO logs (message) VALUES (?)'); $stmt->execute([$message]); } } $logger = new Logger(new DatabaseLogWriter($pdo), LogLevel::INFO);
Custom Formatter
use Mafin\SimpleLogger\Contract\LogFormatterInterface; class JsonFormatter implements LogFormatterInterface { public function format(string $level, string|\Stringable $message, array $context = []): string { return json_encode([ 'timestamp' => time(), 'level' => $level, 'message' => (string) $message, 'context' => $context, ]) . PHP_EOL; } } $logger = new Logger('app.log', LogLevel::DEBUG, new JsonFormatter());
Supported Log Levels
All PSR-3 log levels are supported:
emergency- System is unusablealert- Action must be taken immediatelycritical- Critical conditionserror- Error conditionswarning- Warning conditionsnotice- Normal but significant conditioninfo- Informational messagesdebug- Debug-level messages
Architecture
The library follows SOLID principles with clean separation:
src/
โโโ Contract/ # Interfaces (abstraction layer)
โ โโโ LogWriterInterface.php
โ โโโ LogFormatterInterface.php
โโโ Infrastructure/ # Concrete implementations
โ โโโ FileLogWriter.php
โ โโโ DefaultLogFormatter.php
โโโ Logger.php # Main logger orchestration
Development
Running Tests
composer test
Code Style Check
composer ecs
Static Analysis
composer phpstan
All Checks
composer check
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
Credits
Security
If you discover any security related issues, please email petr.enin@gmail.com instead of using the issue tracker.