-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
Updates all logging statements to use node's process.emitWarning API. Allows for filtering, capturing and silencing of warnings. NODE-2317, NODE-3114
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,39 +287,38 @@ var filterOptions = function(options, names) { | |
}; | ||
|
||
// Write concern keys | ||
var writeConcernKeys = ['w', 'j', 'wtimeout', 'fsync']; | ||
const WRITE_CONCERN_KEYS = ['w', 'j', 'wtimeout', 'fsync', 'writeConcern']; | ||
|
||
// Merge the write concern options | ||
var mergeOptionsAndWriteConcern = function(targetOptions, sourceOptions, keys, mergeWriteConcern) { | ||
// Mix in any allowed options | ||
for (var i = 0; i < keys.length; i++) { | ||
if (!targetOptions[keys[i]] && sourceOptions[keys[i]] !== undefined) { | ||
targetOptions[keys[i]] = sourceOptions[keys[i]]; | ||
} | ||
} | ||
|
||
// No merging of write concern | ||
if (!mergeWriteConcern) return targetOptions; | ||
|
||
// Found no write Concern options | ||
var found = false; | ||
for (i = 0; i < writeConcernKeys.length; i++) { | ||
if (targetOptions[writeConcernKeys[i]]) { | ||
/** | ||
* If there is no WriteConcern related options defined on target then inherit from source. | ||
* Otherwise, do not inherit **any** options from source. | ||
* @internal | ||
* @param {object} target - options object conditionally receiving the writeConcern options | ||
* @param {object} source - options object containing the potentially inherited writeConcern options | ||
*/ | ||
function conditionallyMergeWriteConcern(target, source) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nbbeeken
Author
Contributor
|
||
let found = false; | ||
for (const wcKey of WRITE_CONCERN_KEYS) { | ||
if (wcKey in target) { | ||
// Found a writeConcern option | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!found) { | ||
for (i = 0; i < writeConcernKeys.length; i++) { | ||
if (sourceOptions[writeConcernKeys[i]]) { | ||
targetOptions[writeConcernKeys[i]] = sourceOptions[writeConcernKeys[i]]; | ||
for (const wcKey of WRITE_CONCERN_KEYS) { | ||
if (source[wcKey]) { | ||
if (!('writeConcern' in target)) { | ||
target.writeConcern = {}; | ||
} | ||
target.writeConcern[wcKey] = source[wcKey]; | ||
} | ||
} | ||
} | ||
|
||
return targetOptions; | ||
}; | ||
return target; | ||
} | ||
|
||
/** | ||
* Executes the given operation with provided arguments. | ||
|
@@ -534,7 +533,9 @@ function decorateWithExplain(command, explain) { | |
return { explain: command, verbosity: explain.verbosity }; | ||
} | ||
|
||
const emitProcessWarning = msg => process.emitWarning(msg, 'DeprecationWarning'); | ||
const emitProcessWarning = msg => | ||
process.emitWarning(msg, { type: 'DeprecationWarning', code: MONGODB_WARNING_CODE }); | ||
// eslint-disable-next-line no-console | ||
const emitConsoleWarning = msg => console.error(msg); | ||
const emitDeprecationWarning = process.emitWarning ? emitProcessWarning : emitConsoleWarning; | ||
|
||
|
@@ -816,6 +817,48 @@ function hasAtomicOperators(doc) { | |
); | ||
} | ||
|
||
/** | ||
* When the driver used emitWarning the code will be equal to this. | ||
* @public | ||
* | ||
* @example | ||
* ```js | ||
* process.on('warning', (warning) => { | ||
* if (warning.code === MONGODB_WARNING_CODE) console.error('Ah an important warning! :)') | ||
* }) | ||
* ``` | ||
*/ | ||
const MONGODB_WARNING_CODE = 'MONGODB DRIVER'; | ||
|
||
/** | ||
* @internal | ||
* @param {string} message - message to warn about | ||
*/ | ||
function emitWarning(message) { | ||
if (process.emitWarning) { | ||
return process.emitWarning(message, { code: MONGODB_WARNING_CODE }); | ||
} else { | ||
// Approximate the style of print out on node versions pre 8.x | ||
// eslint-disable-next-line no-console | ||
return console.error(`[${MONGODB_WARNING_CODE}] Warning:`, message); | ||
} | ||
} | ||
|
||
const emittedWarnings = new Set(); | ||
/** | ||
* Will emit a warning once for the duration of the application. | ||
* Uses the message to identify if it has already been emitted | ||
* so using string interpolation can cause multiple emits | ||
* @internal | ||
* @param {string} message - message to warn about | ||
*/ | ||
function emitWarningOnce(message) { | ||
if (!emittedWarnings.has(message)) { | ||
emittedWarnings.add(message); | ||
return emitWarning(message); | ||
} | ||
} | ||
|
||
module.exports = { | ||
filterOptions, | ||
mergeOptions, | ||
|
@@ -832,7 +875,7 @@ module.exports = { | |
isObject, | ||
debugOptions, | ||
MAX_JS_INT: Number.MAX_SAFE_INTEGER + 1, | ||
mergeOptionsAndWriteConcern, | ||
conditionallyMergeWriteConcern, | ||
executeLegacyOperation, | ||
applyRetryableWrites, | ||
applyWriteConcern, | ||
|
@@ -849,5 +892,8 @@ module.exports = { | |
now, | ||
calculateDurationInMs, | ||
makeInterruptableAsyncInterval, | ||
hasAtomicOperators | ||
hasAtomicOperators, | ||
MONGODB_WARNING_CODE, | ||
emitWarning, | ||
emitWarningOnce | ||
}; |
Hi,
Thx for this commit which solves #2744.
This function is used with four args in
lib/db.js
, also why not naming itmergeWriteConcern
if it's not taking any boolean ?Have a nice day !