Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Monolog Autowire Bundle Laravel Package

adrenalinkin/monolog-autowire-bundle

Symfony bundle that enables autowiring of Monolog channel loggers via generated decorator classes, plus a LoggerCollection fallback mechanism. Works even without MonologBundle: missing channels fall back to the default PSR-3 logger or NullLogger.

View on GitHub
Deep Wiki
Context7

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

Latest Stable Version Total Downloads

knpbundles.com

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');
    }
}

License

license

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony