Logger component

The Logger component is a decorator for PSR-3 logger implementations that adds convenience methods for exception logging and context enrichment. It wraps any PSR-3 compatible logger and extends its functionality with exception-specific logging methods and the ability to automatically enrich log context from multiple sources.

When working with exceptions, you often need to log them with proper context before either throwing them or suppressing them based on the environment (e.g., development vs. production). This component provides a clean API for these common scenarios while maintaining full compatibility with PSR-3.

Features

  • Exception logging methods: dedicated exception() and throw() methods for logging exceptions with automatic context enrichment.

  • Debug mode support: toggle between logging and throwing exceptions depending on the environment.

  • Context providers: automatically enrich log entries with additional context from multiple sources (e.g., request ID, user information, etc.).

  • PSR-3 compatible: fully implements PSR-3 LoggerInterface and can wrap any PSR-3 logger.

  • Configurable default log level: set the default severity level for exception logging.

  • Symfony ready: via dedicated runopencode/logger-bundle package, see Logger Bundle for integration details.

Table of Contents

Quick example

Basic usage of the Logger component to log exceptions with custom context:

 1<?php
 2
 3declare(strict_types=1);
 4
 5namespace App\Service;
 6
 7use Psr\Log\LoggerInterface as PsrLoggerInterface;
 8use RunOpenCode\Component\Logger\Contract\LoggerInterface;
 9use RunOpenCode\Component\Logger\Logger;
10
11final readonly class PaymentProcessor
12{
13    public function __construct(private LoggerInterface $logger)
14    {
15        // noop.
16    }
17
18    public function processPayment(Payment $payment): void
19    {
20        try {
21            // Process payment logic
22            $this->gateway->charge($payment);
23        } catch (\Throwable $exception) {
24            // Log the exception with additional context
25            $this->logger->exception(
26                $exception,
27                'Payment processing failed',
28                [
29                    'payment_id' => $payment->getId(),
30                    'amount' => $payment->getAmount(),
31                    'currency' => $payment->getCurrency(),
32                ]
33            );
34            // ...
35        }
36    }
37}

In the example above, if an exception occurs during payment processing, it will be logged with the custom message and context, including payment details. The exception() method logs the exception but does not re-throw it (unless debug mode is enabled), allowing you to handle the error yourself.

For cases where you want to both log and throw the exception, use the throw() method instead:

 1<?php
 2
 3public function criticalOperation(): void
 4{
 5    try {
 6        $this->performOperation();
 7    } catch (\Throwable $exception) {
 8        // Log and re-throw the exception
 9        $this->logger->throw(
10            $exception,
11            'Critical operation failed',
12            ['operation' => 'critical']
13        );
14    }
15}

See Usage for more detailed examples and Context providers to learn how to automatically enrich all log entries with additional context.

For Symfony framework integration, see the Logger Bundle documentation.