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

Logger Aware Bundle Laravel Package

divbyzero/logger-aware-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require divbyzero/logger-aware-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        DivByZero\LoggerAwareBundle\LoggerAwareBundle::class => ['all' => true],
    ];
    
  2. Basic Usage:

    • Annotate any service class with @LoggerAware to auto-inject a logger:
      use DivByZero\LoggerAwareBundle\Annotation\LoggerAware;
      
      class MyService
      {
          #[LoggerAware]
          private $logger;
      
          public function doSomething()
          {
              $this->logger->info('Action performed');
          }
      }
      
  3. First Use Case:

    • Replace manual logger instantiation (e.g., $this->logger = $this->container->get('logger')) with the annotation.
    • Works seamlessly with Symfony’s dependency injection (DI).

Implementation Patterns

Common Workflows

  1. Service-Level Logging:

    • Apply @LoggerAware to services handling business logic (e.g., UserService, PaymentGateway).
    • Example:
      class OrderService
      {
          #[LoggerAware]
          private $logger;
      
          public function createOrder(Order $order)
          {
              $this->logger->debug('Order creation started', ['order_id' => $order->id]);
              // ...
          }
      }
      
  2. Controller Logging:

    • Useful for tracking API requests/responses:
      class ApiController
      {
          #[LoggerAware]
          private $logger;
      
          public function index(Request $request)
          {
              $this->logger->info('API request', [
                  'path' => $request->getPathInfo(),
                  'method' => $request->getMethod(),
              ]);
              // ...
          }
      }
      
  3. Event Listeners/Subscribers:

    • Auto-inject loggers into event handlers:
      class UserRegisteredListener
      {
          #[LoggerAware]
          private $logger;
      
          public function __invoke(UserRegisteredEvent $event)
          {
              $this->logger->info('User registered', ['email' => $event->getUser()->email]);
          }
      }
      

Integration Tips

  • Custom Logger Namespaces: Configure logger channels per service via config/packages/divbyzero_logger_aware.yaml:
    divbyzero_logger_aware:
        services:
            App\Service\MyService: 'app.logger.my_service'
    
  • Leverage Symfony’s Logger Interface: The injected logger adheres to Psr\Log\LoggerInterface, so use standard methods (emergency(), alert(), etc.).
  • Combine with Monolog Handlers: Extend Monolog’s configuration (e.g., monolog.yaml) to route logs to files, databases, or external services.

Gotchas and Tips

Pitfalls

  1. Annotation Processing:

    • Ensure the bundle’s compiler pass runs. If logs aren’t injected, verify:
      • The bundle is enabled in bundles.php.
      • No typos in @LoggerAware (case-sensitive in annotations).
    • Debug with:
      php bin/console debug:container | grep logger
      
  2. Circular Dependencies:

    • Avoid injecting loggers into services that are also logger providers (e.g., a LoggerFactory service).
    • Use private properties to prevent accidental overrides.
  3. Performance Overhead:

    • Minimal, but avoid overusing in high-frequency methods (e.g., loop iterations). Cache logger instances if needed.

Debugging

  • Verify Injection: Dump the service container to check logger binding:
    php bin/console debug:container App\Service\MyService
    
  • Log Level Mismatches: Ensure your Monolog handlers are configured to capture the log level you’re using (e.g., debug vs. info).

Extension Points

  1. Custom Annotations: Extend the bundle to support additional metadata (e.g., log level hints):

    #[LoggerAware(level: 'debug')]
    private $logger;
    

    (Requires bundle customization; check Annotation/LoggerAware.php.)

  2. Dynamic Logger Names: Use a callback to generate logger names dynamically:

    divbyzero_logger_aware:
        services:
            App\Service\DynamicService: '@=service("logger").createLogger("dynamic.'.container.get('request_stack')->getCurrentRequest()->getPathInfo().'")'
    
  3. Integration with Other Bundles:

    • Works with SensioFrameworkExtraBundle for controller logging.
    • Pair with NelmioApiDocBundle to log API documentation generation.
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.
croct/coding-standard
croct/plug-php
nqxcode/phpmorphy
boundwize/pyrameter
develia/commons
dmstr/symfony-system-resources-bundle
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
renatomarinho/laravel-page-speed
develia/geo-bundle
austinheap/laravel-database-encryption
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php