Developed with love by KnpLabs Hire us for your project!
17

monolog-autowire-bundle

by adrenalinkin

Provides autowiring for the loggers registered by symfony/monolog-bundle

Monolog Autowire Bundle На Русском

Latest Stable Version
Total Downloads

knpbundles.com

Introduction

The bundle provides access to all channels registered in MonologBundle viaLoggerHandler.
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 LoggerHandler 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:
bash
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.

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"

Access to the required logging channels using the autowire mechanism is now available via LoggerHandler:

<?php declare(strict_types=1);

use Linkin\Bundle\MonologAutowireBundle\Handler\LoggerHandler;

class AcmeLoggerAware
{
    /**
     * @var LoggerHandler
     */
    private $loggerHandler;

    /**
     * @param LoggerHandler $loggerHandler
     */
    public function __construct(LoggerHandler $loggerHandler) 
    {
        $this->loggerHandler = $loggerHandler;
    }

    public function doSome(): void
    {
        $this->loggerHandler->getLogger('acme_channel')->info('INFO into "acme_channel" channel');
        $this->loggerHandler->getLogger('doctrine')->info('INFO into "doctrine" channel');
        $this->loggerHandler->getLogger()->info('INFO into Fallback or into NullLogger');
    }
}

License

license

Copyright (c) Viktor Linkin since 2019

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.