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

1.0.0 2025-11-05 07:41 UTC

This package is auto-updated.

Last update: 2025-11-05 22:50:35 UTC


README

Current Version Packagist PHP Version Support Monthly Downloads Total Downloads License GitHub Workflow Status Code Quality

🧾 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

MIT license Β© Maatify.dev

You’re free to use, modify, and distribute this library with attribution.