@@ -17,16 +17,13 @@ install the Monolog based logger before using it:
17
17
Logging a Message
18
18
-----------------
19
19
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 ::
22
22
23
23
use Psr\Log\LoggerInterface;
24
24
25
- public function indexAction (LoggerInterface $logger)
25
+ public function index (LoggerInterface $logger)
26
26
{
27
- // alternative way of getting the logger
28
- // $logger = $this->get('logger');
29
-
30
27
$logger->info('I just got the logger');
31
28
$logger->error('An error occurred');
32
29
@@ -38,7 +35,7 @@ your controller::
38
35
// ...
39
36
}
40
37
41
- The `` logger `` service has different methods for different logging levels/priorities.
38
+ The logger service has different methods for different logging levels/priorities.
42
39
You can configure the logger to do different things based on the *level * of a message
43
40
(e.g. :doc: `send an email when an error occurs </logging/monolog_email >`).
44
41
@@ -47,10 +44,6 @@ See LoggerInterface_ for a list of all of the methods on the logger.
47
44
Where Logs are Stored
48
45
---------------------
49
46
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
-
54
47
By default, log entries are written to the ``var/log/dev.log `` file when you're in
55
48
the ``dev `` environment. In the ``prod `` environment, logs are written to ``var/log/prod.log ``,
56
49
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).
71
64
channel can have its *own * handlers, which means you can store different log
72
65
messages in different places. See :doc: `/logging/channels_handlers `.
73
66
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.
76
69
77
70
This example uses *two * handlers: ``stream `` (to write to a file) and ``syslog ``
78
71
to write logs using the :phpfunction: `syslog ` function:
@@ -81,7 +74,7 @@ to write logs using the :phpfunction:`syslog` function:
81
74
82
75
.. code-block :: yaml
83
76
84
- # app/ config/config.yml
77
+ # config/packages/monolog.yaml
85
78
monolog :
86
79
handlers :
87
80
# this "file_log" key could be anything
@@ -99,7 +92,7 @@ to write logs using the :phpfunction:`syslog` function:
99
92
100
93
.. code-block :: xml
101
94
102
- <!-- app/ config/config .xml -->
95
+ <!-- config/packages/monolog .xml -->
103
96
<?xml version =" 1.0" encoding =" UTF-8" ?>
104
97
<container xmlns =" http://symfony.com/schema/dic/services"
105
98
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -126,7 +119,7 @@ to write logs using the :phpfunction:`syslog` function:
126
119
127
120
.. code-block :: php
128
121
129
- // app/ config/config .php
122
+ // config/packages/monolog .php
130
123
$container->loadFromExtension('monolog', array(
131
124
'handlers' => array(
132
125
'file_log' => array(
@@ -157,7 +150,7 @@ one of the messages reaches an ``action_level``. Take this example:
157
150
158
151
.. code-block :: yaml
159
152
160
- # app/ config/config.yml
153
+ # config/packages/monolog.yaml
161
154
monolog :
162
155
handlers :
163
156
filter_for_errors :
@@ -178,7 +171,7 @@ one of the messages reaches an ``action_level``. Take this example:
178
171
179
172
.. code-block :: xml
180
173
181
- <!-- app/ config/config .xml -->
174
+ <!-- config/packages/monolog .xml -->
182
175
<?xml version =" 1.0" encoding =" UTF-8" ?>
183
176
<container xmlns =" http://symfony.com/schema/dic/services"
184
177
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -211,7 +204,7 @@ one of the messages reaches an ``action_level``. Take this example:
211
204
212
205
.. code-block :: php
213
206
214
- // app/ config/config .php
207
+ // config/packages/monolog .php
215
208
$container->loadFromExtension('monolog', array(
216
209
'handlers' => array(
217
210
'filter_for_errors' => array(
@@ -271,7 +264,7 @@ option of your handler to ``rotating_file``:
271
264
272
265
.. code-block :: yaml
273
266
274
- # app/ config/config_dev.yml
267
+ # config/packages/dev/monolog.yaml
275
268
monolog :
276
269
handlers :
277
270
main :
@@ -284,7 +277,7 @@ option of your handler to ``rotating_file``:
284
277
285
278
.. code-block :: xml
286
279
287
- <!-- app/ config/config_dev .xml -->
280
+ <!-- config/packages/dev/monolog .xml -->
288
281
<?xml version =" 1.0" encoding =" UTF-8" ?>
289
282
<container xmlns =" http://symfony.com/schema/dic/services"
290
283
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -308,7 +301,7 @@ option of your handler to ``rotating_file``:
308
301
309
302
.. code-block :: php
310
303
311
- // app/ config/config_dev .php
304
+ // config/packages/dev/monolog .php
312
305
$container->loadFromExtension('monolog', array(
313
306
'handlers' => array(
314
307
'main' => array(
@@ -325,8 +318,7 @@ option of your handler to ``rotating_file``:
325
318
Using a Logger inside a Service
326
319
-------------------------------
327
320
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
330
322
specific channel (``app `` by default), use the ``monolog.logger `` tag with the
331
323
``channel `` property as explained in the
332
324
:ref: `Dependency Injection reference <dic_tags-monolog >`.
@@ -344,9 +336,14 @@ Learn more
344
336
345
337
.. toctree ::
346
338
:maxdepth: 1
347
- :glob:
348
339
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
350
347
351
348
.. _Monolog : https://github.com/Seldaek/monolog
352
349
.. _LoggerInterface : https://github.com/php-fig/log/blob/master/Psr/Log/LoggerInterface.php
0 commit comments