Spaces:
Runtime error
Runtime error
File size: 24,293 Bytes
9756872 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoLogger = exports.defaultLogTransform = exports.stringifyWithMaxLen = exports.createStdioLogger = exports.parseSeverityFromString = exports.MongoLoggableComponent = exports.SEVERITY_LEVEL_MAP = exports.DEFAULT_MAX_DOCUMENT_LENGTH = exports.SeverityLevel = void 0;
const util_1 = require("util");
const bson_1 = require("./bson");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
/** @internal */
exports.SeverityLevel = Object.freeze({
EMERGENCY: 'emergency',
ALERT: 'alert',
CRITICAL: 'critical',
ERROR: 'error',
WARNING: 'warn',
NOTICE: 'notice',
INFORMATIONAL: 'info',
DEBUG: 'debug',
TRACE: 'trace',
OFF: 'off'
});
/** @internal */
exports.DEFAULT_MAX_DOCUMENT_LENGTH = 1000;
/** @internal */
class SeverityLevelMap extends Map {
constructor(entries) {
const newEntries = [];
for (const [level, value] of entries) {
newEntries.push([value, level]);
}
newEntries.push(...entries);
super(newEntries);
}
getNumericSeverityLevel(severity) {
return this.get(severity);
}
getSeverityLevelName(level) {
return this.get(level);
}
}
/** @internal */
exports.SEVERITY_LEVEL_MAP = new SeverityLevelMap([
[exports.SeverityLevel.OFF, -Infinity],
[exports.SeverityLevel.EMERGENCY, 0],
[exports.SeverityLevel.ALERT, 1],
[exports.SeverityLevel.CRITICAL, 2],
[exports.SeverityLevel.ERROR, 3],
[exports.SeverityLevel.WARNING, 4],
[exports.SeverityLevel.NOTICE, 5],
[exports.SeverityLevel.INFORMATIONAL, 6],
[exports.SeverityLevel.DEBUG, 7],
[exports.SeverityLevel.TRACE, 8]
]);
/** @internal */
exports.MongoLoggableComponent = Object.freeze({
COMMAND: 'command',
TOPOLOGY: 'topology',
SERVER_SELECTION: 'serverSelection',
CONNECTION: 'connection',
CLIENT: 'client'
});
/**
* Parses a string as one of SeverityLevel
* @internal
*
* @param s - the value to be parsed
* @returns one of SeverityLevel if value can be parsed as such, otherwise null
*/
function parseSeverityFromString(s) {
const validSeverities = Object.values(exports.SeverityLevel);
const lowerSeverity = s?.toLowerCase();
if (lowerSeverity != null && validSeverities.includes(lowerSeverity)) {
return lowerSeverity;
}
return null;
}
exports.parseSeverityFromString = parseSeverityFromString;
/** @internal */
function createStdioLogger(stream) {
return {
write: (0, util_1.promisify)((log, cb) => {
const logLine = (0, util_1.inspect)(log, { compact: true, breakLength: Infinity });
stream.write(`${logLine}\n`, 'utf-8', cb);
return;
})
};
}
exports.createStdioLogger = createStdioLogger;
/**
* resolves the MONGODB_LOG_PATH and mongodbLogPath options from the environment and the
* mongo client options respectively. The mongodbLogPath can be either 'stdout', 'stderr', a NodeJS
* Writable or an object which has a `write` method with the signature:
* ```ts
* write(log: Log): void
* ```
*
* @returns the MongoDBLogWritable object to write logs to
*/
function resolveLogPath({ MONGODB_LOG_PATH }, { mongodbLogPath }) {
if (typeof mongodbLogPath === 'string' && /^stderr$/i.test(mongodbLogPath)) {
return { mongodbLogPath: createStdioLogger(process.stderr), mongodbLogPathIsStdErr: true };
}
if (typeof mongodbLogPath === 'string' && /^stdout$/i.test(mongodbLogPath)) {
return { mongodbLogPath: createStdioLogger(process.stdout), mongodbLogPathIsStdErr: false };
}
if (typeof mongodbLogPath === 'object' && typeof mongodbLogPath?.write === 'function') {
return { mongodbLogPath: mongodbLogPath, mongodbLogPathIsStdErr: false };
}
if (MONGODB_LOG_PATH && /^stderr$/i.test(MONGODB_LOG_PATH)) {
return { mongodbLogPath: createStdioLogger(process.stderr), mongodbLogPathIsStdErr: true };
}
if (MONGODB_LOG_PATH && /^stdout$/i.test(MONGODB_LOG_PATH)) {
return { mongodbLogPath: createStdioLogger(process.stdout), mongodbLogPathIsStdErr: false };
}
return { mongodbLogPath: createStdioLogger(process.stderr), mongodbLogPathIsStdErr: true };
}
function resolveSeverityConfiguration(clientOption, environmentOption, defaultSeverity) {
return (parseSeverityFromString(clientOption) ??
parseSeverityFromString(environmentOption) ??
defaultSeverity);
}
function compareSeverity(s0, s1) {
const s0Num = exports.SEVERITY_LEVEL_MAP.getNumericSeverityLevel(s0);
const s1Num = exports.SEVERITY_LEVEL_MAP.getNumericSeverityLevel(s1);
return s0Num < s1Num ? -1 : s0Num > s1Num ? 1 : 0;
}
/** @internal */
function stringifyWithMaxLen(value, maxDocumentLength, options = {}) {
let strToTruncate = '';
if (typeof value === 'string') {
strToTruncate = value;
}
else if (typeof value === 'function') {
strToTruncate = value.name;
}
else {
try {
strToTruncate = bson_1.EJSON.stringify(value, options);
}
catch (e) {
strToTruncate = `Extended JSON serialization failed with: ${e.message}`;
}
}
// handle truncation that occurs in the middle of multi-byte codepoints
if (maxDocumentLength !== 0 &&
strToTruncate.length > maxDocumentLength &&
strToTruncate.charCodeAt(maxDocumentLength - 1) !==
strToTruncate.codePointAt(maxDocumentLength - 1)) {
maxDocumentLength--;
if (maxDocumentLength === 0) {
return '';
}
}
return maxDocumentLength !== 0 && strToTruncate.length > maxDocumentLength
? `${strToTruncate.slice(0, maxDocumentLength)}...`
: strToTruncate;
}
exports.stringifyWithMaxLen = stringifyWithMaxLen;
function isLogConvertible(obj) {
const objAsLogConvertible = obj;
// eslint-disable-next-line no-restricted-syntax
return objAsLogConvertible.toLog !== undefined && typeof objAsLogConvertible.toLog === 'function';
}
function attachServerSelectionFields(log, serverSelectionEvent, maxDocumentLength = exports.DEFAULT_MAX_DOCUMENT_LENGTH) {
const { selector, operation, topologyDescription, message } = serverSelectionEvent;
log.selector = stringifyWithMaxLen(selector, maxDocumentLength);
log.operation = operation;
log.topologyDescription = stringifyWithMaxLen(topologyDescription, maxDocumentLength);
log.message = message;
return log;
}
function attachCommandFields(log, commandEvent) {
log.commandName = commandEvent.commandName;
log.requestId = commandEvent.requestId;
log.driverConnectionId = commandEvent.connectionId;
const { host, port } = utils_1.HostAddress.fromString(commandEvent.address).toHostPort();
log.serverHost = host;
log.serverPort = port;
if (commandEvent?.serviceId) {
log.serviceId = commandEvent.serviceId.toHexString();
}
log.databaseName = commandEvent.databaseName;
log.serverConnectionId = commandEvent.serverConnectionId;
return log;
}
function attachConnectionFields(log, event) {
const { host, port } = utils_1.HostAddress.fromString(event.address).toHostPort();
log.serverHost = host;
log.serverPort = port;
return log;
}
function attachSDAMFields(log, sdamEvent) {
log.topologyId = sdamEvent.topologyId;
return log;
}
function attachServerHeartbeatFields(log, serverHeartbeatEvent) {
const { awaited, connectionId } = serverHeartbeatEvent;
log.awaited = awaited;
log.driverConnectionId = serverHeartbeatEvent.connectionId;
const { host, port } = utils_1.HostAddress.fromString(connectionId).toHostPort();
log.serverHost = host;
log.serverPort = port;
return log;
}
/** @internal */
function defaultLogTransform(logObject, maxDocumentLength = exports.DEFAULT_MAX_DOCUMENT_LENGTH) {
let log = Object.create(null);
switch (logObject.name) {
case constants_1.SERVER_SELECTION_STARTED:
log = attachServerSelectionFields(log, logObject, maxDocumentLength);
return log;
case constants_1.SERVER_SELECTION_FAILED:
log = attachServerSelectionFields(log, logObject, maxDocumentLength);
log.failure = logObject.failure?.message;
return log;
case constants_1.SERVER_SELECTION_SUCCEEDED:
log = attachServerSelectionFields(log, logObject, maxDocumentLength);
log.serverHost = logObject.serverHost;
log.serverPort = logObject.serverPort;
return log;
case constants_1.WAITING_FOR_SUITABLE_SERVER:
log = attachServerSelectionFields(log, logObject, maxDocumentLength);
log.remainingTimeMS = logObject.remainingTimeMS;
return log;
case constants_1.COMMAND_STARTED:
log = attachCommandFields(log, logObject);
log.message = 'Command started';
log.command = stringifyWithMaxLen(logObject.command, maxDocumentLength, { relaxed: true });
log.databaseName = logObject.databaseName;
return log;
case constants_1.COMMAND_SUCCEEDED:
log = attachCommandFields(log, logObject);
log.message = 'Command succeeded';
log.durationMS = logObject.duration;
log.reply = stringifyWithMaxLen(logObject.reply, maxDocumentLength, { relaxed: true });
return log;
case constants_1.COMMAND_FAILED:
log = attachCommandFields(log, logObject);
log.message = 'Command failed';
log.durationMS = logObject.duration;
log.failure = logObject.failure?.message ?? '(redacted)';
return log;
case constants_1.CONNECTION_POOL_CREATED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection pool created';
if (logObject.options) {
const { maxIdleTimeMS, minPoolSize, maxPoolSize, maxConnecting, waitQueueTimeoutMS } = logObject.options;
log = {
...log,
maxIdleTimeMS,
minPoolSize,
maxPoolSize,
maxConnecting,
waitQueueTimeoutMS
};
}
return log;
case constants_1.CONNECTION_POOL_READY:
log = attachConnectionFields(log, logObject);
log.message = 'Connection pool ready';
return log;
case constants_1.CONNECTION_POOL_CLEARED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection pool cleared';
if (logObject.serviceId?._bsontype === 'ObjectId') {
log.serviceId = logObject.serviceId?.toHexString();
}
return log;
case constants_1.CONNECTION_POOL_CLOSED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection pool closed';
return log;
case constants_1.CONNECTION_CREATED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection created';
log.driverConnectionId = logObject.connectionId;
return log;
case constants_1.CONNECTION_READY:
log = attachConnectionFields(log, logObject);
log.message = 'Connection ready';
log.driverConnectionId = logObject.connectionId;
return log;
case constants_1.CONNECTION_CLOSED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection closed';
log.driverConnectionId = logObject.connectionId;
switch (logObject.reason) {
case 'stale':
log.reason = 'Connection became stale because the pool was cleared';
break;
case 'idle':
log.reason =
'Connection has been available but unused for longer than the configured max idle time';
break;
case 'error':
log.reason = 'An error occurred while using the connection';
if (logObject.error) {
log.error = logObject.error;
}
break;
case 'poolClosed':
log.reason = 'Connection pool was closed';
break;
default:
log.reason = `Unknown close reason: ${logObject.reason}`;
}
return log;
case constants_1.CONNECTION_CHECK_OUT_STARTED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection checkout started';
return log;
case constants_1.CONNECTION_CHECK_OUT_FAILED:
log = attachConnectionFields(log, logObject);
log.message = 'Connection checkout failed';
switch (logObject.reason) {
case 'poolClosed':
log.reason = 'Connection pool was closed';
break;
case 'timeout':
log.reason = 'Wait queue timeout elapsed without a connection becoming available';
break;
case 'connectionError':
log.reason = 'An error occurred while trying to establish a new connection';
if (logObject.error) {
log.error = logObject.error;
}
break;
default:
log.reason = `Unknown close reason: ${logObject.reason}`;
}
return log;
case constants_1.CONNECTION_CHECKED_OUT:
log = attachConnectionFields(log, logObject);
log.message = 'Connection checked out';
log.driverConnectionId = logObject.connectionId;
return log;
case constants_1.CONNECTION_CHECKED_IN:
log = attachConnectionFields(log, logObject);
log.message = 'Connection checked in';
log.driverConnectionId = logObject.connectionId;
return log;
case constants_1.SERVER_OPENING:
log = attachSDAMFields(log, logObject);
log = attachConnectionFields(log, logObject);
log.message = 'Starting server monitoring';
return log;
case constants_1.SERVER_CLOSED:
log = attachSDAMFields(log, logObject);
log = attachConnectionFields(log, logObject);
log.message = 'Stopped server monitoring';
return log;
case constants_1.SERVER_HEARTBEAT_STARTED:
log = attachSDAMFields(log, logObject);
log = attachServerHeartbeatFields(log, logObject);
log.message = 'Server heartbeat started';
return log;
case constants_1.SERVER_HEARTBEAT_SUCCEEDED:
log = attachSDAMFields(log, logObject);
log = attachServerHeartbeatFields(log, logObject);
log.message = 'Server heartbeat succeeded';
log.durationMS = logObject.duration;
log.serverConnectionId = logObject.serverConnectionId;
log.reply = stringifyWithMaxLen(logObject.reply, maxDocumentLength, { relaxed: true });
return log;
case constants_1.SERVER_HEARTBEAT_FAILED:
log = attachSDAMFields(log, logObject);
log = attachServerHeartbeatFields(log, logObject);
log.message = 'Server heartbeat failed';
log.durationMS = logObject.duration;
log.failure = logObject.failure?.message;
return log;
case constants_1.TOPOLOGY_OPENING:
log = attachSDAMFields(log, logObject);
log.message = 'Starting topology monitoring';
return log;
case constants_1.TOPOLOGY_CLOSED:
log = attachSDAMFields(log, logObject);
log.message = 'Stopped topology monitoring';
return log;
case constants_1.TOPOLOGY_DESCRIPTION_CHANGED:
log = attachSDAMFields(log, logObject);
log.message = 'Topology description changed';
log.previousDescription = log.reply = stringifyWithMaxLen(logObject.previousDescription, maxDocumentLength);
log.newDescription = log.reply = stringifyWithMaxLen(logObject.newDescription, maxDocumentLength);
return log;
default:
for (const [key, value] of Object.entries(logObject)) {
if (value != null)
log[key] = value;
}
}
return log;
}
exports.defaultLogTransform = defaultLogTransform;
/** @internal */
class MongoLogger {
constructor(options) {
this.pendingLog = null;
/**
* This method should be used when logging errors that do not have a public driver API for
* reporting errors.
*/
this.error = this.log.bind(this, 'error');
/**
* This method should be used to log situations where undesirable application behaviour might
* occur. For example, failing to end sessions on `MongoClient.close`.
*/
this.warn = this.log.bind(this, 'warn');
/**
* This method should be used to report high-level information about normal driver behaviour.
* For example, the creation of a `MongoClient`.
*/
this.info = this.log.bind(this, 'info');
/**
* This method should be used to report information that would be helpful when debugging an
* application. For example, a command starting, succeeding or failing.
*/
this.debug = this.log.bind(this, 'debug');
/**
* This method should be used to report fine-grained details related to logic flow. For example,
* entering and exiting a function body.
*/
this.trace = this.log.bind(this, 'trace');
this.componentSeverities = options.componentSeverities;
this.maxDocumentLength = options.maxDocumentLength;
this.logDestination = options.logDestination;
this.logDestinationIsStdErr = options.logDestinationIsStdErr;
this.severities = this.createLoggingSeverities();
}
createLoggingSeverities() {
const severities = Object();
for (const component of Object.values(exports.MongoLoggableComponent)) {
severities[component] = {};
for (const severityLevel of Object.values(exports.SeverityLevel)) {
severities[component][severityLevel] =
compareSeverity(severityLevel, this.componentSeverities[component]) <= 0;
}
}
return severities;
}
turnOffSeverities() {
for (const component of Object.values(exports.MongoLoggableComponent)) {
this.componentSeverities[component] = exports.SeverityLevel.OFF;
for (const severityLevel of Object.values(exports.SeverityLevel)) {
this.severities[component][severityLevel] = false;
}
}
}
logWriteFailureHandler(error) {
if (this.logDestinationIsStdErr) {
this.turnOffSeverities();
this.clearPendingLog();
return;
}
this.logDestination = createStdioLogger(process.stderr);
this.logDestinationIsStdErr = true;
this.clearPendingLog();
this.error(exports.MongoLoggableComponent.CLIENT, {
toLog: function () {
return {
message: 'User input for mongodbLogPath is now invalid. Logging is halted.',
error: error.message
};
}
});
this.turnOffSeverities();
this.clearPendingLog();
}
clearPendingLog() {
this.pendingLog = null;
}
willLog(component, severity) {
if (severity === exports.SeverityLevel.OFF)
return false;
return this.severities[component][severity];
}
log(severity, component, message) {
if (!this.willLog(component, severity))
return;
let logMessage = { t: new Date(), c: component, s: severity };
if (typeof message === 'string') {
logMessage.message = message;
}
else if (typeof message === 'object') {
if (isLogConvertible(message)) {
logMessage = { ...logMessage, ...message.toLog() };
}
else {
logMessage = { ...logMessage, ...defaultLogTransform(message, this.maxDocumentLength) };
}
}
if ((0, utils_1.isPromiseLike)(this.pendingLog)) {
this.pendingLog = this.pendingLog
// eslint-disable-next-line github/no-then
.then(() => this.logDestination.write(logMessage))
// eslint-disable-next-line github/no-then
.then(this.clearPendingLog.bind(this), this.logWriteFailureHandler.bind(this));
return;
}
try {
const logResult = this.logDestination.write(logMessage);
if ((0, utils_1.isPromiseLike)(logResult)) {
// eslint-disable-next-line github/no-then
this.pendingLog = logResult.then(this.clearPendingLog.bind(this), this.logWriteFailureHandler.bind(this));
}
}
catch (error) {
this.logWriteFailureHandler(error);
}
}
/**
* Merges options set through environment variables and the MongoClient, preferring environment
* variables when both are set, and substituting defaults for values not set. Options set in
* constructor take precedence over both environment variables and MongoClient options.
*
* @remarks
* When parsing component severity levels, invalid values are treated as unset and replaced with
* the default severity.
*
* @param envOptions - options set for the logger from the environment
* @param clientOptions - options set for the logger in the MongoClient options
* @returns a MongoLoggerOptions object to be used when instantiating a new MongoLogger
*/
static resolveOptions(envOptions, clientOptions) {
// client options take precedence over env options
const resolvedLogPath = resolveLogPath(envOptions, clientOptions);
const combinedOptions = {
...envOptions,
...clientOptions,
mongodbLogPath: resolvedLogPath.mongodbLogPath,
mongodbLogPathIsStdErr: resolvedLogPath.mongodbLogPathIsStdErr
};
const defaultSeverity = resolveSeverityConfiguration(combinedOptions.mongodbLogComponentSeverities?.default, combinedOptions.MONGODB_LOG_ALL, exports.SeverityLevel.OFF);
return {
componentSeverities: {
command: resolveSeverityConfiguration(combinedOptions.mongodbLogComponentSeverities?.command, combinedOptions.MONGODB_LOG_COMMAND, defaultSeverity),
topology: resolveSeverityConfiguration(combinedOptions.mongodbLogComponentSeverities?.topology, combinedOptions.MONGODB_LOG_TOPOLOGY, defaultSeverity),
serverSelection: resolveSeverityConfiguration(combinedOptions.mongodbLogComponentSeverities?.serverSelection, combinedOptions.MONGODB_LOG_SERVER_SELECTION, defaultSeverity),
connection: resolveSeverityConfiguration(combinedOptions.mongodbLogComponentSeverities?.connection, combinedOptions.MONGODB_LOG_CONNECTION, defaultSeverity),
client: resolveSeverityConfiguration(combinedOptions.mongodbLogComponentSeverities?.client, combinedOptions.MONGODB_LOG_CLIENT, defaultSeverity),
default: defaultSeverity
},
maxDocumentLength: combinedOptions.mongodbLogMaxDocumentLength ??
(0, utils_1.parseUnsignedInteger)(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ??
1000,
logDestination: combinedOptions.mongodbLogPath,
logDestinationIsStdErr: combinedOptions.mongodbLogPathIsStdErr
};
}
}
exports.MongoLogger = MongoLogger;
//# sourceMappingURL=mongo_logger.js.map |