Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4810): define the new Logger #3475

Merged
merged 51 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9eb25b1
feat: finish Logger env options parsing
andymina Nov 21, 2022
ff792fd
feat: implement new Logger constructor
andymina Nov 22, 2022
fae8706
feat: restructure Logger options
andymina Nov 22, 2022
b9d915c
chore: remove sandbox file
andymina Nov 22, 2022
c7f28f0
test: add test for MongoLogger
andymina Nov 22, 2022
6cdb0c3
refactor: use global process instead of importing
andymina Nov 22, 2022
36ee2ad
refactor: rename logger to MongoLogger and add docs
andymina Nov 23, 2022
007b109
test: use MongoLogger name in tests
andymina Nov 23, 2022
de11ba4
docs: add remarks tag
andymina Nov 23, 2022
5dd9b90
fix: correct typo in test
andymina Nov 23, 2022
07b8129
fix: address comments from review
andymina Nov 23, 2022
e62f042
test: restructure tests
andymina Nov 23, 2022
c24a6ec
test: add more tests for MongoLogger
andymina Nov 28, 2022
84a116f
refactor: revert changes to getInt and getUint
andymina Nov 28, 2022
6788243
fix: remove .only from tests
andymina Nov 28, 2022
24010a6
refactor: ignore invalid env vars
andymina Nov 29, 2022
7dafcfa
fix: remove unused imports
andymina Nov 29, 2022
d41bcda
refactor: only accept out or err for logDestination
andymina Nov 29, 2022
893e464
feat: add logger feature flag
andymina Nov 29, 2022
0b7b6e3
test: add feature flag tests
andymina Nov 29, 2022
0cfda96
fix: remove unused import in mongo_logger
andymina Nov 29, 2022
f66f973
fix: manually add feature flag to test
andymina Nov 29, 2022
159aac0
test: reformat testing structure
andymina Nov 30, 2022
c1c0421
test: restructure tests to use context
andymina Nov 30, 2022
1a022fe
refactor: address comments
andymina Dec 1, 2022
dca9796
refactor: make mongoLogger non-existent
andymina Dec 2, 2022
a69b493
test: refactor feature flag tests
andymina Dec 2, 2022
8db2be5
fix: adjust typo in tests
andymina Dec 2, 2022
88b42dd
Merge branch 'main' into NODE-4810
andymina Dec 2, 2022
c676b21
fix: remove .only from test
andymina Dec 2, 2022
3e1e33d
Update test/unit/mongo_logger.test.ts
baileympearson Dec 6, 2022
f236d55
refactor: clean up mongo_logger tests
baileympearson Dec 6, 2022
18db449
better name
baileympearson Dec 6, 2022
2ca2cac
add mongo client test
baileympearson Dec 6, 2022
73b3535
add feature flag tests
baileympearson Dec 6, 2022
a52493b
add severity helper stream tests when helpers are off
baileympearson Dec 6, 2022
e749110
clean up mongo logger
baileympearson Dec 6, 2022
81b62fd
Update test/integration/node-specific/feature_flags.test.ts
baileympearson Dec 7, 2022
a6eee37
Update test/integration/node-specific/feature_flags.test.ts
baileympearson Dec 7, 2022
1c86ce6
Update test/integration/node-specific/feature_flags.test.ts
baileympearson Dec 7, 2022
36bd259
cleanup test wording
baileympearson Dec 7, 2022
bd10d81
rework mongodb log destination tests
baileympearson Dec 7, 2022
67fd13d
add tests for componentSeverities
baileympearson Dec 8, 2022
85d598e
restructure mongo_logger file slightly
baileympearson Dec 8, 2022
fdcfc3e
chore: address Daria's comments in feature flags tests
baileympearson Dec 12, 2022
32f1f26
chore: address Daria's comments in mongo logger tests
baileympearson Dec 12, 2022
7b07723
remove unnecessary block
baileympearson Dec 13, 2022
5b18ef0
chore: add todo for writable in mongo_logger
baileympearson Dec 14, 2022
5262ca0
refactor uint parsing
baileympearson Dec 14, 2022
e72a8c1
fix imports
baileympearson Dec 14, 2022
997a7f2
better naming for parseInt and parseUInt
baileympearson Dec 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
Loading