Skip to content

Commit

Permalink
feat(NODE-4878): Add remaining log configurable client options (#3908)
Browse files Browse the repository at this point in the history
Co-authored-by: Warren James <[email protected]>
Co-authored-by: Durran Jordan <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2023
1 parent c0506b1 commit 54adc9f
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 64 deletions.
16 changes: 14 additions & 2 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ export function parseOptions(
...mongoOptions[Symbol.for('@@mdb.internalLoggerConfig')]
};
loggerClientOptions = {
mongodbLogPath: mongoOptions.mongodbLogPath
mongodbLogPath: mongoOptions.mongodbLogPath,
mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities,
mongodbLogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength
};
}
mongoOptions.mongoLoggerOptions = MongoLogger.resolveOptions(
Expand Down Expand Up @@ -1229,7 +1231,17 @@ export const OPTIONS = {
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogPath: { type: 'any' }
mongodbLogPath: { type: 'any' },
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogComponentSeverities: { type: 'any' },
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogMaxDocumentLength: { type: 'uint' }
} as Record<keyof MongoClientOptions, OptionDescriptor>;

export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export type {
} from './mongo_client';
export type {
Log,
LogComponentSeveritiesClientOptions,
LogConvertible,
Loggable,
LoggableEvent,
Expand Down
17 changes: 16 additions & 1 deletion src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { MONGO_CLIENT_EVENTS } from './constants';
import { Db, type DbOptions } from './db';
import type { Encrypter } from './encrypter';
import { MongoInvalidArgumentError } from './error';
import { type MongoDBLogWritable, MongoLogger, type MongoLoggerOptions } from './mongo_logger';
import {
type LogComponentSeveritiesClientOptions,
type MongoDBLogWritable,
MongoLogger,
type MongoLoggerOptions
} from './mongo_logger';
import { TypedEventEmitter } from './mongo_types';
import { executeOperation } from './operations/execute_operation';
import { RunAdminCommandOperation } from './operations/run_command';
Expand Down Expand Up @@ -262,6 +267,16 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions;
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogMaxDocumentLength?: number;

/** @internal */
[featureFlag: symbol]: any;
Expand Down
76 changes: 65 additions & 11 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,30 @@ export interface MongoLoggerEnvOptions {
MONGODB_LOG_PATH?: string;
}

/** @internal */
export interface LogComponentSeveritiesClientOptions {
/** Optional severity level for command component */
command?: SeverityLevel;
/** Optional severity level for topology component */
topology?: SeverityLevel;
/** Optionsl severity level for server selection component */
serverSelection?: SeverityLevel;
/** Optional severity level for connection component */
connection?: SeverityLevel;
/** Optional severity level for client component */
client?: SeverityLevel;
/** Optional default severity level to be used if any of the above are unset */
default?: SeverityLevel;
}

/** @internal */
export interface MongoLoggerMongoClientOptions {
/** Destination for log messages */
mongodbLogPath?: 'stdout' | 'stderr' | MongoDBLogWritable;
/** Severity levels for logger components */
mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions;
/** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */
mongodbLogMaxDocumentLength?: number;
}

/** @internal */
Expand All @@ -148,7 +168,6 @@ export interface MongoLoggerOptions {
/** Default severity level to be used if any of the above are unset */
default: SeverityLevel;
};

/** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */
maxDocumentLength: number;
/** Destination for log messages. */
Expand Down Expand Up @@ -219,6 +238,18 @@ function resolveLogPath(
return createStdioLogger(process.stderr);
}

function resolveSeverityConfiguration(
clientOption: string | undefined,
environmentOption: string | undefined,
defaultSeverity: SeverityLevel
): SeverityLevel {
return (
parseSeverityFromString(clientOption) ??
parseSeverityFromString(environmentOption) ??
defaultSeverity
);
}

/** @internal */
export interface Log extends Record<string, any> {
t: Date;
Expand Down Expand Up @@ -522,22 +553,45 @@ export class MongoLogger {
...clientOptions,
mongodbLogPath: resolveLogPath(envOptions, clientOptions)
};
const defaultSeverity =
parseSeverityFromString(combinedOptions.MONGODB_LOG_ALL) ?? SeverityLevel.OFF;
const defaultSeverity = resolveSeverityConfiguration(
combinedOptions.mongodbLogComponentSeverities?.default,
combinedOptions.MONGODB_LOG_ALL,
SeverityLevel.OFF
);

return {
componentSeverities: {
command: parseSeverityFromString(combinedOptions.MONGODB_LOG_COMMAND) ?? defaultSeverity,
topology: parseSeverityFromString(combinedOptions.MONGODB_LOG_TOPOLOGY) ?? defaultSeverity,
serverSelection:
parseSeverityFromString(combinedOptions.MONGODB_LOG_SERVER_SELECTION) ?? defaultSeverity,
connection:
parseSeverityFromString(combinedOptions.MONGODB_LOG_CONNECTION) ?? defaultSeverity,
client: parseSeverityFromString(combinedOptions.MONGODB_LOG_CLIENT) ?? defaultSeverity,
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:
parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ?? 1000,
combinedOptions.mongodbLogMaxDocumentLength ??
parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ??
1000,
logDestination: combinedOptions.mongodbLogPath
};
}
Expand Down
Loading

0 comments on commit 54adc9f

Please sign in to comment.