@@ -10,216 +10,21 @@ Logging
1010 :depth: 1
1111 :class: singlecols
1212
13- Overview
14- --------
13+ .. important::
1514
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.
15+ The driver doesn't use the logger in this version. Attempting to use
16+ prior settings in this version won't print anything. Instead, see our
17+ :doc:`command monitoring guide <monitoring/command-monitoring>`.
2018
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.
19+ Temporary Alternative
20+ ---------------------
2321
24- Log Levels
25- ----------
22+ The Node.js team is currently working on rewriting the logger. In the
23+ meantime, you can output monitor events using the following snippet:
2624
27- You can configure the driver to report logger messages at three different
28- levels as shown in the following table.
25+ .. code-block:: javascript
2926
30- .. list-table::
31- :widths: 25 75
32- :header-rows: 1
33-
34- * - Logger Level
35- - Description
36-
37- * - ``debug``
38- - The most verbose logger level which shows all logger messages reported
39- by the driver including those reported in the ``info`` and ``error``
40- levels.
41-
42- * - ``info``
43- - Include informational messages reported by the driver including those
44- reported in the ``error`` level.
45-
46- * - ``error``
47- - Only include error messages reported by the driver. This is the default
48- setting.
49-
50- The following example demonstrates how to set the logger to the ``debug``
51- level:
52-
53- .. literalinclude:: /code-snippets/logging/levels.js
54- :language: javascript
55- :start-after: begin-ex
56- :end-before: end-ex
57-
58- This snippet should output a few lines of logging information that
59- resemble the following:
60-
61- .. code-block:: none
62-
63- [DEBUG-Db:63846] 1585699200000 executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
64- type: 'debug',
65- message: 'executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
66- className: 'Db',
67- ...,
68- }
69- [DEBUG-Server:63846] 1585699200000 executing command [{"ns":"sample_mflix.$cmd","cmd":{"isMaster":true},"options":{}}] against myClusterHostname:27017:27017 {
70- ...
71- }
72- [DEBUG-Server:63846] 11585699200000 executing command
73- [{"ns":"admin.$cmd","cmd":{"endSessions":[{"id":"DQLUQ1/LRKOYMy2CD13iLQ=="}]},"options":{}}] against myClusterHostname:27017 {
74- ...
75- }
76-
77-
78- Filter on a Specific Class
79- --------------------------
80-
81- You can set the Logger to only produce log messages generated by specific
82- classes by defining a filter on it. The following example demonstrates how to
83- apply a filter to log messages from the ``Db`` class only.
84-
85- .. code-block:: js
86-
87- // Set debug level
88- Logger.setLevel("debug");
89-
90- // Only log statements on "Db" class
91- Logger.filter("class", ["Db"]);
92-
93- const db = client.db("sample_mflix");
94-
95- // Execute command { isMaster: true } against db
96- await db.command({ isMaster: true });
97- }
98-
99- The logger output of the code above is similar to the prior example, but
100- excludes logging from classes other than ``Db`` such as ``Server`` and
101- resembles the following:
102-
103- .. code-block:: none
104-
105- [DEBUG-Db:63846] 1585699200000 executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
106- type: 'debug',
107- message: 'executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
108- className: 'Db',
109- ...
110-
111- You can specify any number of the following driver classes in the logging
112- filter array to control what information is passed to the Logger:
113-
114- .. list-table::
115- :widths: 25 75
116- :header-rows: 1
117-
118- * - Class name
119- - Description
120-
121- * - ``Db``
122- - Information related to calls on a db instance
123-
124- * - ``Server``
125- - Information related to a server instance (standalone instance,
126- ``mongos`` instance, or replica set member).
127-
128- * - ``ReplSet``
129- - Information related to a replica set.
130-
131- * - ``Mongos``
132- - Information related to a ``mongos`` instance.
133-
134- * - ``Cursor``
135- - Information related to a cursor.
136-
137- * - ``Pool``
138- - Information related to a connection pool.
139-
140- * - ``Connection``
141- - Information related to a single-instance connection.
142-
143- * - ``Ping``
144- - Information related to a replica set ping operation.
145-
146- You can add your own classes to the logger by creating your own logger
147- instances as shown in the following sample code.
148-
149- .. code-block:: js
150-
151- const { Logger } = require("mongodb");
152-
153- class A {
154- constructor() {
155- this.logger = new Logger("A");
156- }
157-
158- doSomething() {
159- if (this.logger.isInfo()) {
160- this.logger.info("logging A", {});
161- }
162- }
163- }
164-
165- // Execute A to produce logging messages
166- const a = new A();
167- a.doSomething();
168-
169- The logging message is triggered when ``doSomething()`` is called and is
170- presented in the following format:
171-
172- .. code-block:: none
173-
174- [INFO-A:65156] 1585699200000 logging A {
175- type: 'info',
176- message: 'logging A',
177- className: 'A',
178- ...
179- }
180-
181-
182- Custom Logger
183- -------------
184-
185- You can configure the behavior and format of the messages reported by the
186- logger by passing in a **logger callback function** which allows you
187- to access and modify the message and metadata of the logger message.
188-
189- The following example demonstrates how to define a logger callback
190- function and add custom logic to it.
191-
192- .. code-block:: js
193-
194- // Set debug level
195- Logger.setLevel("debug");
196-
197- // Set our own logger
198- Logger.setCurrentLogger((msg, context) => {
199- // Add your custom logic here
200- context['foo'] = 'bar';
201- msg = "Hello, World! " + msg;
202-
203- console.log(msg, context);
204- });
205-
206- const db = client.db("sample_mflix");
207-
208- // Run a command to produce logger messages
209- await db.command({ isMaster: true });
210-
211- The code example above adds additional information in the callback function.
212- The output of the example resembles the following:
213-
214- .. code-block:: none
215-
216- Hello, World! [DEBUG-Db:65873] 1585699200000 executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
217- type: 'debug',
218- message: 'executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
219- className: 'Db',
220- foo: 'bar',
221- ...
222- }
223-
224- For more information on the methods available on the ``Logger``, see the
225- :node-api-4.0:`Logger API documentation </classes/logger.html>`.
27+ const client = new MongoClient(CONNECTION_STRING, { monitorCommands:true });
28+ client.on('commandStarted', (event) => console.debug(event));
29+ client.on('commandSucceeded', (event) => console.debug(event));
30+ client.on('commandFailed', (event) => console.debug(event));
0 commit comments