rnr1721/le7-logger

PSR Logger module for le7 PHP MVC framework or any PHP project

1.0.5 2023-05-20 16:31 UTC

This package is auto-updated.

Last update: 2024-10-20 19:20:58 UTC


README

Requirements

  • PHP 8.1 or higher.
  • Composer 2.0 or higher.

What it can?

  • Log to any destination (need to write similar adapter), by default present file, std and null
  • Write more than one destination (for example in two files similarry or to file and to db)

Installation

composer require rnr1721/le7-logger

Tests

composer test

How use it?

use Core\Logger\LoggerFactoryGeneric;

    $factory = new LoggerFactoryGeneric();

    // $log = $this->factory->logStd(); - for docker etc
    $log = $this->factory->logFile('/home/www/example.com/myproject/log.txt');

    // Use it as any PSR logger
    $log->info('info message');
    $log->critical('alert message');

How use different destinations?

For example, we need to write file to two different destinations

use Core\Logger\LoggerFactoryGeneric;

    $factory = new LoggerFactoryGeneric();

    $path1 = '/home/www/example.com/myproject/log1.txt';
    $path2 = '/home/www/example.com/myproject/log2.txt';

    $log = $this->factory->getLogger();

    $log->addBroadcast(new LoggerRouteFile(['filePath' => $path1]))
            ->addBroadcast(new LoggerRouteFile(['filePath' => $path2]));

    // Use it as any PSR logger
    $log->info('info message');
    $log->critical('alert message');

Different format in file adapter

use Core\Logger\Adapters\LoggerRouteFile;

// ...

    $params = [
        'filePath' => '/path/to/your/log.log',
        'template' => '{date} {level} {message} {context}';
    ];

    $log->addBroadcast(new LoggerRouteFile($params));

or different way

use Core\Logger\Adapters\LoggerRouteFile;

// ...

    $loggerRouteFile = new LoggerRouteFile();
    $loggerRouteFile->setFilePath('/your/file/path')
        ->setTemplate('{date} {level} {message} {context}');

    $log->addBroadcast($loggerRouteFile);