Skip to content

Commit 1f53a7e

Browse files
committed
JT feedback
1 parent e3d3314 commit 1f53a7e

File tree

2 files changed

+67
-68
lines changed

2 files changed

+67
-68
lines changed

source/includes/monitoring-logging/logging.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
require 'vendor/autoload.php';
44

5+
// start-monolog-logger
6+
use Monolog\Logger;
7+
use Monolog\Handler\StreamHandler;
8+
9+
$logger = new Logger('mongodb-logger');
10+
$logger->pushHandler(new StreamHandler(__DIR__ . '/library.log', Logger::DEBUG));
11+
12+
MongoDB\add_logger($logger);
13+
// end-monolog-logger
14+
15+
// start-custom-logger
516
use Psr\Log\AbstractLogger;
617
use Psr\Log\LoggerInterface;
718
use Psr\Log\LogLevel;
819
use MongoDB\PsrLogAdapter;
920

10-
use function MongoDB\add_logger;
11-
use function MongoDB\remove_logger;
12-
13-
// start-register-logger
1421
class MyLogger extends AbstractLogger
1522
{
1623
public array $logs = [];
@@ -21,18 +28,11 @@ public function log($level, $message, array $context = []): void
2128
}
2229
}
2330

24-
$logger = new MyLogger();
25-
add_logger($logger);
26-
print_r($logger->logs);
27-
// end-register-logger
28-
29-
// start-write-messages
30-
PsrLogAdapter::writeLog(PsrLogAdapter::WARN, 'domain1', 'This is a warning message');
31-
PsrLogAdapter::writeLog(PsrLogAdapter::CRITICAL, 'domain2', 'This is a critical message');
32-
33-
print_r($logger->logs);
34-
// end-write-messages
31+
$customLogger = new MyLogger();
32+
MongoDB\add_logger($customLogger );
33+
print_r($customLogger ->logs);
34+
// end-custom-logger
3535

3636
// start-remove-logger
37-
remove_logger($logger);
38-
// end-remove-logger
37+
MongoDB\remove_logger($logger);
38+
// end-remove-logger

source/monitoring-logging/logging.txt

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ Configure Logging
3737
-----------------
3838

3939
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``
4141
interface. Then, use the ``MongoDB\add_logger()`` function to register
4242
your logger.
4343

4444
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:
4646

4747
.. code-block:: php
4848
:copyable: false
@@ -62,65 +62,64 @@ The sample log message includes the following information:
6262
- Domain string: Specifies the driver component that emitted the log message. The
6363
``PHONGO`` domain indicates that the {+extension-short+} generated the event.
6464

65-
Example
66-
~~~~~~~
65+
.. note::
6766

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+}.
7288

7389
This example performs the following actions:
7490

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
7794
- Registers the logger
78-
- Prints each log message
7995

8096
.. literalinclude:: /includes/monitoring-logging/logging.php
8197
: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
84100
:dedent:
85101

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:
124123

125124
Remove a Logger
126125
---------------

0 commit comments

Comments
 (0)