8000 minor #15278 [Logger] [Monolog] Add ConfigBuilders for Monolog (Nyholm) · symfony/symfony-docs@942541a · GitHub
[go: up one dir, main page]

Skip to content

Commit 942541a

Browse files
committed
minor #15278 [Logger] [Monolog] Add ConfigBuilders for Monolog (Nyholm)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Logger] [Monolog] Add ConfigBuilders for Monolog Only two more PRs after this =) Commits ------- 0fcc3e8 [Monolog] Add ConfigBuilders for Monolog
2 parents d070632 + 0fcc3e8 commit 942541a

10 files changed

+209
-216
lines changed

email.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,11 @@ the report with details of the sent emails.
394394
.. code-block:: php
395395
396396
// config/packages/dev/web_profiler.php
397-
$container->loadFromExtension('web_profiler', [
398-
'intercept_redirects' => 'true',
399-
]);
397+
use Symfony\Config\WebProfilerConfig;
398+
399+
return static function (WebProfilerConfig $webProfiler) {
400+
$webProfiler->interceptRedirects(true);
401+
};
400402
401403
.. tip::
402404

logging.rst

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,22 @@ to write logs using the :phpfunction:`syslog` function:
138138
.. code-block:: php
139139
140140
// config/packages/prod/monolog.php
141-
$container->loadFromExtension('monolog', [
142-
'handlers' => [
143-
// this "file_log" key could be anything
144-
'file_log' => [
145-
'type' => 'stream',
146-
// log to var/logs/(environment).log
147-
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
148-
// log *all* messages (debug is lowest level)
149-
'level' => 'debug',
150-
],
151-
'syslog_handler' => [
152-
'type' => 'syslog',
153-
// log error-level messages and higher
154-
'level' => 'error',
155-
],
156-
],
157-
]);
141+
use Symfony\Config\MonologConfig;
142+
143+
return static function (MonologConfig $monolog) {
144+
// this "file_log" key could be anything
145+
$monolog->handler('file_log')
146+
->type('stream')
147+
// log to var/logs/(environment).log
148+
->path('%kernel.logs_dir%/%kernel.environment%.log')
149+
// log *all* messages (debug is lowest level)
150+
->level('debug');
151+
152+
$monolog->handler('syslog_handler')
153+
->type('syslog')
154+
// log error-level messages and higher
155+
->level('error');
156+
};
158157
159158
This defines a *stack* of handlers and each handler is called in the order that it's
160159
defined.
@@ -236,29 +235,29 @@ one of the messages reaches an ``action_level``. Take this example:
236235
.. code-block:: php
237236
238237
// config/packages/prod/monolog.php
239-
$container->loadFromExtension('monolog', [
240-
'handlers' => [
241-
'filter_for_errors' => [
242-
'type' => 'fingers_crossed',
243-
// if *one* log is error or higher, pass *all* to file_log
244-
'action_level' => 'error',
245-
'handler' => 'file_log',
246-
],
247-
248-
// now passed *all* logs, but only if one log is error or higher
249-
'file_log' => [
250-
'type' => 'stream',
251-
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
252-
'level' => 'debug',
253-
],
254-
255-
// still passed *all* logs, and still only logs error or higher
256-
'syslog_handler' => [
257-
'type' => 'syslog',
258-
'level' => 'error',
259-
],
260-
],
261-
]);
238+
use Symfony\Config\MonologConfig;
239+
240+
return static function (MonologConfig $monolog) {
241+
$monolog->handler('filter_for_errors')
242+
->type('fingers_crossed')
243+
// if *one* log is error or higher, pass *all* to file_log
244+
->actionLevel('error')
245+
->handler('file_log')
246+
;
247+
248+
// now passed *all* logs, but only if one log is error or higher
249+
$monolog->handler('file_log')
250+
->type('stream')
251+
->path('%kernel.logs_dir%/%kernel.environment%.log')
252+
->level('debug')
253+
;
254+
255+
// still passed *all* logs, and still only logs error or higher
256+
$monolog->handler('syslog_handler')
257+
->type('syslog')
258+
->level('error')
259+
;
260+
};
262261
263262
Now, if even one log entry has an ``error`` level or higher, then *all* log entries
264263
for that request are saved to a file via the ``file_log`` handler. That means that
@@ -331,18 +330,17 @@ option of your handler to ``rotating_file``:
331330
.. code-block:: php
332331
333332
// config/packages/prod/monolog.php
334-
$container->loadFromExtension('monolog', [
335-
'handlers' => [
336-
'main' => [
337-
'type' => 'rotating_file',
338-
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
339-
'level' => 'debug',
340-
// max number of log files to keep
341-
// defaults to zero, which means infinite files
342-
'max_files' => 10,
343-
],
344-
],
345-
]);
333+
use Symfony\Config\MonologConfig;
334+
335+
return static function (MonologConfig $monolog) {
336+
$monolog->handler('main')
337+
->type('rotating_file')
338+
->path('%kernel.logs_dir%/%kernel.environment%.log')
339+
->level('debug')
340+
// max number of log files to keep
341+
// defaults to zero, which means infinite files
342+
->maxFiles(10);
343+
};
346344
347345
Using a Logger inside a Service
348346
-------------------------------

