Skip to content

nikolaposa/monolog-factory

Repository files navigation

Monolog Factory

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' => '[email protected]',
                'subject' => 'Test',
                'from' => '[email protected]',
                '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' => '[email protected]',
                        'subject' => 'Test',
                        'from' => '[email protected]',
                        'level' => Logger::ALERT,
                    ],
                    'formatter' => [
                        'name' => HtmlFormatter::class,
                    ],
                ],
            ],
            'processors' => [
                [
                    'name' => PsrLogMessageProcessor::class,
                ],
            ],
        ],
        'logger2' => [
            'name' => 'logger2',
            'handlers' => [
                [
                    'name' => BufferHandler::class,
                    'params' => [
                        'handler' => [
                            '__class__' => NativeMailerHandler::class,
                            'to' => '[email protected]',
                            'subject' => 'Test',
                            'from' => '[email protected]',
                        ],
                        '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.

About

🏭 Configuration-based Monolog logger factory.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages