Skip to content

Commit 1b73442

Browse files
author
Chris Cho
authored
DOCSP-8052: logging and monitoring (#34)
* DOCSP-8052 - Add logging and monitoring sections.
1 parent a48cc5e commit 1b73442

File tree

6 files changed

+567
-1
lines changed

6 files changed

+567
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ignored first line
2+
const { MongoClient, Logger } = require("mongodb");
3+
4+
// Replace the following with your MongoDB deployment's connection
5+
// string.
6+
const uri =
7+
"mongodb+srv://<clusterUrl>/?replicaSet=rs&retryWrites=true&w=majority";
8+
9+
const client = new MongoClient(uri);
10+
11+
async function main(client) {
12+
// Set debug level
13+
Logger.setLevel("debug");
14+
15+
const db = client.db("sample_mflix");
16+
17+
// Run a sample command to produce logger messages
18+
await db.command({ isMaster: true });
19+
}
20+
21+
async function run() {
22+
try {
23+
await client.connect();
24+
25+
// Establish and verify connection
26+
await client.db("admin").command({ ping: 1 });
27+
console.log("Connected successfully to server");
28+
29+
// Run our sample function
30+
await main(client);
31+
} finally {
32+
// Ensures that the client will close when you finish/error
33+
await client.close();
34+
}
35+
}
36+
run().catch(console.dir);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ignored first line
2+
const { MongoClient } = require("mongodb");
3+
4+
// Replace the following with your MongoDB deployment's connection
5+
// string.
6+
const uri =
7+
"mongodb+srv://<clusterUrl>/?replicaSet=rs&retryWrites=true&w=majority";
8+
9+
const client = new MongoClient(uri);
10+
11+
// Replace <event name> with the name of the event you are subscribing to.
12+
const eventName = "<event name>";
13+
client.on(eventName, event => {
14+
console.log(`received ${eventName}: ${JSON.stringify(event, null, 2)}`);
15+
});
16+
17+
async function run() {
18+
try {
19+
await client.connect();
20+
21+
// Establish and verify connection
22+
await client.db("admin").command({ ping: 1 });
23+
console.log("Connected successfully");
24+
} finally {
25+
// Ensures that the client will close when you finish/error
26+
await client.close();
27+
}
28+
}
29+
run().catch(console.dir);

source/fundamentals.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ Fundamentals
99

1010
/fundamentals/crud
1111
/fundamentals/authentication
12+
/fundamentals/logging
13+
/fundamentals/monitoring

source/fundamentals/logging.txt

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

Comments
 (0)