|
| 1 | +.. _php-logging: |
| 2 | + |
| 3 | +================= |
| 4 | +Log Driver Events |
| 5 | +================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: debugging, printing |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+library-short+} to |
| 24 | +set up and configure **logging**. Logging allows you to receive |
| 25 | +information about database operations, server connections, errors, |
| 26 | +and other events that occur while your application runs. |
| 27 | + |
| 28 | +The {+library-short+} supports `Psr\\Log\\LoggerInterface |
| 29 | +<https://www.php-fig.org/psr/psr-3>`__, a PSR-3 |
| 30 | +logger interface that configures your application to receive log messages. |
| 31 | +You can register one or more instances of a class that implements |
| 32 | +``Psr\Log\LoggerInterface`` to receive log messages. The {+library-short+} is built on top of |
| 33 | +the MongoDB C driver and the {+extension-short+}, so the logger receives event notifications |
| 34 | +from each component. |
| 35 | + |
| 36 | +Configure Logging |
| 37 | +----------------- |
| 38 | + |
| 39 | +To configure your application to receive messages about driver events, |
| 40 | +create an instance of a logger class that implements the ``Psr\Log\LoggerInterface`` |
| 41 | +interface. Then, use the ``MongoDB\add_logger()`` function to register |
| 42 | +your logger. |
| 43 | + |
| 44 | +After registering a logger, the {+library-short+} generates log messages |
| 45 | +that resemble the following sample message: |
| 46 | + |
| 47 | +.. code-block:: none |
| 48 | + :copyable: false |
| 49 | + |
| 50 | + [0] => Array |
| 51 | + ( |
| 52 | + [0] => debug |
| 53 | + [1] => Created client with hash: ... |
| 54 | + [2] => PHONGO |
| 55 | + ) |
| 56 | + |
| 57 | +The sample log message includes the following information: |
| 58 | + |
| 59 | +- Log level: Indicates the severity of the message. The ``debug`` level corresponds to |
| 60 | + standard driver activities. To view a list of possible levels, see `PSR\\Log\\LogLevel <https://www.php-fig.org/psr/psr-3/#5-psrlogloglevel>`__. |
| 61 | +- Message: Describes the logged event, which signals the creation of a new client. |
| 62 | +- Domain string: Specifies the driver component that emitted the log message. The |
| 63 | + ``PHONGO`` domain indicates that the {+extension-short+} generated the event. |
| 64 | + |
| 65 | +.. note:: Log Message Format |
| 66 | + |
| 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 simplifies |
| 76 | +logging configuration by providing a ``Monolog\Logger`` class. This class implements |
| 77 | +the ``Psr\Log\LoggerInterface`` interface and provides handlers that direct logs to |
| 78 | +specified locations. |
| 79 | + |
| 80 | +To use Monolog, install the ``monolog/monolog`` package by running |
| 81 | +the following command: |
| 82 | + |
| 83 | +.. code-block:: shell |
| 84 | + |
| 85 | + composer require monolog/monolog |
| 86 | + |
| 87 | +Then, you can create a logger by defining a ``Monolog\Logger`` object |
| 88 | +and registering it with the {+library-short+}. |
| 89 | + |
| 90 | +This example performs the following actions: |
| 91 | + |
| 92 | +- Creates a Monolog logger called ``mongodb-logger`` |
| 93 | +- Uses a handler to write all logs with a severity of ``debug`` or higher |
| 94 | + to a file called ``mongodb.log`` in your project directory |
| 95 | +- Registers the logger |
| 96 | + |
| 97 | +.. literalinclude:: /includes/monitoring-logging/logging.php |
| 98 | + :language: php |
| 99 | + :start-after: start-monolog-logger |
| 100 | + :end-before: end-monolog-logger |
| 101 | + :dedent: |
| 102 | + |
| 103 | +Create a Custom Logger |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +To create a custom PSR-3 logger, create a class that implements the |
| 107 | +``Psr\Log\LoggerInterface`` interface. You can implement ``Psr\Log\LoggerInterface`` |
| 108 | +by defining a class that extends the ``Psr\Log\AbstractLogger`` class. |
| 109 | +``AbstractLogger`` implements ``LoggerInterface`` and a generic ``log()`` method, |
| 110 | +which receives log messages at each severity level. |
| 111 | + |
| 112 | +This example performs the following actions: |
| 113 | + |
| 114 | +- Creates a logger class named ``MyLogger`` that extends the ``AbstractLogger`` class |
| 115 | + and records the log level, description, and domain of each event |
| 116 | +- Creates a ``MyLogger`` object and registers the logger |
| 117 | +- Prints each log message |
| 118 | + |
| 119 | +.. literalinclude:: /includes/monitoring-logging/logging.php |
| 120 | + :language: php |
| 121 | + :start-after: start-custom-logger |
| 122 | + :end-before: end-custom-logger |
| 123 | + :dedent: |
| 124 | + |
| 125 | +Remove a Logger |
| 126 | +--------------- |
| 127 | + |
| 128 | +To unregister a logger, pass your logger object as a parameter to the ``MongoDB\remove_logger()`` |
| 129 | +function. After calling this function, your logger no longer receives log |
| 130 | +messages about your application. |
| 131 | + |
| 132 | +The following example unregisters a logger: |
| 133 | + |
| 134 | +.. literalinclude:: /includes/monitoring-logging/logging.php |
| 135 | + :language: php |
| 136 | + :start-after: start-remove-logger |
| 137 | + :end-before: end-remove-logger |
| 138 | + :dedent: |
| 139 | + |
| 140 | +Additional Information |
| 141 | +---------------------- |
| 142 | + |
| 143 | +To learn more about PSR-3 loggers, see `PSR-3: Logger Interface |
| 144 | +<https://www.php-fig.org/psr/psr-3/>`__ in the PHP-FIG documentation. |
| 145 | + |
| 146 | +To learn more about Monolog, see the :github:`monolog </Seldaek/monolog>` |
| 147 | +GitHub repository. |
| 148 | + |
| 149 | +API Documentation |
| 150 | +~~~~~~~~~~~~~~~~~ |
| 151 | + |
| 152 | +To learn more about the {+library-short+} methods discussed in this guide, see the |
| 153 | +following API documentation: |
| 154 | + |
| 155 | +- :phpmethod:`MongoDB\add_logger()` |
| 156 | +- :phpmethod:`MongoDB\remove_logger()` |
| 157 | + |
| 158 | +To learn more about how the underlying C driver generates log messages, see `Logging |
| 159 | +<https://mongoc.org/libmongoc/current/logging.html>`__ in the ``libmongoc`` |
| 160 | +API documentation. |
0 commit comments