Skip to content

Commit eee0a11

Browse files
committed
Updated logging/* articles to Symfony 4
1 parent 7699ca2 commit eee0a11

8 files changed

+83
-101
lines changed

logging.rst

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ logs that can be stored in a variety of different places.
77
Logging a Message
88
-----------------
99

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

1313
use Psr\Log\LoggerInterface;
1414

15-
public function indexAction(LoggerInterface $logger)
15+
public function index(LoggerInterface $logger)
1616
{
17-
// alternative way of getting the logger
18-
// $logger = $this->get('logger');
19-
2017
$logger->info('I just got the logger');
2118
$logger->error('An error occurred');
22-
19+
2320
$logger->critical('I left the oven on!', array(
2421
// include extra "context" info in your logs
2522
'cause' => 'in_hurry',
@@ -28,7 +25,7 @@ your controller::
2825
// ...
2926
}
3027

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

@@ -37,10 +34,6 @@ See LoggerInterface_ for a list of all of the methods on the logger.
3734
Where Logs are Stored
3835
---------------------
3936

40-
The configuration for *where* logs are stored lives in the specific
41-
:doc:`environment </configuration/environments>` configuration files: ``config_dev.yml``
42-
and ``config_prod.yml``.
43-
4437
By default, log entries are written to the ``var/log/dev.log`` file when you're in
4538
the ``dev`` environment. In the ``prod`` environment, logs are written to ``var/log/prod.log``,
4639
but *only* during a request where an error or high-priority log entry was made
@@ -61,8 +54,8 @@ to different locations (e.g. files, database, Slack, etc).
6154
channel can have its *own* handlers, which means you can store different log
6255
messages in different places. See :doc:`/logging/channels_handlers`.
6356

64-
Symfony pre-configures some basic handlers in the ``config_dev.yml`` and ``config_prod.yml``
65-
files. Check these out for some real-world examples.
57+
Symfony pre-configures some basic handlers in the default ``monolog.yaml``
58+
config files. Check these out for some real-world examples.
6659

6760
This example uses *two* handlers: ``stream`` (to write to a file) and ``syslog``
6861
to write logs using the :phpfunction:`syslog` function:
@@ -71,7 +64,7 @@ to write logs using the :phpfunction:`syslog` function:
7164

7265
.. code-block:: yaml
7366
74-
# app/config/config.yml
67+
# config/packcages/monolog.yaml
7568
monolog:
7669
handlers:
7770
# this "file_log" key could be anything
@@ -89,7 +82,7 @@ to write logs using the :phpfunction:`syslog` function:
8982
9083
.. code-block:: xml
9184
92-
<!-- app/config/config.xml -->
85+
<!-- config/packcages/monolog.xml -->
9386
<?xml version="1.0" encoding="UTF-8" ?>
9487
<container xmlns="http://symfony.com/schema/dic/services"
9588
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -116,7 +109,7 @@ to write logs using the :phpfunction:`syslog` function:
116109
117110
.. code-block:: php
118111
119-
// app/config/config.php
112+
// config/packcages/monolog.php
120113
$container->loadFromExtension('monolog', array(
121114
'handlers' => array(
122115
'file_log' => array(
@@ -147,7 +140,7 @@ one of the messages reaches an ``action_level``. Take this example:
147140

148141
.. code-block:: yaml
149142
150-
# app/config/config.yml
143+
# config/packcages/monolog.yaml
151144
monolog:
152145
handlers:
153146
filter_for_errors:
@@ -168,7 +161,7 @@ one of the messages reaches an ``action_level``. Take this example:
168161
169162
.. code-block:: xml
170163
171-
<!-- app/config/config.xml -->
164+
<!-- config/packcages/monolog.xml -->
172165
<?xml version="1.0" encoding="UTF-8" ?>
173166
<container xmlns="http://symfony.com/schema/dic/services"
174167
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -201,7 +194,7 @@ one of the messages reaches an ``action_level``. Take this example:
201194
202195
.. code-block:: php
203196
204-
// app/config/config.php
197+
// config/packcages/monolog.php
205198
$container->loadFromExtension('monolog', array(
206199
'handlers' => array(
207200
'filter_for_errors' => array(
@@ -261,7 +254,7 @@ option of your handler to ``rotating_file``:
261254

262255
.. code-block:: yaml
263256
264-
# app/config/config_dev.yml
257+
# config/packcages/dev/monolog.yaml
265258
monolog:
266259
handlers:
267260
main:
@@ -274,7 +267,7 @@ option of your handler to ``rotating_file``:
274267
275268
.. code-block:: xml
276269
277-
<!-- app/config/config_dev.xml -->
270+
<!-- config/packcages/dev/monolog.xml -->
278271
<?xml version="1.0" encoding="UTF-8" ?>
279272
<container xmlns="http://symfony.com/schema/dic/services"
280273
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -298,7 +291,7 @@ option of your handler to ``rotating_file``:
298291
299292
.. code-block:: php
300293
301-
// app/config/config_dev.php
294+
// config/packcages/dev/monolog.php
302295
$container->loadFromExtension('monolog', array(
303296
'handlers' => array(
304297
'main' => array(
@@ -315,8 +308,7 @@ option of your handler to ``rotating_file``:
315308
Using a Logger inside a Service
316309
-------------------------------
317310

318-
To use a logger in your own services, add the ``@logger`` service as an argument
319-
of those services. If you want to use a pre-configured logger which uses a
311+
If you want to use in your own services a pre-configured logger which uses a
320312
specific channel (``app`` by default), use the ``monolog.logger`` tag with the
321313
``channel`` property as explained in the
322314
:ref:`Dependency Injection reference <dic_tags-monolog>`.
@@ -334,9 +326,14 @@ Learn more
334326

335327
.. toctree::
336328
:maxdepth: 1
337-
:glob:
338329

339-
logging/*
330+
logging/monolog_regex_based_excludes
331+
logging/monolog_email
332+
logging/channels_handlers
333+
logging/monolog_console
334+
logging/disable_microsecond_precision
335+
logging/formatter
336+
logging/processors
340337

341338
.. _Monolog: https://github.com/Seldaek/monolog
342339
.. _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)