Skip to content

Commit a138452

Browse files
committed
Created logger bundle
1 parent 7a7128d commit a138452

20 files changed

Lines changed: 1686 additions & 4 deletions

File tree

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
"psr-4": {
4040
"RunOpenCode\\Component\\Bitmask\\": "src/RunOpenCode/Component/Bitmask/src/",
4141
"RunOpenCode\\Component\\Dataset\\": "src/RunOpenCode/Component/Dataset/src/",
42+
"RunOpenCode\\Component\\Logger\\": "src/RunOpenCode/Component/Logger/src/",
4243
"RunOpenCode\\Component\\Metadata\\": "src/RunOpenCode/Component/Metadata/src/",
4344
"RunOpenCode\\Component\\Query\\": "src/RunOpenCode/Component/Query/src/",
45+
"RunOpenCode\\Bundle\\LoggerBundle\\": "src/RunOpenCode/Bundle/LoggerBundle/src/",
4446
"RunOpenCode\\Bundle\\QueryBundle\\": "src/RunOpenCode/Bundle/QueryBundle/src/",
4547
"RunOpenCode\\Bundle\\MetadataBundle\\": "src/RunOpenCode/Bundle/MetadataBundle/src/",
4648
"Monorepo\\": "monorepo/"
@@ -54,8 +56,10 @@
5456
"psr-4": {
5557
"RunOpenCode\\Component\\Bitmask\\Tests\\": "src/RunOpenCode/Component/Bitmask/tests/",
5658
"RunOpenCode\\Component\\Dataset\\Tests\\": "src/RunOpenCode/Component/Dataset/tests/",
59+
"RunOpenCode\\Component\\Logger\\Tests\\": "src/RunOpenCode/Component/Logger/tests/",
5760
"RunOpenCode\\Component\\Metadata\\Tests\\": "src/RunOpenCode/Component/Metadata/tests/",
5861
"RunOpenCode\\Component\\Query\\Tests\\": "src/RunOpenCode/Component/Query/tests/",
62+
"RunOpenCode\\Bundle\\LoggerBundle\\Tests\\": "src/RunOpenCode/Bundle/LoggerBundle/tests/",
5963
"RunOpenCode\\Bundle\\QueryBundle\\Tests\\": "src/RunOpenCode/Bundle/QueryBundle/tests/",
6064
"RunOpenCode\\Bundle\\MetadataBundle\\Tests\\": "src/RunOpenCode/Bundle/MetadataBundle/tests/"
6165
}

docker/php/Dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ ENV PHP_EXTENSION_DIR=/usr/local/lib/php/extensions/no-debug-non-zts-20240924
8787
#####################################################################################
8888

8989
RUN composer global config --no-plugins allow-plugins.infection/extension-installer true && \
90-
composer global require infection/infection && \
91-
composer global require maglnet/composer-require-checker && \
92-
composer global require icanhazstring/composer-unused && \
93-
composer global require phpmetrics/phpmetrics && \
90+
composer global require maglnet/composer-require-checker icanhazstring/composer-unused infection/infection phpmetrics/phpmetrics && \
9491
export PATH=~/.config/composer/vendor/bin:$PATH
9592

9693
#####################################################################################

docs/source/bundles/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ Table of Contents
1414
:maxdepth: 2
1515
:titlesonly:
1616

17+
logger-bundle/index
1718
metadata-bundle/index
1819

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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.

docs/source/components/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ Table of Contents
1818
:titlesonly:
1919

2020
dataset/index
21+
logger/index
2122
metadata/index
2223
query/index

0 commit comments

Comments
 (0)