nikolaposa/monolog-factory

Configuration-based Monolog factory

3.0.0 2019-11-30 12:48 UTC

This package is auto-updated.

Last update: 2020-06-23 12:36:58 UTC


README

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

Monolog Factory that allows configuration-based creation of Logger objects.

In addition to the generic factory, this package features one to be used with PSR-11 dependency injection containers.

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require nikolaposa/monolog-factory

Usage

Generic factory

use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use MonologFactory\LoggerFactory;

$loggerFactory = new LoggerFactory();

$logger = $loggerFactory->create('my_logger', [
    'handlers' => [
        [
            'name' => NativeMailerHandler::class,
            'params' => [
                'to' => 'test@example.com',
                'subject' => 'Test',
                'from' => 'noreply@example.com',
                'level' => Logger::ALERT,
            ],
            'formatter' => [
                'name' => HtmlFormatter::class,
            ],
        ],
    ],
    'processors' => [
        [
            'name' => PsrLogMessageProcessor::class,
        ],
    ],
]);

DI container factory configuration

use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use MonologFactory\DiContainerLoggerFactory;

return [
    'logger' => [
        'logger1' => [
            'name' => 'logger1',
            'handlers' => [
                [
                    'name' => NativeMailerHandler::class,
                    'params' => [
                        'to' => 'test@example.com',
                        'subject' => 'Test',
                        'from' => 'noreply@example.com',
                        'level' => Logger::ALERT,
                    ],
                    'formatter' => [
                        'name' => HtmlFormatter::class,
                    ],
                ],
            ],
            'processors' => [
                [
                    'name' => PsrLogMessageProcessor::class,
                ],
            ],
        ],
        'logger2' => [
            'name' => 'logger2',
            'handlers' => [
                [
                    'name' => BufferHandler::class,
                    'params' => [
                        'handler' => [
                            '__class__' => NativeMailerHandler::class,
                            'to' => 'test@example.com',
                            'subject' => 'Test',
                            'from' => 'noreply@example.com',
                        ],
                        'buffer_limit' => 5,
                    ],
                    'processors' => [
                        [
                            'name' => MemoryUsageProcessor::class,
                        ],
                    ],
                ],
            ],
            'processors' => [
                [
                    'name' => PsrLogMessageProcessor::class,
                ],
            ],
        ],
    ],
    'di' => [
        'factories' => [
            'Logger1' => new DiContainerLoggerFactory('logger1'),
            //... or more preferred/optimal way:
            'Logger2' => [DiContainerLoggerFactory::class, 'logger2'],
        ],
    ],
];

See more examples.

Credits

License

Released under MIT License - see the License File for details.