-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-47063: Logging #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
norareidy
wants to merge
7
commits into
mongodb:comp-cov
Choose a base branch
from
norareidy:DOCSP-47063-logging
base: comp-cov
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
DOCSP-47063: Logging #267
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f37d0fe
DOCSP-47063: Logging
norareidy 41aceb4
edits
norareidy 7decdd4
word
norareidy a5cdb31
feedback
norareidy b895d72
code edit
norareidy ee302ce
fix
norareidy e3d3314
Merge remote-tracking branch 'upstream/comp-cov' into DOCSP-47063-log…
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
use MongoDB\PsrLogAdapter; | ||
|
||
use function MongoDB\add_logger; | ||
use function MongoDB\remove_logger; | ||
|
||
// start-register-logger | ||
class MyLogger extends AbstractLogger | ||
{ | ||
public array $logs = []; | ||
|
||
public function log($level, $message, array $context = []): void | ||
{ | ||
$this->logs[] = [$level, $message, $context['domain']]; | ||
} | ||
} | ||
|
||
$logger = new MyLogger(); | ||
add_logger($logger); | ||
print_r($logger->logs); | ||
// end-register-logger | ||
|
||
// start-write-messages | ||
PsrLogAdapter::writeLog(PsrLogAdapter::WARN, 'domain1', 'This is a warning message'); | ||
PsrLogAdapter::writeLog(PsrLogAdapter::CRITICAL, 'domain2', 'This is a critical message'); | ||
|
||
print_r($logger->logs); | ||
// end-write-messages | ||
|
||
// start-remove-logger | ||
remove_logger($logger); | ||
// end-remove-logger |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
.. _php-logging: | ||
|
||
================= | ||
Log Driver Events | ||
================= | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: debugging, printing | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+library-short+} to | ||
set up and configure **logging**. Logging allows you to receive | ||
information about database operations, server connections, errors, | ||
and other events that occur while your application runs. | ||
|
||
The {+library-short+} supports `Psr\\Log\\LoggerInterface | ||
<https://www.php-fig.org/psr/psr-3>`__, a PSR-3 | ||
logger interface that configures your application to receive log messages. | ||
You can register one or more ``Psr\Log\LoggerInterface`` objects to | ||
send messages to the logger. The {+library-short+} is built on top of | ||
the MongoDB C driver and the {+extension-short+}, so the logger receives event notifications | ||
from both components. | ||
|
||
Configure Logging | ||
----------------- | ||
|
||
To configure your application to receive messages about driver events, | ||
create a logger class that implements the ``Psr\Log\LoggerInterface`` | ||
interface. Then, use the ``MongoDB\add_logger()`` function to register | ||
your logger. | ||
|
||
After registering a logger, the {+library-short+} generates log messages | ||
as array values that resemble the following sample message: | ||
|
||
.. code-block:: php | ||
:copyable: false | ||
|
||
[0] => Array | ||
( | ||
[0] => debug | ||
[1] => Created client with hash: ... | ||
[2] => PHONGO | ||
) | ||
|
||
The sample log message includes the following information: | ||
|
||
- Log level: Indicates the severity of the message. The ``debug`` level corresponds to | ||
standard driver activities. To view a list of possible levels, see `PSR\\Log\\LogLevel <https://www.php-fig.org/psr/psr-3/#5-psrlogloglevel>`__. | ||
- Message: Describes the logged event, which signals the creation of a new client. | ||
- Domain string: Specifies the driver component that emitted the log message. The | ||
``PHONGO`` domain indicates that the {+extension-short+} generated the event. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
To implement ``Psr\Log\LoggerInterface``, you can create a class that | ||
extends the ``Psr\Log\AbstractLogger`` class. ``AbstractLogger`` implements | ||
``LoggerInterface`` and a generic ``log()`` method, which receives log | ||
messages at each severity level. | ||
|
||
This example performs the following actions: | ||
|
||
- Creates a logger called ``MyLogger`` that extends the ``AbstractLogger`` class | ||
and records the log level, description, and domain of each event | ||
- Registers the logger | ||
- Prints each log message | ||
|
||
.. literalinclude:: /includes/monitoring-logging/logging.php | ||
:language: php | ||
:start-after: start-register-logger | ||
:end-before: end-register-logger | ||
:dedent: | ||
|
||
Write Custom Log Messages | ||
------------------------- | ||
|
||
You can generate custom log messages by using the ``PsrLogAdapter::writeLog()`` | ||
function. This function allows you to write directly to the logger and | ||
can help with application monitoring and debugging. Pass the severity level, domain, and | ||
description as parameters to ``writeLog()``. | ||
|
||
The following example writes log messages that have ``warning`` and | ||
``critical`` severity levels to the logger: | ||
|
||
.. io-code-block:: | ||
:copyable: | ||
|
||
.. input:: /includes/monitoring-logging/logging.php | ||
:start-after: start-write-messages | ||
:end-before: end-write-messages | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
Array | ||
( | ||
[0] => Array | ||
( | ||
[0] => warning | ||
[1] => This is a warning message | ||
[2] => domain1 | ||
) | ||
[1] => Array | ||
( | ||
[0] => critical | ||
[1] => This is a critical message | ||
[2] => domain2 | ||
) | ||
) | ||
|
||
Remove a Logger | ||
--------------- | ||
|
||
To unregister a logger, pass your logger object as a parameter to the ``MongoDB\remove_logger()`` | ||
function. After calling this function, your logger no longer receives log | ||
messages about your application. | ||
|
||
The following example unregisters a logger: | ||
|
||
.. literalinclude:: /includes/monitoring-logging/logging.php | ||
:language: php | ||
:start-after: start-remove-logger | ||
:end-before: end-remove-logger | ||
:dedent: | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about the PSR-3 logger, see `PSR-3: Logger Interface | ||
<https://www.php-fig.org/psr/psr-3/>`__ in the PHP-FIG documentation. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about the {+library-short+} methods discussed in this guide, see the | ||
following API documentation: | ||
|
||
- :phpmethod:`MongoDB\add_logger()` | ||
- :phpmethod:`MongoDB\remove_logger()` | ||
|
||
To learn more about how the underlying C driver generates log messages, see `Logging | ||
<https://mongoc.org/libmongoc/current/logging.html>`__ in the ``libmongoc`` | ||
API documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: theres a missing bullet point here for Change streams (out of scope but you can add)