Skip to content

Commit

Permalink
feat(NODE-4810): define the new Logger (#3475)
Browse files Browse the repository at this point in the history
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
  • Loading branch information
andymina and baileympearson committed Dec 15, 2022
1 parent dfcc3d9 commit 6ef11d7
Show file tree
Hide file tree
Showing 9 changed files with 934 additions and 25 deletions.
66 changes: 48 additions & 18 deletions src/connection_string.ts
Expand Up @@ -14,7 +14,7 @@ import {
MongoMissingCredentialsError,
MongoParseError
} from './error';
import { Logger, LoggerLevel } from './logger';
import { Logger as LegacyLogger, LoggerLevel as LegacyLoggerLevel } from './logger';
import {
DriverInfo,
MongoClient,
Expand All @@ -24,6 +24,7 @@ import {
ServerApi,
ServerApiVersion
} from './mongo_client';
import { MongoLogger, MongoLoggerEnvOptions, MongoLoggerMongoClientOptions } from './mongo_logger';
import { PromiseProvider } from './promise_provider';
import { ReadConcern, ReadConcernLevel } from './read_concern';
import { ReadPreference, ReadPreferenceMode } from './read_preference';
Expand All @@ -35,6 +36,7 @@ import {
HostAddress,
isRecord,
makeClientMetadata,
parseInteger,
setDifference
} from './utils';
import { W, WriteConcern } from './write_concern';
Expand Down Expand Up @@ -199,15 +201,16 @@ function getBoolean(name: string, value: unknown): boolean {
throw new MongoParseError(`Expected ${name} to be stringified boolean value, got: ${value}`);
}

function getInt(name: string, value: unknown): number {
if (typeof value === 'number') return Math.trunc(value);
const parsedValue = Number.parseInt(String(value), 10);
if (!Number.isNaN(parsedValue)) return parsedValue;
function getIntFromOptions(name: string, value: unknown): number {
const parsedInt = parseInteger(value);
if (parsedInt != null) {
return parsedInt;
}
throw new MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`);
}

function getUint(name: string, value: unknown): number {
const parsedValue = getInt(name, value);
function getUIntFromOptions(name: string, value: unknown): number {
const parsedValue = getIntFromOptions(name, value);
if (parsedValue < 0) {
throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`);
}
Expand Down Expand Up @@ -507,6 +510,30 @@ export function parseOptions(
);
}

const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
mongoOptions[loggerFeatureFlag] = mongoOptions[loggerFeatureFlag] ?? false;

let loggerEnvOptions: MongoLoggerEnvOptions = {};
let loggerClientOptions: MongoLoggerMongoClientOptions = {};
if (mongoOptions[loggerFeatureFlag]) {
loggerEnvOptions = {
MONGODB_LOG_COMMAND: process.env.MONGODB_LOG_COMMAND,
MONGODB_LOG_TOPOLOGY: process.env.MONGODB_LOG_TOPOLOGY,
MONGODB_LOG_SERVER_SELECTION: process.env.MONGODB_LOG_SERVER_SELECTION,
MONGODB_LOG_CONNECTION: process.env.MONGODB_LOG_CONNECTION,
MONGODB_LOG_ALL: process.env.MONGODB_LOG_ALL,
MONGODB_LOG_MAX_DOCUMENT_LENGTH: process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH,
MONGODB_LOG_PATH: process.env.MONGODB_LOG_PATH
};
loggerClientOptions = {
mongodbLogPath: mongoOptions.mongodbLogPath
};
}
mongoOptions.mongoLoggerOptions = MongoLogger.resolveOptions(
loggerEnvOptions,
loggerClientOptions
);

return mongoOptions;
}

