@@ -51,20 +51,42 @@ level:
5151
5252.. literalinclude:: /code-snippets/logging/levels.js
5353 :language: javascript
54+ :start-after: begin-ex
55+ :end-before: end-ex
5456 :dedent: 4
5557
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+
5678Filter on a Specific Class
5779--------------------------
5880
5981You can set the Logger to only produce log messages generated by specific
60- classes. The following example demonstrates how to log messages from the
61- ``Db`` class only.
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.
6284
6385.. code-block:: js
6486
65- async function main(client) {
6687 // Set debug level
6788 Logger.setLevel("debug");
89+
6890 // Only log statements on "Db" class
6991 Logger.filter("class", ["Db"]);
7092
@@ -74,7 +96,20 @@ classes. The following example demonstrates how to log messages from the
7496 await db.command({ isMaster: true });
7597 }
7698
77- You can specify the following driver classes for the logging filter:
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:
78113
79114.. list-table::
80115 :header-rows: 1
@@ -107,7 +142,7 @@ You can specify the following driver classes for the logging filter:
107142 * - ``Ping``
108143 - Information related to a replica set ping operation.
109144
110- You can add your own classes to the logger filter by creating your own logger
145+ You can add your own classes to the logger by creating your own logger
111146instances as shown in the following sample code.
112147
113148.. code-block:: js
@@ -126,37 +161,63 @@ instances as shown in the following sample code.
126161 }
127162 }
128163
129- // Execute A
164+ // Execute A to produce logging messages
130165 const a = new A();
131166 a.doSomething();
132167
168+ The logging message is triggered when ``doSomething()`` is called and is
169+ presented in the following format:
170+
171+ .. code-block:: none
172+
173+ [INFO-A:65156] 1585699200000 logging A {
174+ type: 'info',
175+ message: 'logging A',
176+ className: 'A',
177+ ...
178+ }
179+
180+
133181Custom Logger
134182-------------
135183
136184You 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.
185+ logger by passing in a **logger callback function** which allows you
186+ to access and modify the message and metadata of the logger message.
141187
142- The following example demonstrates how to define a custom logger.
188+ The following example demonstrates how to define a logger callback
189+ function and add custom logic to it.
143190
144191.. code-block:: js
145192
146- async function main(client) {
147- // Set debug level
148- Logger.setLevel("debug");
193+ // Set debug level
194+ Logger.setLevel("debug");
149195
150- // Set our own logger
151- Logger.setCurrentLogger((msg, context) => {
152- // Add your custom logic here
153- console.log(msg, context) ;
154- }) ;
196+ // Set our own logger
197+ Logger.setCurrentLogger((msg, context) => {
198+ // Add your custom logic here
199+ context['foo'] = 'bar' ;
200+ msg = "Hello, World! " + msg ;
155201
156- const db = client.db("sample_mflix");
202+ console.log(msg, context);
203+ });
157204
158- // Execute command { isMaster: true } against db to produce log messages
159- await db.command({ isMaster: true });
205+ const db = client.db("sample_mflix");
206+
207+ // Run a command to produce logger messages
208+ await db.command({ isMaster: true });
209+
210+ The code example above adds additional information in the callback function.
211+ The output of the example resembles the following:
212+
213+ .. code-block:: none
214+
215+ Hello, World! [DEBUG-Db:65873] 1585699200000 executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}] {
216+ type: 'debug',
217+ message: 'executing command {"isMaster":true} against sample_mflix.$cmd with options [{"readPreference":{"mode":"primary"}}]',
218+ className: 'Db',
219+ foo: 'bar',
220+ ...
160221 }
161222
162223For more information on the methods available on the ``Logger``, see the
0 commit comments