|
| 1 | +============= |
| 2 | +Logger Bundle |
| 3 | +============= |
| 4 | + |
| 5 | +The Logger Bundle provides seamless integration of the Logger component with |
| 6 | +the Symfony framework. This bundle automatically configures the Logger as a |
| 7 | +service and provides configuration options for customizing its behavior. |
| 8 | + |
| 9 | +The Logger component is a decorator for PSR-3 logger implementations that adds |
| 10 | +convenience methods for exception logging and context enrichment. See the |
| 11 | +:doc:`../../components/logger/index` for details about the Logger component |
| 12 | +itself. |
| 13 | + |
| 14 | +Installation |
| 15 | +------------ |
| 16 | + |
| 17 | +To use the Logger component in a Symfony application, install the bundle: |
| 18 | + |
| 19 | +.. code-block:: console |
| 20 | +
|
| 21 | + composer require runopencode/logger-bundle |
| 22 | +
|
| 23 | +The bundle will be automatically registered in your ``config/bundles.php`` if |
| 24 | +you're using Symfony Flex. |
| 25 | + |
| 26 | +Configuration |
| 27 | +------------- |
| 28 | + |
| 29 | +The bundle provides several configuration options that can be set in your |
| 30 | +``config/packages/runopencode_logger.yaml`` file: |
| 31 | + |
| 32 | +.. code-block:: yaml |
| 33 | +
|
| 34 | + runopencode_logger: |
| 35 | + debug: '%kernel.debug%' |
| 36 | + default_log_level: 'error' |
| 37 | +
|
| 38 | +Configuration options |
| 39 | +~~~~~~~~~~~~~~~~~~~~~ |
| 40 | + |
| 41 | +* ``debug`` (boolean, default: ``false``): Enable debug mode. When enabled, the |
| 42 | + ``exception()`` method will throw exceptions after logging them. This is |
| 43 | + useful during development. Typically set to ``%kernel.debug%`` to match |
| 44 | + Symfony's debug mode. |
| 45 | + |
| 46 | +* ``default_log_level`` (string, default: ``'critical'``): The default log level |
| 47 | + used for exception logging when no level is explicitly specified. Valid values |
| 48 | + are: ``emergency``, ``alert``, ``critical``, ``error``, ``warning``, |
| 49 | + ``notice``, ``info``, ``debug``. |
| 50 | + |
| 51 | +Using the Logger service |
| 52 | +------------------------- |
| 53 | + |
| 54 | +Once the bundle is installed and configured, the Logger service is automatically |
| 55 | +available for dependency injection. Inject it using the interface: |
| 56 | + |
| 57 | +.. code-block:: php |
| 58 | + :linenos: |
| 59 | +
|
| 60 | + <?php |
| 61 | +
|
| 62 | + declare(strict_types=1); |
| 63 | +
|
| 64 | + namespace App\Service; |
| 65 | +
|
| 66 | + use RunOpenCode\Component\Logger\Contract\LoggerInterface; |
| 67 | +
|
| 68 | + final readonly class UserService |
| 69 | + { |
| 70 | + public function __construct(private LoggerInterface $logger) |
| 71 | + { |
| 72 | + // noop. |
| 73 | + } |
| 74 | +
|
| 75 | + public function registerUser(array $userData): User |
| 76 | + { |
| 77 | + try { |
| 78 | + $user = $this->repository->create($userData); |
| 79 | + $this->mailer->sendWelcomeEmail($user); |
| 80 | + |
| 81 | + return $user; |
| 82 | + } catch (\Throwable $exception) { |
| 83 | + $this->logger->exception( |
| 84 | + $exception, |
| 85 | + 'User registration failed', |
| 86 | + ['email' => $userData['email'] ?? 'unknown'] |
| 87 | + ); |
| 88 | + // ... |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +
|
| 93 | +The Logger is automatically configured with Symfony's default logger (the |
| 94 | +``logger`` service). |
| 95 | + |
| 96 | +Automatic context provider registration |
| 97 | +---------------------------------------- |
| 98 | + |
| 99 | +One of the most powerful features of the bundle is automatic registration of |
| 100 | +context providers. Any service that implements ``LoggerContextInterface`` is |
| 101 | +automatically detected and injected into the Logger. |
| 102 | + |
| 103 | +Creating a context provider |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +Simply create a service that implements the interface: |
| 107 | + |
| 108 | +.. code-block:: php |
| 109 | + :linenos: |
| 110 | +
|
| 111 | + <?php |
| 112 | +
|
| 113 | + declare(strict_types=1); |
| 114 | +
|
| 115 | + namespace App\Logger\Context; |
| 116 | +
|
| 117 | + use RunOpenCode\Component\Logger\Contract\LoggerContextInterface; |
| 118 | + use Symfony\Component\HttpFoundation\RequestStack; |
| 119 | +
|
| 120 | + final readonly class RequestContextProvider implements LoggerContextInterface |
| 121 | + { |
| 122 | + public function __construct(private RequestStack $requestStack) |
| 123 | + { |
| 124 | + // noop. |
| 125 | + } |
| 126 | +
|
| 127 | + public function get(array $current): array |
| 128 | + { |
| 129 | + $request = $this->requestStack->getCurrentRequest(); |
| 130 | +
|
| 131 | + if (null === $request) { |
| 132 | + return []; |
| 133 | + } |
| 134 | +
|
| 135 | + return [ |
| 136 | + 'request_id' => $request->headers->get('X-Request-ID') ?? uniqid(), |
| 137 | + 'request_uri' => $request->getRequestUri(), |
| 138 | + 'request_method' => $request->getMethod(), |
| 139 | + 'user_agent' => $request->headers->get('User-Agent'), |
| 140 | + ]; |
| 141 | + } |
| 142 | + } |
| 143 | +
|
| 144 | +If you have services autoconfiguration enabled (which is the default in Symfony), |
| 145 | +this service will be automatically registered and tagged with |
| 146 | +``runopencode.logger.context_provider``. No additional configuration is needed! |
| 147 | + |
| 148 | +Manual registration |
| 149 | +~~~~~~~~~~~~~~~~~~~ |
| 150 | + |
| 151 | +If you're not using autoconfiguration or want to explicitly register a context |
| 152 | +provider, you can do so in your services configuration: |
| 153 | + |
| 154 | +.. code-block:: yaml |
| 155 | +
|
| 156 | + services: |
| 157 | + App\Logger\Context\RequestContextProvider: |
| 158 | + tags: |
| 159 | + - { name: 'runopencode.logger.context_provider' } |
| 160 | +
|
| 161 | +Best practices |
| 162 | +-------------- |
| 163 | + |
| 164 | +1. **Use the interface**: Always inject ``LoggerInterface`` from the Logger |
| 165 | + component, not the concrete ``Logger`` class or PSR-3's ``LoggerInterface``. |
| 166 | + |
| 167 | +2. **Configure per environment**: Use different configuration files for dev, |
| 168 | + test, and prod environments to adjust debug mode and log levels. |
| 169 | + |
| 170 | +3. **Create focused context providers**: Each context provider should have a |
| 171 | + single responsibility (e.g., one for request data, one for user data, etc.). |
| 172 | + |
| 173 | +4. **Keep providers lightweight**: Context providers are called on every log |
| 174 | + entry, so avoid expensive operations. |
| 175 | + |
| 176 | +See the :doc:`../../components/logger/context-providers` documentation for more |
| 177 | +information about creating and using context providers. |
0 commit comments