Skip to content

Commit bafb1a8

Browse files
committed
minor #8683 Updated logging/* articles to Symfony 4 (javiereguiluz, weaverryan)
This PR was merged into the 4.0 branch. Discussion ---------- Updated logging/* articles to Symfony 4 Commits ------- 96ce11e fixing typo eee0a11 Updated logging/* articles to Symfony 4
2 parents 9cdb38b + 96ce11e commit bafb1a8

8 files changed

+82
-100
lines changed

logging.rst

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ install the Monolog based logger before using it:
1717
Logging a Message
1818
-----------------
1919

20-
To log a message, fetch the ``logger`` service from the container in
21-
your controller::
20+
If the application uses the :ref:`default services.yaml configuration <service-container-services-load-example>`,
21+
you can get the logger service injecting the ``LoggerInterface`` class::
2222

2323
use Psr\Log\LoggerInterface;
2424

25-
public function indexAction(LoggerInterface $logger)
25+
public function index(LoggerInterface $logger)
2626
{
27-
// alternative way of getting the logger
28-
// $logger = $this->get('logger');
29-
3027
$logger->info('I just got the logger');
3128
$logger->error('An error occurred');
3229

@@ -38,7 +35,7 @@ your controller::
3835
// ...
3936
}
4037

41-
The ``logger`` service has different methods for different logging levels/priorities.
38+
The logger service has different methods for different logging levels/priorities.
4239
You can configure the logger to do different things based on the *level* of a message
4340
(e.g. :doc:`send an email when an error occurs </logging/monolog_email>`).
4441

@@ -47,10 +44,6 @@ See LoggerInterface_ for a list of all of the methods on the logger.
4744
Where Logs are Stored
4845
---------------------
4946

50-
The configuration for *where* logs are stored lives in the specific
51-
:doc:`environment </configuration/environments>` configuration files: ``config_dev.yml``
52-
and ``config_prod.yml``.
53-
5447
By default, log entries are written to the ``var/log/dev.log`` file when you're in
5548
the ``dev`` environment. In the ``prod`` environment, logs are written to ``var/log/prod.log``,
5649
but *only* during a request where an error or high-priority log entry was made
@@ -71,8 +64,8 @@ to different locations (e.g. files, database, Slack, etc).
7164
channel can have its *own* handlers, which means you can store different log
7265
messages in different places. See :doc:`/logging/channels_handlers`.
7366

74-
Symfony pre-configures some basic handlers in the ``config_dev.yml`` and ``config_prod.yml``
75-
files. Check these out for some real-world examples.
67+
Symfony pre-configures some basic handlers in the default ``monolog.yaml``
68+
config files. Check these out for some real-world examples.
7669

7770
This example uses *two* handlers: ``stream`` (to write to a file) and ``syslog``
7871
to write logs using the :phpfunction:`syslog` function:
@@ -81,7 +74,7 @@ to write logs using the :phpfunction:`syslog` function:
8174

8275
.. code-block:: yaml
8376
84-
# app/config/config.yml
77+
# config/packages/monolog.yaml
8578
monolog:
8679
handlers:
8780
# this "file_log" key could be anything
@@ -99,7 +92,7 @@ to write logs using the :phpfunction:`syslog` function:
9992
10093
.. code-block:: xml
10194
102-
<!-- app/config/config.xml -->
95+
<!-- config/packages/monolog.xml -->
10396
<?xml version="1.0" encoding="UTF-8" ?>
10497
<container xmlns="http://symfony.com/schema/dic/services"
10598
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -126,7 +119,7 @@ to write logs using the :phpfunction:`syslog` function:
126119
127120
.. code-block:: php
128121
129-
// app/config/config.php
122+
// config/packages/monolog.php
130123
$container->loadFromExtension('monolog', array(
131124
'handlers' => array(
132125
'file_log' => array(
@@ -157,7 +150,7 @@ one of the messages reaches an ``action_level``. Take this example:
157150

158151
.. code-block:: yaml
159152
160-
# app/config/config.yml
153+
# config/packages/monolog.yaml
161154
monolog:
162155
handlers:
163156
filter_for_errors:
@@ -178,7 +171,7 @@ one of the messages reaches an ``action_level``. Take this example:
178171
179172
.. code-block:: xml
180173
181-
<!-- app/config/config.xml -->
174+
<!-- config/packages/monolog.xml -->
182175
<?xml version="1.0" encoding="UTF-8" ?>
183176
<container xmlns="http://symfony.com/schema/dic/services"
184177
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -211,7 +204,7 @@ one of the messages reaches an ``action_level``. Take this example:
211204
212205
.. code-block:: php
213206
214-
// app/config/config.php
207+
// config/packages/monolog.php
215208
$container->loadFromExtension('monolog', array(
216209
'handlers' => array(
217210
'filter_for_errors' => array(
@@ -271,7 +264,7 @@ option of your handler to ``rotating_file``:
271264

272265
.. code-block:: yaml
273266
274-
# app/config/config_dev.yml
267+
# config/packages/dev/monolog.yaml
275268
monolog:
276269
handlers:
277270
main:
@@ -284,7 +277,7 @@ option of your handler to ``rotating_file``:
284277
285278
.. code-block:: xml
286279
287-
<!-- app/config/config_dev.xml -->
280+
<!-- config/packages/dev/monolog.xml -->
288281
<?xml version="1.0" encoding="UTF-8" ?>
289282
<container xmlns="http://symfony.com/schema/dic/services"
290283
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -308,7 +301,7 @@ option of your handler to ``rotating_file``:
308301
309302
.. code-block:: php
310303
311-
// app/config/config_dev.php
304+
// config/packages/dev/monolog.php
312305
$container->loadFromExtension('monolog', array(
313306
'handlers' => array(
314307
'main' => array(
@@ -325,8 +318,7 @@ option of your handler to ``rotating_file``:
325318
Using a Logger inside a Service
326319
-------------------------------
327320

328-
To use a logger in your own services, add the ``@logger`` service as an argument
329-
of those services. If you want to use a pre-configured logger which uses a
321+
If you want to use in your own services a pre-configured logger which uses a
330322
specific channel (``app`` by default), use the ``monolog.logger`` tag with the
331323
``channel`` property as explained in the
332324
:ref:`Dependency Injection reference <dic_tags-monolog>`.
@@ -344,9 +336,14 @@ Learn more
344336

345337
.. toctree::
346338
:maxdepth: 1
347-
:glob:
348339

349-
logging/*
340+
logging/monolog_regex_based_excludes
341+
logging/monolog_email
342+
logging/channels_handlers
343+
logging/monolog_console
344+
logging/disable_microsecond_precision
345+
logging/formatter
346+
logging/processors
350347

351348
.. _Monolog: https://github.com/Seldaek/monolog
352349
.. _LoggerInterface: https://github.com/php-fig/log/blob/master/Psr/Log/LoggerInterface.php

logging/channels_handlers.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ Switching a Channel to a different Handler
2525

2626
Now, suppose you want to log the ``security`` channel to a different file.
2727
To do this, just create a new handler and configure it to log only messages
28-
from the ``security`` channel. You might add this in ``config.yml`` to log
29-
in all environments, or just ``config_prod.yml`` to happen only in ``prod``:
28+
from the ``security`` channel:
3029

3130
.. configuration-block::
3231

3332
.. code-block:: yaml
3433
35-
# app/config/config.yml
34+
# config/packages/monolog.yaml
3635
monolog:
3736
handlers:
3837
security:
@@ -49,7 +48,7 @@ in all environments, or just ``config_prod.yml`` to happen only in ``prod``:
4948
5049
.. code-block:: xml
5150
52-
<!-- app/config/config.xml -->
51+
<!-- config/packages/monolog.xml-->
5352
<container xmlns="http://symfony.com/schema/dic/services"
5453
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5554
xmlns:monolog="http://symfony.com/schema/dic/monolog"
@@ -76,7 +75,7 @@ in all environments, or just ``config_prod.yml`` to happen only in ``prod``:
7675
7776
.. code-block:: php
7877
79-
// app/config/config.php
78+
// config/packages/monolog.php
8079
$container->loadFromExtension('monolog', array(
8180
'handlers' => array(
8281
'security' => array(
@@ -138,13 +137,13 @@ You can also configure additional channels without the need to tag your services
138137

139138
.. code-block:: yaml
140139
141-
# app/config/config.yml
140+
# config/packages/monolog.yaml
142141
monolog:
143142
channels: ['foo', 'bar']
144143
145144
.. code-block:: xml
146145
147-
<!-- app/config/config.xml -->
146+
<!-- config/packages/monolog.xml -->
148147
<container xmlns="http://symfony.com/schema/dic/services"
149148
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
150149
xmlns:monolog="http://symfony.com/schema/dic/monolog"
@@ -161,7 +160,7 @@ You can also configure additional channels without the need to tag your services
161160
162161
.. code-block:: php
163162
164-
// app/config/config.php
163+
// config/packages/monolog.php
165164
$container->loadFromExtension('monolog', array(
166165
'channels' => array(
167166
'foo',

logging/disable_microsecond_precision.rst

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ log generation. This is recommended for systems that generate a large number of
1111

1212
.. code-block:: yaml
1313
14-
# app/config/config.yml
14+
# config/packages/monolog.yaml
1515
monolog:
1616
use_microseconds: false
17-
handlers:
18-
applog:
19-
type: stream
20-
path: /var/log/symfony.log
21-
level: error
17+
# ...
2218
2319
.. code-block:: xml
2420
25-
<!-- app/config/config.xml -->
21+
<!-- config/packages/monolog.xml -->
2622
<?xml version="1.0" encoding="UTF-8" ?>
2723
<container xmlns="http://symfony.com/schema/dic/services"
2824
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -33,25 +29,14 @@ log generation. This is recommended for systems that generate a large number of
3329
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
3430
3531
<monolog:config use-microseconds="false">
36-
<monolog:handler
37-
name="applog"
38-
type="stream"
39-
path="/var/log/symfony.log"
40-
level="error"
41-
/>
32+
<!-- ... -->
4233
</monolog:config>
4334
</container>
4435
4536
.. code-block:: php
4637
47-
// app/config/config.php
38+
// config/packages/monolog.php
4839
$container->loadFromExtension('monolog', array(
4940
'use_microseconds' => false,
50-
'handlers' => array(
51-
'applog' => array(
52-
'type' => 'stream',
53-
'path' => '/var/log/symfony.log',
54-
'level' => 'error',
55-
),
56-
),
41+
// ...
5742
));

logging/formatter.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ configure your handler to use it:
2020
2121
Monolog\Formatter\JsonFormatter: ~
2222
23-
# app/config/config_prod.yml (and/or config_dev.yml)
23+
# config/packages/prod/monolog.yaml (and/or config/packages/dev/monolog.yaml)
2424
monolog:
2525
handlers:
2626
file:
@@ -44,7 +44,7 @@ configure your handler to use it:
4444
<service id="Monolog\Formatter\JsonFormatter" />
4545
</services>
4646
47-
<!-- app/config/config_prod.xml (and/or config_dev.xml) -->
47+
<!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
4848
<monolog:config>
4949
<monolog:handler
5050
name="file"
@@ -57,13 +57,12 @@ configure your handler to use it:
5757
5858
.. code-block:: php
5959
60-
// app/config/config.php
60+
// config/services.php
6161
use Monolog\Formatter\JsonFormatter;
6262
63-
// config/services.php
6463
$container->register(JsonFormatter::class);
6564
66-
// app/config/config_prod.php (or config_dev.php)
65+
// config/packages/prod/monolog.php (and/or config/packages/dev/monolog.php)
6766
$container->loadFromExtension('monolog', array(
6867
'handlers' => array(
6968
'file' => array(

logging/monolog_console.rst

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ current log level and the console verbosity.
3535

3636
The example above could then be rewritten as::
3737

38+
use Psr\Log\LoggerInterface;
3839
use Symfony\Component\Console\Input\InputInterface;
3940
use Symfony\Component\Console\Output\OutputInterface;
4041

41-
protected function execute(InputInterface $input, OutputInterface $output)
42+
private $logger;
43+
44+
public function __constructor(LoggerInterface $logger)
4245
{
43-
// assuming the Command extends ContainerAwareCommand...
44-
$logger = $this->getContainer()->get('logger');
45-
$logger->debug('Some info');
46+
$this->logger = $logger;
47+
}
4648

47-
$logger->notice('Some more info');
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$this->logger->debug('Some info');
52+
// ...
53+
$this->logger->notice('Some more info');
4854
}
4955

5056
Depending on the verbosity level that the command is run in and the user's
@@ -53,14 +59,13 @@ the console. If they are displayed, they are timestamped and colored appropriate
5359
Additionally, error logs are written to the error output (php://stderr).
5460
There is no need to conditionally handle the verbosity settings anymore.
5561

56-
The Monolog console handler is enabled by default in the Symfony Framework. For
57-
example, in ``config_dev.yml``:
62+
The Monolog console handler is enabled by default:
5863

5964
.. configuration-block::
6065

6166
.. code-block:: yaml
6267
63-
# app/config/config_dev.yml
68+
# config/packages/dev/monolog.yaml
6469
monolog:
6570
handlers:
6671
# ...
@@ -75,7 +80,7 @@ example, in ``config_dev.yml``:
7580
7681
.. code-block:: xml
7782
78-
<!-- app/config/config.xml -->
83+
<!-- config/packages/dev/monolog.xml -->
7984
<?xml version="1.0" encoding="UTF-8" ?>
8085
<container xmlns="http://symfony.com/schema/dic/services"
8186
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -98,7 +103,7 @@ example, in ``config_dev.yml``:
98103
99104
.. code-block:: php
100105
101-
// app/config/config.php
106+
// config/packages/dev/monolog.php
102107
$container->loadFromExtension('monolog', array(
103108
'handlers' => array(
104109
'console' => array(

0 commit comments

Comments
 (0)