maatify / psr-logger
PSR-3 compatible logger with dynamic file naming and hourly rotation, powered by Monolog
Installs: 248
Dependents: 3
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
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.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.
π§± Built on Monolog
This package is powered by Monolog v3 β
the industry-standard logging library for PHP.
It extends Monolog with automatic path detection, hourly rotation, and project-aware file structure,
making it ideal for scalable and professional PHP applications.
π¦ Installation
Install via Composer:
composer require maatify/psr-logger
π¦ Dependencies
This library relies on:
| Dependency | Purpose | Link |
|---|---|---|
| monolog/monolog | Core logging framework for PHP (v3) | github.com/Seldaek/monolog |
| psr/log | PSR-3 compliant interface for logging interoperability | php-fig.org/psr/psr-3 |
| vlucas/phpdotenv | Environment variable management for dynamic log paths | github.com/vlucas/phpdotenv |
| phpunit/phpunit | Unit testing framework (development only) | phpunit.de |
π§±
maatify/psr-loggerbuilds upon these open-source libraries to provide a unified, context-aware, and developer-friendly logging experience across all Maatify packages.
βοΈ 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 (Class-based Logging)
Automatically detects and uses the calling class name as the logging context β no need to specify it manually.
use Maatify\PsrLogger\Traits\LoggerContextTrait; class UserService { use LoggerContextTrait; public function __construct() { // Option 1οΈβ£ β Initialize once and use via $this->logger $this->initLogger(); // auto context β logs/UserService.log $this->logger->info('User service initialized.'); // Option 2οΈβ£ β (New in v1.0.1) Get the logger instance directly $logger = $this->initLogger('services/user'); $logger->debug('Inline logger usage example.'); } }
Resulting files:
storage/logs/2025/11/05/10/UserService.log
storage/logs/2025/11/05/10/services/user.log
π§± Custom Context Example
You can override the automatic context by specifying a custom path or namespace.
This is useful for grouping logs by module or feature (e.g., services/payment, queue/worker, etc.).
use Maatify\PsrLogger\Traits\LoggerContextTrait; class PaymentProcessor { use LoggerContextTrait; public function __construct() { // Initialize logger with a custom context $this->initLogger('services/payment'); $this->logger->info('Payment processor initialized.'); } public function process(int $orderId): void { // Direct inline usage β available since v1.0.1 $logger = $this->initLogger('services/payment/process'); $logger->info('Processing order', ['order_id' => $orderId]); } }
Resulting file:
storage/logs/2025/11/05/10/services/payment/process.log
β‘ Static Logger for Utility & Bootstrap Classes (New in v1.0.2)
When working with static classes, such as Bootstrap, CronRunner, or FileLockManager,
you can now use the StaticLoggerTrait to obtain PSR-3 loggers without class instantiation.
use Maatify\PsrLogger\Traits\StaticLoggerTrait; final class Bootstrap { use StaticLoggerTrait; public static function init(): void { $logger = self::getLogger('bootstrap/init'); $logger->info('Bootstrap initialized successfully'); } }
Resulting file:
storage/logs/2025/11/10/10/bootstrap/init.log
This makes it easy to integrate consistent logging even inside global initializers, CLI runners, or static utility helpers.
| Mode | Description | Example | Since |
|---|---|---|---|
| Static Trait | For static classes (no instantiation) | self::getLogger('bootstrap/init') |
v1.0.2 |
π§© Summary
| Mode | Description | Example | Since |
|---|---|---|---|
| Factory-based | Create standalone logger | LoggerFactory::create('context') |
v1.0.0 |
| Trait (init-only) | Initializes $this->logger for class-wide use |
$this->initLogger() |
v1.0.0 |
| Trait (return) | Returns logger instance directly for inline use | $logger = $this->initLogger('context') |
v1.0.1 |
π§± 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
πΏ Optional: Environment Initialization
This package ships with vlucas/phpdotenv
to simplify environment configuration management.
The logger itself does not automatically load your
.envfile. You should initialize it at the project level before using the logger.
Example:
use Dotenv\Dotenv; use Maatify\PsrLogger\LoggerFactory; require __DIR__ . '/vendor/autoload.php'; $dotenv = Dotenv::createImmutable(__DIR__); $dotenv->safeLoad(); $logger = LoggerFactory::create('bootstrap/test'); $logger->info('Logger initialized successfully!');
π§ 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]);
π Built Upon
maatify/psr-logger proudly builds upon several mature and industry-standard open-source foundations:
| Library | Description | Usage in Project |
|---|---|---|
| monolog/monolog | Industry-standard PHP logging library (v3) | Core logging engine used for structured logging, channel management, and handler pipeline. |
| psr/log | PSR-3 logging interface | Defines the standardized logger interface used by all Maatify components. |
| vlucas/phpdotenv | Environment configuration manager | Loads and manages environment variables for dynamic log paths and environment detection. |
| phpunit/phpunit | PHP unit testing framework | Powers the automated test suite and CI/CD validation through GitHub Actions. |
Huge thanks to the open-source community for their continued contributions, enabling Maatify to build reliable, scalable, and developer-friendly PHP infrastructure. β€οΈ
π Documentation & Resources
| File | Description |
|---|---|
| π CHANGELOG.md | Version history and release notes |
| π LICENSE | Project license (MIT) |
| π¦ composer.json | Package metadata and dependencies |
| π§± CONTRIBUTING.md | Contribution and coding standards |
| π§Ύ VERSION | Current release version |
πͺͺ License
MIT License Β© Maatify.dev
Youβre free to use, modify, and distribute this library with proper attribution.
π₯ Authors & Credits
This library is part of the Maatify.dev Core Ecosystem, developed and maintained under the technical leadership of:
π€ Mohamed Abdulalim β Backend Lead & Technical Architect
Lead architect of the Maatify Backend Infrastructure, overseeing the architecture, core library design,
and technical standardization across all backend modules within the Maatify ecosystem.
π Maatify.dev | βοΈ mohamed@maatify.dev
π€ Contributors
The Maatify.dev Engineering Team and open-source collaborators who continuously refine, test, and extend
the capabilities of this library across multiple Maatify projects.
π§© This project represents a unified engineering effort led by Mohamed Abdulalim, ensuring that every Maatify backend component adheres to the same secure, consistent, and maintainable foundation.