@@ -51,20 +51,42 @@ level:
51
51
52
52
.. literalinclude:: /code-snippets/logging/levels.js
53
53
:language: javascript
54
+ :start-after: begin-ex
55
+ :end-before: end-ex
54
56
:dedent: 4
55
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
+
56
78
Filter on a Specific Class
57
79
--------------------------
58
80
59
81
You 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.
62
84
63
85
.. code-block:: js
64
86
65
- async function main(client) {
66
87
// Set debug level
67
88
Logger.setLevel("debug");
89
+
68
90
// Only log statements on "Db" class
69
91
Logger.filter("class", ["Db"]);
70
92
@@ -74,7 +96,20 @@ classes. The following example demonstrates how to log messages from the
74
96
await db.command({ isMaster: true });
75
97
}
76
98
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:
78
113
79
114
.. list-table::
80
115
:header-rows: 1
@@ -107,7 +142,7 @@ You can specify the following driver classes for the logging filter:
107
142
* - ``Ping``
108
143
- Information related to a replica set ping operation.
109
144
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
111
146
instances as shown in the following sample code.
112
147
113
148
.. code-block:: js
@@ -126,37 +161,63 @@ instances as shown in the following sample code.
126
161
}
127
162
}
128
163
129
- // Execute A
164
+ // Execute A to produce logging messages
130
165
const a = new A();
131
166
a.doSomething();
132
167
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
+
133
181
Custom Logger
134
182
-------------
135
183
136
184
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.
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.
141
187
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.
143
190
144
191
.. code-block:: js
145
192
146
- async function main(client) {
147
- // Set debug level
148
- Logger.setLevel("debug");
193
+ // Set debug level
194
+ Logger.setLevel("debug");
149
195
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 ;
155
201
156
- const db = client.db("sample_mflix");
202
+ console.log(msg, context);
203
+ });
157
204
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
+ ...
160
221
}
161
222
162
223
For more information on the methods available on the ``Logger``, see the
0 commit comments