maatify / psr-logger
PSR-3 compatible logger with dynamic file naming and hourly rotation, powered by Monolog
Installs: 5
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/maatify/psr-logger
Requires
- php: ^8.4
- monolog/monolog: ^3.0
- psr/log: ^3.0
- vlucas/phpdotenv: ^5.6
README
π§Ύ maatify/psr-logger
A PSR-3 compatible logger powered by Monolog, supporting dynamic file naming, hourly log rotation, and project-aware path detection. Built for professional PHP projects that need organized, flexible, and standards-compliant logging.
π¦ Installation
Install via Composer:
composer require maatify/psr-logger
βοΈ Features
- β
PSR-3 compatible (
Psr\Log\LoggerInterface) - β Built on Monolog v3
- β
Smart file rotation by date/hour (
Y/m/d/H) - β Dynamic file naming by class or custom context
- β Works both standalone or inside any Composer project
- β Optional log cleanup (manual or via cron)
- β Zero configuration β auto-detects project root
π§© Usage
Basic Example
use Maatify\PsrLogger\LoggerFactory; $logger = LoggerFactory::create('services/payment'); $logger->info('Payment started', ['order_id' => 501]);
Resulting file:
storage/logs/2025/11/05/10/services/payment.log
Auto Context Detection
Automatically uses the calling class name as the log context:
use Maatify\PsrLogger\Traits\LoggerContextTrait; class UserService { use LoggerContextTrait; public function __construct() { $this->initLogger(); // auto: logs/UserService.log $this->logger->info('User service initialized'); } }
π§± Folder Structure
maatify-psr-logger/
βββ src/
β βββ Config/
β β βββ LoggerConfig.php
β βββ Helpers/
β β βββ PathHelper.php
β βββ Rotation/
β β βββ LogCleaner.php
β βββ Traits/
β β βββ LoggerContextTrait.php
β βββ LoggerFactory.php
βββ scripts/
β βββ clean_logs.php
βββ .env.example
βββ composer.json
βββ README.md
π§Ή Log Cleanup (manual or cron)
You can run the provided script to delete old log files manually or via cron.
1. Manual Cleanup
php vendor/maatify/psr-logger/scripts/clean_logs.php
2. Composer Shortcut
Add to your projectβs composer.json:
"scripts": {
"logs:clean": "php vendor/maatify/psr-logger/scripts/clean_logs.php"
}
Then run:
composer logs:clean
3. Cron Job Example
0 3 * * * php /var/www/project/vendor/maatify/psr-logger/scripts/clean_logs.php >> /var/log/log_cleanup.log 2>&1
βοΈ Environment Variables
| Variable | Default | Description |
|---|---|---|
LOG_PATH |
storage/logs |
Base directory for log files |
LOG_RETENTION_DAYS |
14 |
Number of days to keep logs (used by cleanup) |
Example .env file:
LOG_PATH=storage/logs LOG_RETENTION_DAYS=14
π§ Project Root Detection
If the library is installed inside:
vendor/maatify/psr-logger
it automatically goes 3 levels up to the project root. If running standalone (during development), it uses its own root directory.
π Integration Examples
πͺΆ Slim Framework
use Maatify\PsrLogger\LoggerFactory; $container->set('logger', function() { return LoggerFactory::create('slim/app'); }); // Example usage inside a route: $app->get('/ping', function ($request, $response) { $this->get('logger')->info('Ping request received'); return $response->write('pong'); });
π§± Laravel
In app/Providers/AppServiceProvider.php:
use Maatify\PsrLogger\LoggerFactory; public function register() { $this->app->singleton('maatify.logger', function () { return LoggerFactory::create('laravel/app'); }); }
Usage anywhere:
app('maatify.logger')->info('Laravel integration working!');
βοΈ Native PHP Project
require __DIR__ . '/vendor/autoload.php'; use Maatify\PsrLogger\LoggerFactory; $logger = LoggerFactory::create('custom/project'); $logger->error('Something went wrong', ['code' => 500]);
π§Ύ License
Youβre free to use, modify, and distribute this library with attribution.