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

v1.0.0 2025-11-05 11:27 UTC

This package is auto-updated.

Last update: 2025-12-05 11:42:03 UTC


README

PHP Version PSR-3 License

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 unusable
  • alert - Action must be taken immediately
  • critical - Critical conditions
  • error - Error conditions
  • warning - Warning conditions
  • notice - Normal but significant condition
  • info - Informational messages
  • debug - 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.