@@ -37,12 +37,12 @@ Configure Logging
37
37
-----------------
38
38
39
39
To configure your application to receive messages about driver events,
40
- create a logger class that implements the ``Psr\Log\LoggerInterface``
40
+ create an instance of a logger class that implements the ``Psr\Log\LoggerInterface``
41
41
interface. Then, use the ``MongoDB\add_logger()`` function to register
42
42
your logger.
43
43
44
44
After registering a logger, the {+library-short+} generates log messages
45
- as array values that resemble the following sample message:
45
+ that resemble the following sample message:
46
46
47
47
.. code-block:: php
48
48
:copyable: false
@@ -62,65 +62,64 @@ The sample log message includes the following information:
62
62
- Domain string: Specifies the driver component that emitted the log message. The
63
63
``PHONGO`` domain indicates that the {+extension-short+} generated the event.
64
64
65
- Example
66
- ~~~~~~~
65
+ .. note::
67
66
68
- To implement ``Psr\Log\LoggerInterface``, you can create a class that
69
- extends the ``Psr\Log\AbstractLogger`` class. ``AbstractLogger`` implements
70
- ``LoggerInterface`` and a generic ``log()`` method, which receives log
71
- messages at each severity level.
67
+ The preceding example shows a log message stored in an array. However,
68
+ the format of your log messages might differ depending on your logging
69
+ implementation.
70
+
71
+ Create a Monolog Logger
72
+ ~~~~~~~~~~~~~~~~~~~~~~~
73
+
74
+ You can use `Monolog <https://packagist.org/packages/monolog/monolog>`__, a
75
+ PHP logging library, to configure logging in your application. Monolog provides
76
+ a ``Monolog\Logger`` logging class that implements the ``Psr\Log\LoggerInterface``
77
+ interface. It also provides handlers that direct logs to specified locations.
78
+
79
+ To use Monolog, install the ``monolog/monolog`` package by running
80
+ the following command:
81
+
82
+ .. code-block:: shell
83
+
84
+ composer require monolog/monolog
85
+
86
+ Then, you can create a logger by defining a ``Monolog\Logger`` object
87
+ and registering it with the {+library-short+}.
72
88
73
89
This example performs the following actions:
74
90
75
- - Creates a logger called ``MyLogger`` that extends the ``AbstractLogger`` class
76
- and records the log level, description, and domain of each event
91
+ - Creates a Monolog logger called ``mongodb-logger``
92
+ - Uses a handler to write all logs with a severity of ``debug`` or higher
93
+ to a file called ``library.log`` in your project directory
77
94
- Registers the logger
78
- - Prints each log message
79
95
80
96
.. literalinclude:: /includes/monitoring-logging/logging.php
81
97
:language: php
82
- :start-after: start-register -logger
83
- :end-before: end-register -logger
98
+ :start-after: start-monolog -logger
99
+ :end-before: end-monolog -logger
84
100
:dedent:
85
101
86
- Write Custom Log Messages
87
- -------------------------
88
-
89
- You can generate custom log messages by using the ``PsrLogAdapter::writeLog()``
90
- function. This function allows you to write directly to the logger and
91
- can help with application monitoring and debugging. Pass the severity level, domain, and
92
- description as parameters to ``writeLog()``.
93
-
94
- The following example writes log messages that have ``warning`` and
95
- ``critical`` severity levels to the logger:
96
-
97
- .. io-code-block::
98
- :copyable:
99
-
100
- .. input:: /includes/monitoring-logging/logging.php
101
- :start-after: start-write-messages
102
- :end-before: end-write-messages
103
- :language: php
104
- :dedent:
105
-
106
- .. output::
107
- :visible: false
108
-
109
- Array
110
- (
111
- [0] => Array
112
- (
113
- [0] => warning
114
- [1] => This is a warning message
115
- [2] => domain1
116
- )
117
- [1] => Array
118
- (
119
- [0] => critical
120
- [1] => This is a critical message
121
- [2] => domain2
122
- )
123
- )
102
+ Create a Custom Logger
103
+ ~~~~~~~~~~~~~~~~~~~~~~
104
+
105
+ To create a custom PSR-3 logger, create a class that implements the
106
+ ``Psr\Log\LoggerInterface`` interface. You can implement ``Psr\Log\LoggerInterface``
107
+ by defining a class that extends the ``Psr\Log\AbstractLogger`` class.
108
+ ``AbstractLogger`` implements ``LoggerInterface`` and a generic ``log()`` method,
109
+ which receives log messages at each severity level.
110
+
111
+ This example performs the following actions:
112
+
113
+ - Creates a logger class named ``MyLogger`` that extends the ``AbstractLogger`` class
114
+ and records the log level, description, and domain of each event
115
+ - Creates a ``MyLogger`` object and registers the logger
116
+ - Prints each log message
117
+
118
+ .. literalinclude:: /includes/monitoring-logging/logging.php
119
+ :language: php
120
+ :start-after: start-custom-logger
121
+ :end-before: end-custom-logger
122
+ :dedent:
124
123
125
124
Remove a Logger
126
125
---------------
0 commit comments