maatify/slim-logger

Slim-compatible structured logger with PSR-7 request support

1.0.6 2025-04-18 13:16 UTC

This package is auto-updated.

Last update: 2025-04-18 13:17:32 UTC


README

Maatify.dev

Current version Packagist PHP Version Support Monthly Downloads Total Downloads Stars

Tests

Maatify Slim Logger is a lightweight, PSR-7 compatible, Slim-friendly structured logger for PHP applications. It is part of the modular Maatify ecosystem.

๐Ÿš€ Features

  • โœ… PSR-7 request-aware logging (integrates with Slim Framework)
  • โœ… Uses LogLevelEnum (PHP 8.1+)
  • โœ… Logs to file in JSON format
  • โœ… File names include log level (_response_error_...)
  • โœ… Static access with Logger::recordStatic()
  • โœ… Automatically creates secure log directories
  • โœ… Works with Slim or pure PHP
  • โœ… GitHub Actions CI ready

๐Ÿ“ฆ Installation

composer require maatify/slim-logger

๐Ÿงฑ Namespaces

  • Logger: Maatify\SlimLogger\Log\Logger
  • Enum: Maatify\SlimLogger\Log\LogLevelEnum
  • Path Helper: Maatify\SlimLogger\Store\File\Path

๐Ÿ“ Folder Structure

maatify-slim-logger/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ Log/
โ”‚       โ”œโ”€โ”€ Logger.php
โ”‚       โ””โ”€โ”€ LogLevelEnum.php
โ”‚   โ””โ”€โ”€ Store/
โ”‚       โ””โ”€โ”€ File/
โ”‚           โ””โ”€โ”€ Path.php

โœ… Basic Usage (Slim or Plain PHP)

use Maatify\SlimLogger\Log\Logger;
use Maatify\SlimLogger\Log\LogLevelEnum;
use Maatify\SlimLogger\Store\File\Path;

$logger = new Logger(new Path(__DIR__));
$logger->record('User login', null, 'user/actions', LogLevelEnum::Info);

๐Ÿ’ก Usage in Slim Route

$app->post('/action', function ($request, $response) use ($logger) {
    $logger->record('User posted action.', $request, 'user/submit', LogLevelEnum::Debug);
    return $response;
});

โš ๏ธ Logging Exceptions

try {
    throw new \Exception('Oops');
} catch (\Throwable $e) {
    $logger->record($e, null, 'errors/runtime', LogLevelEnum::Error);
}

๐Ÿ“ฃ Static Logging

Log from anywhere โ€” no need to instantiate:

use Maatify\SlimLogger\Log\Logger;
use Maatify\SlimLogger\Log\LogLevelEnum;

Logger::recordStatic('Static log entry.', null, 'system/status', LogLevelEnum::Info);

โœ… Static Exception Example

try {
    throw new \Exception("Static exception!");
} catch (\Throwable $e) {
    Logger::recordStatic($e, null, 'errors/fatal', LogLevelEnum::Error);
}

๐Ÿงช (Advanced) Overriding Static Instance for Testing

In PHPUnit or setup environments, use:

$testLogger = new Logger(new Path(__DIR__ . '/logs'));
Logger::setInstance($testLogger);

Then recordStatic() will use that injected instance and path.

๐Ÿ” Log File Naming Example

/logs/24/04/18/user/actions_response_info_20250418AM.log

โš™๏ธ Configuration

Option Default Description
Path project root Base path for log files
Extension .log File extension for log files

๐Ÿงช Run Tests Locally

composer test

๐Ÿš€ GitHub CI Integration

Tests run automatically on push via:

.github/workflows/run-tests.yml

๐Ÿ“„ License

MIT License ยฉ 2025 Maatify.dev

๐Ÿ™‹โ€โ™‚๏ธ Questions?