maatify / slim-logger
Slim-compatible structured logger with PSR-7 request support
1.0.6
2025-04-18 13:16 UTC
Requires
- php: >=8.2.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10
README
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?
- GitHub: github.com/maatify/slim-logger