Expand Down Expand Up @@ -561,10 +588,10 @@ function setOption(
mongoOptions[name] = getBoolean(name, values[0]);
break;
case 'int':
mongoOptions[name] = getInt(name, values[0]);
mongoOptions[name] = getIntFromOptions(name, values[0]);
break;
case 'uint':
mongoOptions[name] = getUint(name, values[0]);
mongoOptions[name] = getUIntFromOptions(name, values[0]);
break;
case 'string':
if (values[0] == null) {
Expand Down Expand Up @@ -770,7 +797,7 @@ export const OPTIONS = {
enableUtf8Validation: { type: 'boolean', default: true },
family: {
transform({ name, values: [value] }): 4 | 6 {
const transformValue = getInt(name, value);
const transformValue = getIntFromOptions(name, value);
if (transformValue === 4 || transformValue === 6) {
return transformValue;
}
Expand Down Expand Up @@ -849,9 +876,9 @@ export const OPTIONS = {
type: 'uint'
},
logger: {
default: new Logger('MongoClient'),
default: new LegacyLogger('MongoClient'),
transform({ values: [value] }) {
if (value instanceof Logger) {
if (value instanceof LegacyLogger) {
return value;
}
emitWarning('Alternative loggers might not be supported');
Expand All @@ -863,13 +890,13 @@ export const OPTIONS = {
loggerLevel: {
target: 'logger',
transform({ values: [value] }) {
return new Logger('MongoClient', { loggerLevel: value as LoggerLevel });
return new LegacyLogger('MongoClient', { loggerLevel: value as LegacyLoggerLevel });
}
},
maxConnecting: {
default: 2,
transform({ name, values: [value] }): number {
const maxConnecting = getUint(name, value);
const maxConnecting = getUIntFromOptions(name, value);
if (maxConnecting === 0) {
throw new MongoInvalidArgumentError('maxConnecting must be > 0 if specified');
}
Expand All @@ -887,7 +914,7 @@ export const OPTIONS = {
maxStalenessSeconds: {
target: 'readPreference',
transform({ name, options, values: [value] }) {
const maxStalenessSeconds = getUint(name, value);
const maxStalenessSeconds = getUIntFromOptions(name, value);
if (options.readPreference) {
return ReadPreference.fromOptions({
readPreference: { ...options.readPreference, maxStalenessSeconds }
Expand Down Expand Up @@ -1206,7 +1233,7 @@ export const OPTIONS = {
const wc = WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
wtimeout: getUint('wtimeout', value)
wtimeout: getUIntFromOptions('wtimeout', value)
}
});
if (wc) return wc;
Expand All @@ -1219,7 +1246,7 @@ export const OPTIONS = {
const wc = WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
wtimeoutMS: getUint('wtimeoutMS', value)
wtimeoutMS: getUIntFromOptions('wtimeoutMS', value)
}
});
if (wc) return wc;
Expand Down Expand Up @@ -1274,4 +1301,7 @@ export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
* Set of permitted feature flags
* @internal
*/
export const FEATURE_FLAGS = new Set([Symbol.for('@@mdb.skipPingOnConnect')]);
export const FEATURE_FLAGS = new Set([
Symbol.for('@@mdb.skipPingOnConnect'),
Symbol.for('@@mdb.enableMongoLogger')
]);
8 changes: 8 additions & 0 deletions src/index.ts
Expand Up @@ -300,6 +300,14 @@ export type {
SupportedTLSSocketOptions,
WithSessionCallback
} from './mongo_client';
export type {
MongoLoggableComponent,
MongoLogger,
MongoLoggerEnvOptions,
MongoLoggerMongoClientOptions,
MongoLoggerOptions,
SeverityLevel
} from './mongo_logger';
export type {
CommonEvents,
EventsDescription,
Expand Down
19 changes: 13 additions & 6 deletions src/mongo_client.ts
Expand Up @@ -15,7 +15,8 @@ import { Db, DbOptions } from './db';
import type { AutoEncrypter, AutoEncryptionOptions } from './deps';
import type { Encrypter } from './encrypter';
import { MongoInvalidArgumentError } from './error';
import type { Logger, LoggerLevel } from './logger';
import type { Logger as LegacyLogger, LoggerLevel as LegacyLoggerLevel } from './logger';
import { MongoLogger, MongoLoggerOptions } from './mongo_logger';
import { TypedEventEmitter } from './mongo_types';
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
import { ReadPreference, ReadPreferenceMode } from './read_preference';
Expand Down Expand Up @@ -233,9 +234,9 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
*/
promiseLibrary?: any;
/** The logging level */
loggerLevel?: LoggerLevel;
loggerLevel?: LegacyLoggerLevel;
/** Custom logger object */
logger?: Logger;
logger?: LegacyLogger;
/** Enable command monitoring for this client */
monitorCommands?: boolean;
/** Server API version */
Expand Down Expand Up @@ -296,7 +297,7 @@ export interface MongoClientPrivate {
readonly readConcern?: ReadConcern;
readonly writeConcern?: WriteConcern;
readonly readPreference: ReadPreference;
readonly logger: Logger;
readonly logger: LegacyLogger;
readonly isMongoClient: true;
}

Expand Down Expand Up @@ -334,6 +335,8 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
s: MongoClientPrivate;
/** @internal */
topology?: Topology;
/** @internal */
readonly mongoLogger: MongoLogger;

/**
* The consolidate, parsed, transformed and merged options.
Expand All @@ -345,6 +348,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
super();

this[kOptions] = parseOptions(url, this, options);
this.mongoLogger = new MongoLogger(this[kOptions].mongoLoggerOptions);

// eslint-disable-next-line @typescript-eslint/no-this-alias
const client = this;
Expand Down Expand Up @@ -417,7 +421,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
return this.s.bsonOptions;
}

get logger(): Logger {
get logger(): LegacyLogger {
return this.s.logger;
}

Expand Down Expand Up @@ -708,7 +712,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
}

/** Return the mongo client logger */
getLogger(): Logger {
getLogger(): LegacyLogger {
return this.s.logger;
}
}
Expand Down Expand Up @@ -803,4 +807,7 @@ export interface MongoOptions

/** @internal */
[featureFlag: symbol]: any;

/** @internal */
mongoLoggerOptions: MongoLoggerOptions;
}

0 comments on commit 6ef11d7

Please sign in to comment.