Skip to content

Commit

Permalink
update command logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dariakp committed Jun 22, 2021
1 parent cb621ba commit da74f03
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
1 change: 0 additions & 1 deletion lib/core/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ class Connection extends EventEmitter {
write(buffer) {
// Debug Log
if (this.logger.isDebug()) {
// TODO: might need to redact?
if (!Array.isArray(buffer)) {
this.logger.debug(`writing buffer [ ${buffer.toString('hex').length} ] to ${this.address}`);
} else {
Expand Down
2 changes: 0 additions & 2 deletions lib/core/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ class CoreCursor extends Readable {

if (cursor.operation) {
if (cursor.logger.isDebug()) {
// TODO: can this contain sensitive data?
cursor.logger.debug(
`issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify(
cursor.query
Expand Down Expand Up @@ -592,7 +591,6 @@ class CoreCursor extends Readable {
}

if (cursor.logger.isDebug()) {
// TODO: can this contain sensitive data?
cursor.logger.debug(
`issue initial query [${JSON.stringify(cursor.cmd)}] with flags [${JSON.stringify(
cursor.query
Expand Down
5 changes: 3 additions & 2 deletions lib/core/sdam/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const isNodeShuttingDownError = require('../error').isNodeShuttingDownError;
const isNetworkErrorBeforeHandshake = require('../error').isNetworkErrorBeforeHandshake;
const maxWireVersion = require('../utils').maxWireVersion;
const makeStateMachine = require('../utils').makeStateMachine;
const extractCommand = require('../../command_utils').extractCommand;
const common = require('./common');
const ServerType = common.ServerType;
const isTransactionCommand = require('../transactions').isTransactionCommand;
Expand Down Expand Up @@ -261,11 +262,11 @@ class Server extends EventEmitter {

// Debug log
if (this.s.logger.isDebug()) {
// TODO: redact or delete
const extractedCommand = extractCommand(cmd);
this.s.logger.debug(
`executing command [${JSON.stringify({
ns,
cmd,
cmd: extractedCommand.shouldRedact ? extractedCommand.name : cmd,
options: debugOptions(DEBUG_FIELDS, options)
})}] against ${this.name}`
);
Expand Down
8 changes: 5 additions & 3 deletions lib/core/topologies/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var inherits = require('util').inherits,
createCompressionInfo = require('./shared').createCompressionInfo,
resolveClusterTime = require('./shared').resolveClusterTime,
SessionMixins = require('./shared').SessionMixins,
extractCommand = require('../../command_utils').extractCommand,
relayEvents = require('../utils').relayEvents;

const collationNotSupported = require('../utils').collationNotSupported;
Expand Down Expand Up @@ -608,19 +609,20 @@ Server.prototype.command = function(ns, cmd, options, callback) {
options = Object.assign({}, options, { wireProtocolCommand: false });

// Debug log
if (self.s.logger.isDebug())
// TODO: redact or delete
if (self.s.logger.isDebug()) {
const extractedCommand = extractCommand(cmd);
self.s.logger.debug(
f(
'executing command [%s] against %s',
JSON.stringify({
ns: ns,
cmd: cmd,
cmd: extractedCommand.shouldRedact ? extractedCommand.name : cmd,
options: debugOptions(debugFields, options)
}),
self.name
)
);
}

// If we are not connected or have a disconnectHandler specified
if (disconnectHandler(self, 'command', ns, cmd, options, callback)) return;
Expand Down
5 changes: 3 additions & 2 deletions lib/operations/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
const MongoDBNamespace = require('../utils').MongoDBNamespace;
const extractCommand = require('../command_utils').extractCommand;

const debugFields = [
'authSource',
Expand Down Expand Up @@ -95,10 +96,10 @@ class CommandOperation extends OperationBase {

// Debug information
if (db.s.logger.isDebug()) {
// TODO: redact or delete
const extractedCommand = extractCommand(command);
db.s.logger.debug(
`executing command ${JSON.stringify(
command
extractedCommand.shouldRedact ? extractedCommand.name : command
)} against ${dbName}.$cmd with options [${JSON.stringify(
debugOptions(debugFields, options)
)}]`
Expand Down
9 changes: 7 additions & 2 deletions lib/operations/command_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const maxWireVersion = require('../core/utils').maxWireVersion;
const decorateWithExplain = require('../utils').decorateWithExplain;
const commandSupportsReadConcern = require('../core/sessions').commandSupportsReadConcern;
const MongoError = require('../core/error').MongoError;
const extractCommand = require('../command_utils').extractCommand;

const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;

Expand Down Expand Up @@ -89,8 +90,12 @@ class CommandOperationV2 extends OperationBase {
}

if (this.logger && this.logger.isDebug()) {
// TODO: redact or delete
this.logger.debug(`executing command ${JSON.stringify(cmd)} against ${this.ns}`);
const extractedCommand = extractCommand(cmd);
this.logger.debug(
`executing command ${JSON.stringify(
extractedCommand.shouldRedact ? extractedCommand.name : cmd
)} against ${this.ns}`
);
}

server.command(this.ns.toString(), cmd, this.options, (err, result) => {
Expand Down
8 changes: 5 additions & 3 deletions lib/operations/db_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const MongoError = require('../core').MongoError;
const parseIndexOptions = require('../utils').parseIndexOptions;
const ReadPreference = require('../core').ReadPreference;
const toError = require('../utils').toError;
const extractCommand = require('../command_utils').extractCommand;
const CONSTANTS = require('../constants');
const MongoDBNamespace = require('../utils').MongoDBNamespace;

Expand Down Expand Up @@ -227,15 +228,16 @@ function executeCommand(db, command, options, callback) {
options.readPreference = ReadPreference.resolve(db, options);

// Debug information
if (db.s.logger.isDebug())
// TODO: redact or delete
if (db.s.logger.isDebug()) {
const extractedCommand = extractCommand(command);
db.s.logger.debug(
`executing command ${JSON.stringify(
command
extractedCommand.shouldRedact ? extractedCommand.name : command
)} against ${dbName}.$cmd with options [${JSON.stringify(
debugOptions(debugFields, options)
)}]`
);
}

// Execute command
db.s.topology.command(db.s.namespace.withCollection('$cmd'), command, options, (err, result) => {
Expand Down

0 comments on commit da74f03

Please sign in to comment.