|
| 1 | +======= |
| 2 | +Logging |
| 3 | +======= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecols |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +This guide shows you how to configure the driver logger which is responsible |
| 17 | +for reporting events that occur while your application is running. You can |
| 18 | +set the logger level, specify logging settings for individual classes, and |
| 19 | +provide a custom logger implementation. |
| 20 | + |
| 21 | +You should read this guide if you need to customize the default behavior of the |
| 22 | +driver logger or want to explore the information provided by the logger. |
| 23 | + |
| 24 | +Log Levels |
| 25 | +---------- |
| 26 | + |
| 27 | +You can configure the driver to report logger messages at three different |
| 28 | +levels as shown in the following table. |
| 29 | + |
| 30 | +.. list-table:: |
| 31 | + :header-rows: 1 |
| 32 | + |
| 33 | + * - Logger Level |
| 34 | + - Description |
| 35 | + |
| 36 | + * - ``debug`` |
| 37 | + - The most verbose logger level which shows all logger messages reported |
| 38 | + by the driver including those reported in the ``info`` and ``error`` |
| 39 | + levels. |
| 40 | + |
| 41 | + * - ``info`` |
| 42 | + - Include informational messages reported by the driver including those |
| 43 | + reported in the ``error`` level. |
| 44 | + |
| 45 | + * - ``error`` |
| 46 | + - Only include error messages reported by the driver. This is the default |
| 47 | + setting. |
| 48 | + |
| 49 | +The following example demonstrates how to set the logger to the ``debug`` |
| 50 | +level: |
| 51 | + |
| 52 | +.. literalinclude:: /code-snippets/logging/levels.js |
| 53 | + :language: javascript |
| 54 | + :dedent: 4 |
| 55 | + |
| 56 | +Filter on a Specific Class |
| 57 | +-------------------------- |
| 58 | + |
| 59 | +You can set the Logger to only produce log messages that are reported by |
| 60 | +specific classes. The following example demonstrates how to log messages from |
| 61 | +only the ``Db`` class. |
| 62 | + |
| 63 | +.. code-block:: js |
| 64 | + |
| 65 | + async function main(client) { |
| 66 | + // Set debug level |
| 67 | + Logger.setLevel("debug"); |
| 68 | + // Only log statements on "Db" class |
| 69 | + Logger.filter("class", ["Db"]); |
| 70 | + |
| 71 | + const db = client.db("sample_mflix"); |
| 72 | + |
| 73 | + // Execute command { isMaster: true } against db |
| 74 | + await db.command({ isMaster: true }); |
| 75 | + } |
| 76 | + |
| 77 | +The driver classes that can be specified for filtering are as follows: |
| 78 | + |
| 79 | +.. list-table:: |
| 80 | + :header-rows: 1 |
| 81 | + |
| 82 | + * - Class name |
| 83 | + - Description |
| 84 | + |
| 85 | + * - ``Db`` |
| 86 | + - Information related to calls on a db instance |
| 87 | + |
| 88 | + * - ``Server`` |
| 89 | + - Information related to a server instance (standalone instance, |
| 90 | + ``mongos`` instance, or replica set member). |
| 91 | + |
| 92 | + * - ``ReplSet`` |
| 93 | + - Information related to a replica set. |
| 94 | + |
| 95 | + * - ``Mongos`` |
| 96 | + - Information related to a ``mongos`` instance. |
| 97 | + |
| 98 | + * - ``Cursor`` |
| 99 | + - Information related to a cursor. |
| 100 | + |
| 101 | + * - ``Pool`` |
| 102 | + - Information related to a connection pool. |
| 103 | + |
| 104 | + * - ``Connection`` |
| 105 | + - Information related to a single-instance connection. |
| 106 | + |
| 107 | + * - ``Ping`` |
| 108 | + - Information related to a replica set ping operation. |
| 109 | + |
| 110 | +You can add your own classes to the logger filter by creating your own logger |
| 111 | +instances as shown in the following sample code. |
| 112 | + |
| 113 | +.. code-block:: js |
| 114 | + |
| 115 | + const { Logger } = require("mongodb"); |
| 116 | + |
| 117 | + class A { |
| 118 | + constructor() { |
| 119 | + this.logger = new Logger("A"); |
| 120 | + } |
| 121 | + |
| 122 | + doSomething() { |
| 123 | + if (this.logger.isInfo()) { |
| 124 | + this.logger.info("logging A", {}); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Execute A |
| 130 | + const a = new A(); |
| 131 | + a.doSomething(); |
| 132 | + |
| 133 | +Custom Logger |
| 134 | +------------- |
| 135 | + |
| 136 | +You can configure the behavior and format of the messages reported by the |
| 137 | +logger by passing in a |
| 138 | +:node-api:`logger callback function <Logger.html#~loggerCallback>` which |
| 139 | +allows you to access and modify the message and metadata of the logger |
| 140 | +message. |
| 141 | + |
| 142 | +The following example demonstrates how to define a custom logger. |
| 143 | + |
| 144 | +.. code-block:: js |
| 145 | + |
| 146 | + async function main(client) { |
| 147 | + // Set debug level |
| 148 | + Logger.setLevel("debug"); |
| 149 | + |
| 150 | + // Set our own logger |
| 151 | + Logger.setCurrentLogger(function(msg, context) { |
| 152 | + // Add your custom logic here |
| 153 | + console.log(msg, context); |
| 154 | + }); |
| 155 | + |
| 156 | + const db = client.db("sample_mflix"); |
| 157 | + |
| 158 | + // Execute command { isMaster: true } against db to produce log messages |
| 159 | + await db.command({ isMaster: true }); |
| 160 | + } |
| 161 | + |
| 162 | +For more information on the methods available on the ``Logger``, see the |
| 163 | +:node-api:`Logger API documentation <Logger.html>`. |
0 commit comments