adrenalinkin / monolog-autowire-bundle
Provides autowiring for the loggers registered by symfony/monolog-bundle
Installs: 28
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ~7.1
- psr/log: ^1.0
- symfony/config: ^3.4||^4.0
- symfony/dependency-injection: ^3.4||^4.0
- symfony/finder: ^3.4||^4.0
- symfony/http-kernel: ^3.4||^4.0
Suggests
- symfony/monolog-bundle: Install and enable bundle to activate main functionality of the current bundle
This package is auto-updated.
Last update: 2024-12-09 03:38:04 UTC
README
Introduction
Bundle provides the ability to connect loggers registered in MonologBundle
through the standard autowire
mechanism.
The goal is achieved thanks to auto-generated classes of loggers. Each class decorates one object of one
of the registered monolog
channel.
Also available is the second way to achieve the goal - using the LoggerCollection
.
If the requested channel does not exist - will be selected fallback logger
.
As fallback logger
will be used service, which referenced by @Psr\Log\LoggerInterface
.
In that case where logger
was not registered in service container will be returned instance of Psr\Log\NullLogger
.
Important: Bundle will work properly in the absence of MonologBundle
in the project.
In that case LoggerCollection
will always return a fallback value.
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require adrenalinkin/monolog-autowire-bundle
is command requires you to have Composer install globally.
Step 2: Enable the Bundle
Then, enable the bundle by updating your app/AppKernel.php
file to enable the bundle:
<?php declare(strict_types=1); // app/AppKernel.php class AppKernel extends Kernel { // ... public function registerBundles() { $bundles = [ // ... new Linkin\Bundle\MonologAutowireBundle\LinkinMonologAutowireBundle(), ]; return $bundles; } // ... }
Configuration
To start using bundle you don't need to define some additional configuration. All parameters has values by default:
linkin_monolog_autowire: # directory where should be stored auto-generated loggers decorators loggers_dir: '%kernel.project_dir%/var/loggers' # path to loggers decorator template decorator_template: 'ChannelLogger.php.dist'
Usage
Suppose our project has the following configuration MonologBundle
:
monolog: handlers: doctrine: type: stream path: "%kernel.logs_dir%/%kernel.environment%.doctrine.log" level: info channels: - "doctrine" acme: type: stream path: "%kernel.logs_dir%/%kernel.environment%.acme_channel.log" level: info channels: - "acme_channel"
Use through auto-generated loggers
Class names are generated based on the channel name. All non-alphanumeric values are deleted,
and the name is converted to the format of CamelCase
. All classes begin with Channel
and end with Logger
.
<?php declare(strict_types=1); use Linkin\Bundle\MonologAutowireBundle\Logger\ChannelAcmeLogLogger; use Linkin\Bundle\MonologAutowireBundle\Logger\ChannelDoctrineLogger; use Psr\Log\LoggerInterface; class AcmeLoggerAware { /** * @var ChannelDoctrineLogger */ private $acmeLogLogger; /** * @var ChannelDoctrineLogger */ private $doctrineLogger; /** * @var LoggerInterface */ private $logger; /** * @param ChannelAcmeLogLogger $acmeLogLogger * @param ChannelDoctrineLogger $doctrineLogger * @param LoggerInterface $logger */ public function __construct( ChannelAcmeLogLogger $acmeLogLogger, ChannelDoctrineLogger $doctrineLogger, LoggerInterface $logger ) { $this->acmeLogLogger = $acmeLogLogger; $this->doctrineLogger = $doctrineLogger; $this->logger = $logger; } public function doSome(): void { $this->acmeLogLogger->info('INFO into "acme_log" channel'); $this->doctrineLogger->info('INFO into "doctrine" channel'); $this->logger->info('INFO into Fallback or into NullLogger'); } }
Use through collection of loggers
<?php declare(strict_types=1); use Linkin\Bundle\MonologAutowireBundle\Collection\LoggerCollection; use Psr\Log\LoggerInterface; class AcmeLoggerAware { /** * @var LoggerInterface */ private $acmeLogLogger; /** * @var LoggerInterface */ private $doctrineLogger; /** * @var LoggerInterface */ private $logger; /** * @param LoggerCollection $loggerCollection */ public function __construct(LoggerCollection $loggerCollection) { $this->acmeLogLogger = $loggerCollection->getLogger('acme_log'); $this->doctrineLogger = $loggerCollection->getLogger('doctrine'); $this->logger = $loggerCollection->getLogger(); } public function doSome(): void { $this->acmeLogLogger->info('INFO into "acme_log" channel'); $this->doctrineLogger->info('INFO into "doctrine" channel'); $this->logger->info('INFO into Fallback or into NullLogger'); } }