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

Getting Started

Minimal Setup

  1. Installation:

    composer require adrenalinkin/monolog-autowire-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        Adrenalinkin\MonologAutowireBundle\MonologAutowireBundle::class => ['all' => true],
    ];
    
  2. First Use Case: Autowire a channel-specific logger in a service:

    use Psr\Log\LoggerInterface;
    
    class MyService
    {
        public function __construct(
            private LoggerInterface $myChannelLogger // Autowired by bundle
        ) {}
    }
    

    Ensure myChannelLogger is registered in monolog config (e.g., config/packages/monolog.yaml):

    monolog:
        channels:
            - my_channel
    

Implementation Patterns

Workflow: Channel-Specific Logging

  1. Define Channels: Configure channels in monolog.yaml:

    monolog:
        channels:
            - app
            - security
            - database
    
  2. Autowire Loggers:

    class SecurityService
    {
        public function __construct(
            private LoggerInterface $securityLogger // Autowired
        ) {}
    }
    
  3. Fallback Handling: Use LoggerCollection for dynamic channel resolution:

    use Adrenalinkin\MonologAutowireBundle\Logger\LoggerCollection;
    
    class DynamicLoggerService
    {
        public function __construct(
            private LoggerCollection $loggerCollection
        ) {}
    
        public function log(string $channel, string $message)
        {
            $logger = $this->loggerCollection->get($channel);
            $logger->info($message);
        }
    }
    

Integration Tips

  • Symfony Flex: Works seamlessly with Symfony 4.4+.
  • Custom Handlers: Extend existing channels by adding handlers to monolog.yaml:
    monolog:
        handlers:
            my_channel:
                type: stream
                path: "%kernel.logs_dir%/my_channel.log"
                channels: [my_channel]
    
  • Dependency Injection: Prefer autowiring over manual service lookup for type safety.

Gotchas and Tips

Pitfalls

  1. Missing Channels: Autowiring a logger for an unregistered channel throws ParameterNotFoundException. Use LoggerCollection for fallback:

    $logger = $loggerCollection->get('nonexistent_channel'); // Returns PSR NullLogger
    
  2. MonologBundle Dependency: The bundle works without MonologBundle, but LoggerCollection defaults to NullLogger if no channels exist.

  3. Circular Dependencies: Avoid circular references between services requiring the same channel logger. Use LoggerCollection to break cycles.

Debugging

  • Service Dump: Check registered loggers with:
    php bin/console debug:container | grep logger
    
  • Channel Validation: Verify channels in monolog.yaml match autowired logger names (case-sensitive).

Extension Points

  1. Custom Logger Factories: Override logger generation via monolog_autowire.logger_factory service:

    services:
        monolog_autowire.logger_factory:
            class: App\Logger\CustomLoggerFactory
            arguments: ['@monolog.logger']
    
  2. Logger Collection Fallback: Change the fallback logger by binding Psr\Log\LoggerInterface to a custom service.

  3. Dynamic Channel Registration: Register channels programmatically via Monolog\Registry:

    $registry = $container->get('monolog.registry');
    $registry->addLogger('dynamic_channel', $logger);
    
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