Skip to content

Commit 54adc9f

Browse files
aditi-khare-mongoDBW-A-Jamesdurran
authored
feat(NODE-4878): Add remaining log configurable client options (#3908)
Co-authored-by: Warren James <warren.james@mongodb.com> Co-authored-by: Durran Jordan <durran@gmail.com>
1 parent c0506b1 commit 54adc9f

File tree

5 files changed

+270
-64
lines changed

5 files changed

+270
-64
lines changed

src/connection_string.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ export function parseOptions(
530530
...mongoOptions[Symbol.for('@@mdb.internalLoggerConfig')]
531531
};
532532
loggerClientOptions = {
533-
mongodbLogPath: mongoOptions.mongodbLogPath
533+
mongodbLogPath: mongoOptions.mongodbLogPath,
534+
mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities,
535+
mongodbLogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength
534536
};
535537
}
536538
mongoOptions.mongoLoggerOptions = MongoLogger.resolveOptions(
@@ -1229,7 +1231,17 @@ export const OPTIONS = {
12291231
* @internal
12301232
* TODO: NODE-5671 - remove internal flag
12311233
*/
1232-
mongodbLogPath: { type: 'any' }
1234+
mongodbLogPath: { type: 'any' },
1235+
/**
1236+
* @internal
1237+
* TODO: NODE-5671 - remove internal flag
1238+
*/
1239+
mongodbLogComponentSeverities: { type: 'any' },
1240+
/**
1241+
* @internal
1242+
* TODO: NODE-5671 - remove internal flag
1243+
*/
1244+
mongodbLogMaxDocumentLength: { type: 'uint' }
12331245
} as Record<keyof MongoClientOptions, OptionDescriptor>;
12341246

12351247
export const DEFAULT_OPTIONS = new CaseInsensitiveMap(

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ export type {
353353
} from './mongo_client';
354354
export type {
355355
Log,
356+
LogComponentSeveritiesClientOptions,
356357
LogConvertible,
357358
Loggable,
358359
LoggableEvent,

src/mongo_client.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import { MONGO_CLIENT_EVENTS } from './constants';
2121
import { Db, type DbOptions } from './db';
2222
import type { Encrypter } from './encrypter';
2323
import { MongoInvalidArgumentError } from './error';
24-
import { type MongoDBLogWritable, MongoLogger, type MongoLoggerOptions } from './mongo_logger';
24+
import {
25+
type LogComponentSeveritiesClientOptions,
26+
type MongoDBLogWritable,
27+
MongoLogger,
28+
type MongoLoggerOptions
29+
} from './mongo_logger';
2530
import { TypedEventEmitter } from './mongo_types';
2631
import { executeOperation } from './operations/execute_operation';
2732
import { RunAdminCommandOperation } from './operations/run_command';
@@ -262,6 +267,16 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
262267
* TODO: NODE-5671 - remove internal flag
263268
*/
264269
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
270+
/**
271+
* @internal
272+
* TODO: NODE-5671 - remove internal flag
273+
*/
274+
mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions;
275+
/**
276+
* @internal
277+
* TODO: NODE-5671 - remove internal flag
278+
*/
279+
mongodbLogMaxDocumentLength?: number;
265280

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

src/mongo_logger.ts

+65-11
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,30 @@ export interface MongoLoggerEnvOptions {
126126
MONGODB_LOG_PATH?: string;
127127
}
128128

129+
/** @internal */
130+
export interface LogComponentSeveritiesClientOptions {
131+
/** Optional severity level for command component */
132+
command?: SeverityLevel;
133+
/** Optional severity level for topology component */
134+
topology?: SeverityLevel;
135+
/** Optionsl severity level for server selection component */
136+
serverSelection?: SeverityLevel;
137+
/** Optional severity level for connection component */
138+
connection?: SeverityLevel;
139+
/** Optional severity level for client component */
140+
client?: SeverityLevel;
141+
/** Optional default severity level to be used if any of the above are unset */
142+
default?: SeverityLevel;
143+
}
144+
129145
/** @internal */
130146
export interface MongoLoggerMongoClientOptions {
131147
/** Destination for log messages */
132148
mongodbLogPath?: 'stdout' | 'stderr' | MongoDBLogWritable;
149+
/** Severity levels for logger components */
150+
mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions;
151+
/** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */
152+
mongodbLogMaxDocumentLength?: number;
133153
}
134154

135155
/** @internal */
@@ -148,7 +168,6 @@ export interface MongoLoggerOptions {
148168
/** Default severity level to be used if any of the above are unset */
149169
default: SeverityLevel;
150170
};
151-
152171
/** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */
153172
maxDocumentLength: number;
154173
/** Destination for log messages. */
@@ -219,6 +238,18 @@ function resolveLogPath(
219238
return createStdioLogger(process.stderr);
220239
}
221240

241+
function resolveSeverityConfiguration(
242+
clientOption: string | undefined,
243+
environmentOption: string | undefined,
244+
defaultSeverity: SeverityLevel
245+
): SeverityLevel {
246+
return (
247+
parseSeverityFromString(clientOption) ??
248+
parseSeverityFromString(environmentOption) ??
249+
defaultSeverity
250+
);
251+
}
252+
222253
/** @internal */
223254
export interface Log extends Record<string, any> {
224255
t: Date;
@@ -522,22 +553,45 @@ export class MongoLogger {
522553
...clientOptions,
523554
mongodbLogPath: resolveLogPath(envOptions, clientOptions)
524555
};
525-
const defaultSeverity =
526-
parseSeverityFromString(combinedOptions.MONGODB_LOG_ALL) ?? SeverityLevel.OFF;
556+
const defaultSeverity = resolveSeverityConfiguration(
557+
combinedOptions.mongodbLogComponentSeverities?.default,
558+
combinedOptions.MONGODB_LOG_ALL,
559+
SeverityLevel.OFF
560+
);
527561

528562
return {
529563
componentSeverities: {
530-
command: parseSeverityFromString(combinedOptions.MONGODB_LOG_COMMAND) ?? defaultSeverity,
531-
topology: parseSeverityFromString(combinedOptions.MONGODB_LOG_TOPOLOGY) ?? defaultSeverity,
532-
serverSelection:
533-
parseSeverityFromString(combinedOptions.MONGODB_LOG_SERVER_SELECTION) ?? defaultSeverity,
534-
connection:
535-
parseSeverityFromString(combinedOptions.MONGODB_LOG_CONNECTION) ?? defaultSeverity,
536-
client: parseSeverityFromString(combinedOptions.MONGODB_LOG_CLIENT) ?? defaultSeverity,
564+
command: resolveSeverityConfiguration(
565+
combinedOptions.mongodbLogComponentSeverities?.command,
566+
combinedOptions.MONGODB_LOG_COMMAND,
567+
defaultSeverity
568+
),
569+
topology: resolveSeverityConfiguration(
570+
combinedOptions.mongodbLogComponentSeverities?.topology,
571+
combinedOptions.MONGODB_LOG_TOPOLOGY,
572+
defaultSeverity
573+
),
574+
serverSelection: resolveSeverityConfiguration(
575+
combinedOptions.mongodbLogComponentSeverities?.serverSelection,
576+
combinedOptions.MONGODB_LOG_SERVER_SELECTION,
577+
defaultSeverity
578+
),
579+
connection: resolveSeverityConfiguration(
580+
combinedOptions.mongodbLogComponentSeverities?.connection,
581+
combinedOptions.MONGODB_LOG_CONNECTION,
582+
defaultSeverity
583+
),
584+
client: resolveSeverityConfiguration(
585+
combinedOptions.mongodbLogComponentSeverities?.client,
586+
combinedOptions.MONGODB_LOG_CLIENT,
587+
defaultSeverity
588+
),
537589
default: defaultSeverity
538590
},
539591
maxDocumentLength:
540-
parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ?? 1000,
592+
combinedOptions.mongodbLogMaxDocumentLength ??
593+
parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ??
594+
1000,
541595
logDestination: combinedOptions.mongodbLogPath
542596
};
543597
}

0 commit comments

Comments
 (0)