@@ -7,19 +7,16 @@ logs that can be stored in a variety of different places.
7
7
Logging a Message
8
8
-----------------
9
9
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 ::
12
12
13
13
use Psr\Log\LoggerInterface;
14
14
15
- public function indexAction (LoggerInterface $logger)
15
+ public function index (LoggerInterface $logger)
16
16
{
17
- // alternative way of getting the logger
18
- // $logger = $this->get('logger');
19
-
20
17
$logger->info('I just got the logger');
21
18
$logger->error('An error occurred');
22
-
19
+
23
20
$logger->critical('I left the oven on!', array(
24
21
// include extra "context" info in your logs
25
22
'cause' => 'in_hurry',
@@ -28,7 +25,7 @@ your controller::
28
25
// ...
29
26
}
30
27
31
- The `` logger `` service has different methods for different logging levels/priorities.
28
+ The logger service has different methods for different logging levels/priorities.
32
29
You can configure the logger to do different things based on the *level * of a message
33
30
(e.g. :doc: `send an email when an error occurs </logging/monolog_email >`).
34
31
@@ -37,10 +34,6 @@ See LoggerInterface_ for a list of all of the methods on the logger.
37
34
Where Logs are Stored
38
35
---------------------
39
36
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
-
44
37
By default, log entries are written to the ``var/log/dev.log `` file when you're in
45
38
the ``dev `` environment. In the ``prod `` environment, logs are written to ``var/log/prod.log ``,
46
39
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).
61
54
channel can have its *own * handlers, which means you can store different log
62
55
messages in different places. See :doc: `/logging/channels_handlers `.
63
56
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.
66
59
67
60
This example uses *two * handlers: ``stream `` (to write to a file) and ``syslog ``
68
61
to write logs using the :phpfunction: `syslog ` function:
@@ -71,7 +64,7 @@ to write logs using the :phpfunction:`syslog` function:
71
64
72
65
.. code-block :: yaml
73
66
74
- # app/ config/config.yml
67
+ # config/packcages/monolog.yaml
75
68
monolog :
76
69
handlers :
77
70
# this "file_log" key could be anything
@@ -89,7 +82,7 @@ to write logs using the :phpfunction:`syslog` function:
89
82
90
83
.. code-block :: xml
91
84
92
- <!-- app/ config/config .xml -->
85
+ <!-- config/packcages/monolog .xml -->
93
86
<?xml version =" 1.0" encoding =" UTF-8" ?>
94
87
<container xmlns =" http://symfony.com/schema/dic/services"
95
88
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -116,7 +109,7 @@ to write logs using the :phpfunction:`syslog` function:
116
109
117
110
.. code-block :: php
118
111
119
- // app/ config/config .php
112
+ // config/packcages/monolog .php
120
113
$container->loadFromExtension('monolog', array(
121
114
'handlers' => array(
122
115
'file_log' => array(
@@ -147,7 +140,7 @@ one of the messages reaches an ``action_level``. Take this example:
147
140
148
141
.. code-block :: yaml
149
142
150
- # app/ config/config.yml
143
+ # config/packcages/monolog.yaml
151
144
monolog :
152
145
handlers :
153
146
filter_for_errors :
@@ -168,7 +161,7 @@ one of the messages reaches an ``action_level``. Take this example:
168
161
169
162
.. code-block :: xml
170
163
171
- <!-- app/ config/config .xml -->
164
+ <!-- config/packcages/monolog .xml -->
172
165
<?xml version =" 1.0" encoding =" UTF-8" ?>
173
166
<container xmlns =" http://symfony.com/schema/dic/services"
174
167
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -201,7 +194,7 @@ one of the messages reaches an ``action_level``. Take this example:
201
194
202
195
.. code-block :: php
203
196
204
- // app/ config/config .php
197
+ // config/packcages/monolog .php
205
198
$container->loadFromExtension('monolog', array(
206
199
'handlers' => array(
207
200
'filter_for_errors' => array(
@@ -261,7 +254,7 @@ option of your handler to ``rotating_file``:
261
254
262
255
.. code-block :: yaml
263
256
264
- # app/ config/config_dev.yml
257
+ # config/packcages/dev/monolog.yaml
265
258
monolog :
266
259
handlers :
267
260
main :
@@ -274,7 +267,7 @@ option of your handler to ``rotating_file``:
274
267
275
268
.. code-block :: xml
276
269
277
- <!-- app/ config/config_dev .xml -->
270
+ <!-- config/packcages/dev/monolog .xml -->
278
271
<?xml version =" 1.0" encoding =" UTF-8" ?>
279
272
<container xmlns =" http://symfony.com/schema/dic/services"
280
273
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -298,7 +291,7 @@ option of your handler to ``rotating_file``:
298
291
299
292
.. code-block :: php
300
293
301
- // app/ config/config_dev .php
294
+ // config/packcages/dev/monolog .php
302
295
$container->loadFromExtension('monolog', array(
303
296
'handlers' => array(
304
297
'main' => array(
@@ -315,8 +308,7 @@ option of your handler to ``rotating_file``:
315
308
Using a Logger inside a Service
316
309
-------------------------------
317
310
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
320
312
specific channel (``app `` by default), use the ``monolog.logger `` tag with the
321
313
``channel `` property as explained in the
322
314
:ref: `Dependency Injection reference <dic_tags-monolog >`.
@@ -334,9 +326,14 @@ Learn more
334
326
335
327
.. toctree ::
336
328
:maxdepth: 1
337
- :glob:
338
329
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
340
337
341
338
.. _Monolog : https://github.com/Seldaek/monolog
342
339
.. _LoggerInterface : https://github.com/php-fig/log/blob/master/Psr/Log/LoggerInterface.php
0 commit comments