logging/channels_handlers.rst

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,19 @@ can do it in any (or all) environments:
7878
.. code-block:: php
7979
8080
// config/packages/prod/monolog.php
81-
$container->loadFromExtension('monolog', [
82-
'handlers' => [
83-
'security' => [
84-
'type' => 'stream',
85-
'path' => '%kernel.logs_dir%/security.log',
86-
'channels' => [
87-
'security',
88-
],
89-
],
90-
'main' => [
91-
// ...
92-
'channels' => [
93-
'!security',
94-
],
95-
],
96-
],
97-
]);
81+
use Symfony\Config\MonologConfig;
82+
83+
return static function (MonologConfig $monolog) {
84+
$monolog->handler('security')
85+
->type('stream')
86+
->path('%kernel.logs_dir%/security.log')
87+
->channels()->elements(['security']);
88+
89+
$monolog->handler('main')
90+
// ...
91+
92+
->channels()->elements(['!security']);
93+
};
9894
9995
.. caution::
10096

@@ -163,12 +159,11 @@ You can also configure additional channels without the need to tag your services
163159
.. code-block:: php
164160
165161
// config/packages/prod/monolog.php
166-
$container->loadFromExtension('monolog', [
167-
'channels' => [
168-
'foo',
169-
'bar',
170-
],
171-
]);
162+
use Symfony\Config\MonologConfig;
163+
164+
return static function (MonologConfig $monolog) {
165+
$monolog->channels(['foo', 'bar']);
166+
};
172167
173168
Symfony automatically registers one service per channel (in this example, the
174169
channel ``foo`` creates a service called ``monolog.logger.foo``). In order to

logging/formatter.rst

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ configure your handler to use it:
2323
2424
.. code-block:: xml
2525
26-
<!-- config/services.xml -->
26+
<!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
2727
<?xml version="1.0" encoding="UTF-8" ?>
2828
<container xmlns="http://symfony.com/schema/dic/services"
2929
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -33,7 +33,6 @@ configure your handler to use it:
3333
http://symfony.com/schema/dic/monolog
3434
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
3535
36-
<!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
3736
<monolog:config>
3837
<monolog:handler
3938
name="file"
@@ -46,16 +45,13 @@ configure your handler to use it:
4645
4746
.. code-block:: php
4847
49-
// config/services.php
50-
use Monolog\Formatter\JsonFormatter;
51-
5248
// config/packages/prod/monolog.php (and/or config/packages/dev/monolog.php)
53-
$container->loadFromExtension('monolog', [
54-
'handlers' => [
55-
'file' => [
56-
'type' => 'stream',
57-
'level' => 'debug',
58-
'formatter' => 'monolog.formatter.json',
59-
],
60-
],
61-
]);
49+
use Symfony\Config\MonologConfig;
50+
51+
return static function (MonologConfig $monolog) {
52+
$monolog->handler('file')
53+
->type('stream')
54+
->level('debug')
55+
->formatter('monolog.formatter.json')
56+
;
57+
};

logging/handlers.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ Then reference it in the Monolog configuration:
8888
8989
// config/packages/prod/monolog.php
9090
use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
91-
92-
$container->loadFromExtension('monolog', [
93-
'handlers' => [
94-
'es' => [
95-
'type' => 'service',
96-
'id' => ElasticsearchLogstashHandler::class,
97-
],
98-
],
99-
]);
91+
use Symfony\Config\MonologConfig;
92+
93+
return static function (MonologConfig $monolog) {
94+
$monolog->handler('es')
95+
->type('service')
96+
->id(ElasticsearchLogstashHandler::class)
97+
;
98+
};
10099
101100
.. _`ELK stack`: https://www.elastic.co/what-is/elk-stack

logging/monolog_console.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ The Monolog console handler is enabled by default:
122122
.. code-block:: php
123123
124124
// config/packages/dev/monolog.php
125-
$container->loadFromExtension('monolog', [
126-
'handlers' => [
127-
'console' => [
128-
'type' => 'console',
129-
'process_psr_3_messages' => false,
130-
'channels' => ['!event', '!doctrine', '!console'],
131-
],
132-
],
133-
]);
125+
use Symfony\Config\MonologConfig;
126+
127+
return static function (MonologConfig $monolog) {
128+
$monolog->handler('console')
129+
->type('console')
130+
->processPsr3Messages(false)
131+
->channels()->elements(['!event', '!doctrine', '!console'])
132+
;
133+
};
134134
135135
Now, log messages will be shown on the console based on the log levels and verbosity.
136136
By default (normal verbosity level), warnings and higher will be shown. But in

0 commit comments

Comments
 (0)
0