1
- Logging with Monolog
2
- ====================
1
+ Logging
2
+ =======
3
3
4
- Symfony integrates seamlessly with `Monolog `_, the most popular PHP logging
5
- library, to create and store log messages in a variety of different places.
6
-
7
- Installation
8
- ------------
4
+ Symfony comes with a minimalist `PSR-3 `_ logger: :class: `Symfony\\ Component\\ HttpKernel\\ Log\\ Logger `.
5
+ In conformance with `the twelve-factor app methodology `_, it sends messages starting from the
6
+ ``WARNING `` level to `stderr `_.
9
7
10
- In applications using :doc: `Symfony Flex </setup/flex >`, run this command to
11
- install the Monolog based logger before using it:
8
+ The minimal log level can be changed by setting the ``SHELL_VERBOSITY `` environment variable:
12
9
13
- .. code-block :: terminal
10
+ ========================= =================
11
+ ``SHELL_VERBOSITY `` value Minimum log level
12
+ ========================= =================
13
+ ``-1 `` ``ERROR ``
14
+ ``1 `` ``NOTICE ``
15
+ ``2 `` ``INFO ``
16
+ ``3 `` ``DEBUG ``
17
+ ========================= =================
14
18
15
- $ composer require symfony/monolog-bundle
19
+ The minimum log level, the default output and the log format can also be changed by
20
+ passing the appropriate arguments to the constructor of :class: `Symfony\\ Component\\ HttpKernel\\ Log\\ Logger `.
21
+ To do so, :ref: `override the "logger" service definition <service-psr4-loader >`.
16
22
17
23
Logging a Message
18
24
-----------------
19
25
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::
26
+ To log a message, inject the default logger in your controller::
22
27
23
28
use Psr\Log\LoggerInterface;
24
29
@@ -35,12 +40,27 @@ you can get the logger service injecting the ``LoggerInterface`` class::
35
40
// ...
36
41
}
37
42
38
- The logger service has different methods for different logging levels/priorities.
39
- You can configure the logger to do different things based on the *level * of a message
40
- (e.g. :doc: `send an email when an error occurs </logging/monolog_email >`).
41
-
43
+ The ``logger `` service has different methods for different logging levels/priorities.
42
44
See LoggerInterface _ for a list of all of the methods on the logger.
43
45
46
+ Monolog
47
+ -------
48
+
49
+ Symfony integrates seamlessly with `Monolog `_, the most popular PHP logging
50
+ library, to create and store log messages in a variety of different places
51
+ and trigger various actions.
52
+
53
+ For instance, using Monolog you can configure the logger to do different things based on the
54
+ *level * of a message (e.g. :doc: `send an email when an error occurs </logging/monolog_email >`).
55
+
56
+ Run this command to install the Monolog based logger before using it:
57
+
58
+ .. code-block :: terminal
59
+
60
+ $ composer require symfony/monolog-bundle
61
+
62
+ The following sections assume that Monolog is installed.
63
+
44
64
Where Logs are Stored
45
65
---------------------
46
66
@@ -349,6 +369,9 @@ Learn more
349
369
350
370
logging/monolog_regex_based_excludes
351
371
372
+ .. _`the twelve-factor app methodology` : https://12factor.net/logs
373
+ .. _PSR-3 : https://www.php-fig.org/psr/psr-3/
374
+ .. _`stderr` : https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
352
375
.. _Monolog : https://github.com/Seldaek/monolog
353
376
.. _LoggerInterface : https://github.com/php-fig/log/blob/master/Psr/Log/LoggerInterface.php
354
377
.. _`logrotate` : https://github.com/logrotate/logrotate
0 commit comments