Skip to content

Commit 8bd9777

Browse files
authored
fix: use emitWarning API for internal messages (#2743)
Updates all logging statements to use node's process.emitWarning API. Allows for filtering, capturing and silencing of warnings. NODE-2317, NODE-3114
1 parent d67ffa7 commit 8bd9777

File tree

18 files changed

+138
-47
lines changed

18 files changed

+138
-47
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"es/no-destructuring": "error",
2222
"es/no-rest-spread-properties": "error",
2323
"es/no-spread-elements": "error",
24-
"no-console": "off",
24+
"no-console": "error",
2525
"eqeqeq": ["error", "always", { "null": "ignore" }],
2626
"strict": ["error", "global"]
2727
},

lib/collection.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const deprecate = require('util').deprecate;
44
const deprecateOptions = require('./utils').deprecateOptions;
5+
const emitWarningOnce = require('./utils').emitWarningOnce;
56
const checkCollectionName = require('./utils').checkCollectionName;
67
const ObjectID = require('./core').BSON.ObjectID;
78
const MongoError = require('./core').MongoError;
@@ -323,7 +324,7 @@ Collection.prototype.find = deprecateOptions(
323324
function(query, options, callback) {
324325
if (typeof callback === 'object') {
325326
// TODO(MAJOR): throw in the future
326-
console.warn('Third parameter to `find()` must be a callback or undefined');
327+
emitWarningOnce('Third parameter to `find()` must be a callback or undefined');
327328
}
328329

329330
let selector = query;
@@ -1092,7 +1093,7 @@ Collection.prototype.findOne = deprecateOptions(
10921093
function(query, options, callback) {
10931094
if (typeof callback === 'object') {
10941095
// TODO(MAJOR): throw in the future
1095-
console.warn('Third parameter to `findOne()` must be a callback or undefined');
1096+
emitWarningOnce('Third parameter to `findOne()` must be a callback or undefined');
10961097
}
10971098

10981099
if (typeof query === 'function') (callback = query), (query = {}), (options = {});

lib/core/auth/scram.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Buffer = require('safe-buffer').Buffer;
44
const retrieveBSON = require('../connection/utils').retrieveBSON;
55
const MongoError = require('../error').MongoError;
66
const AuthProvider = require('./auth_provider').AuthProvider;
7+
const emitWarningOnce = require('../../utils').emitWarning;
78

89
const BSON = retrieveBSON();
910
const Binary = BSON.Binary;
@@ -24,7 +25,7 @@ class ScramSHA extends AuthProvider {
2425
prepare(handshakeDoc, authContext, callback) {
2526
const cryptoMethod = this.cryptoMethod;
2627
if (cryptoMethod === 'sha256' && saslprep == null) {
27-
console.warn('Warning: no saslprep library specified. Passwords will not be sanitized');
28+
emitWarningOnce('Warning: no saslprep library specified. Passwords will not be sanitized');
2829
}
2930

3031
crypto.randomBytes(24, (err, nonce) => {

lib/core/connection/logger.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var Logger = function(className, options) {
3737
if (options.logger) {
3838
currentLogger = options.logger;
3939
} else if (currentLogger == null) {
40+
// eslint-disable-next-line no-console
4041
currentLogger = console.log;
4142
}
4243

lib/core/connection/msg.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const Buffer = require('safe-buffer').Buffer;
3131
const opcodes = require('../wireprotocol/shared').opcodes;
3232
const databaseNamespace = require('../wireprotocol/shared').databaseNamespace;
3333
const ReadPreference = require('../topologies/read_preference');
34+
const MongoError = require('../../core/error').MongoError;
3435

3536
// Incrementing request id
3637
let _requestId = 0;
@@ -196,7 +197,8 @@ class BinMsg {
196197
while (this.index < this.data.length) {
197198
const payloadType = this.data.readUInt8(this.index++);
198199
if (payloadType === 1) {
199-
console.error('TYPE 1');
200+
// It was decided that no driver makes use of payload type 1
201+
throw new MongoError('OP_MSG Payload Type 1 detected unsupported protocol');
200202
} else if (payloadType === 0) {
201203
const bsonSize = this.data.readUInt32LE(this.index);
202204
const bin = this.data.slice(this.index, this.index + bsonSize);

lib/core/sdam/topology.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const ServerSessionPool = require('../sessions').ServerSessionPool;
2727
const makeClientMetadata = require('../utils').makeClientMetadata;
2828
const CMAP_EVENT_NAMES = require('../../cmap/events').CMAP_EVENT_NAMES;
2929
const compareTopologyVersion = require('./server_description').compareTopologyVersion;
30+
const emitWarning = require('../../utils').emitWarning;
3031

3132
const common = require('./common');
3233
const drainTimerQueue = common.drainTimerQueue;
@@ -739,7 +740,7 @@ class Topology extends EventEmitter {
739740
}
740741

741742
unref() {
742-
console.log('not implemented: `unref`');
743+
emitWarning('not implemented: `unref`');
743744
}
744745

745746
// NOTE: There are many places in code where we explicitly check the last isMaster

lib/core/tools/smoke_plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ exports.attachToRunner = function(runner, outputFile) {
5252
fs.writeFileSync(outputFile, JSON.stringify(smokeOutput));
5353

5454
// Standard NodeJS uncaught exception handler
55+
// eslint-disable-next-line no-console
5556
console.error(err.stack);
5657
process.exit(1);
5758
});

lib/core/topologies/read_preference.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const emitWarningOnce = require('../../utils').emitWarningOnce;
23

34
/**
45
* The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
@@ -20,7 +21,7 @@ const ReadPreference = function(mode, tags, options) {
2021

2122
// TODO(major): tags MUST be an array of tagsets
2223
if (tags && !Array.isArray(tags)) {
23-
console.warn(
24+
emitWarningOnce(
2425
'ReadPreference tags must be an array, this will change in the next major version'
2526
);
2627

lib/core/uri_parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const qs = require('querystring');
44
const dns = require('dns');
55
const MongoParseError = require('./error').MongoParseError;
66
const ReadPreference = require('./topologies/read_preference');
7+
const emitWarningOnce = require('../utils').emitWarningOnce;
78

89
/**
910
* The following regular expression validates a connection string and breaks the
@@ -438,7 +439,7 @@ function parseQueryString(query, options) {
438439
// special cases for known deprecated options
439440
if (result.wtimeout && result.wtimeoutms) {
440441
delete result.wtimeout;
441-
console.warn('Unsupported option `wtimeout` specified');
442+
emitWarningOnce('Unsupported option `wtimeout` specified');
442443
}
443444

444445
return Object.keys(result).length ? result : null;

lib/core/wireprotocol/kill_cursors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const MongoError = require('../error').MongoError;
55
const MongoNetworkError = require('../error').MongoNetworkError;
66
const collectionNamespace = require('./shared').collectionNamespace;
77
const maxWireVersion = require('../utils').maxWireVersion;
8+
const emitWarning = require('../utils').emitWarning;
89
const command = require('./command');
910

1011
function killCursors(server, ns, cursorState, callback) {
@@ -31,7 +32,7 @@ function killCursors(server, ns, cursorState, callback) {
3132
if (typeof callback === 'function') {
3233
callback(err, null);
3334
} else {
34-
console.warn(err);
35+
emitWarning(err);
3536
}
3637
}
3738
}

0 commit comments

Comments
 (0)