Skip to content

Commit

Permalink
test: add test for MongoLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina committed Nov 22, 2022
1 parent 85fd61a commit 395da6c
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 180 deletions.
32 changes: 16 additions & 16 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {
MongoMissingCredentialsError,
MongoParseError
} from './error';
import { Logger, LoggerLevel } from './logger';
import { extractLoggerOptions, Logger as LoggingApi, SeverityLevel } from './logging_api';
import { Logger as LegacyLogger, LoggerLevel as LegacyLoggerLevel } from './logger';
import {
DriverInfo,
MongoClient,
Expand All @@ -25,6 +24,7 @@ import {
ServerApi,
ServerApiVersion
} from './mongo_client';
import { Logger, SeverityLevel } from './mongo_logger';
import { PromiseProvider } from './promise_provider';
import { ReadConcern, ReadConcernLevel } from './read_concern';
import { ReadPreference, ReadPreferenceMode } from './read_preference';
Expand Down Expand Up @@ -207,7 +207,7 @@ function getInt(name: string, value: unknown): number {
throw new MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`);
}

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

const loggingApiMongoClientOptions = { mongodbLogPath: mongoOptions.mongodbLogPath };
const loggingApiOptions = extractLoggerOptions(loggingApiMongoClientOptions);
const loggerMongoClientOptions = { mongodbLogPath: mongoOptions.mongodbLogPath };
const loggerOptions = Logger.resolveOptions(loggerMongoClientOptions);
const loggingComponents = [
loggingApiOptions.commandSeverity,
loggingApiOptions.topologySeverity,
loggingApiOptions.serverSelectionSeverity,
loggingApiOptions.connectionSeverity,
loggingApiOptions.defaultSeverity
loggerOptions.command,
loggerOptions.topology,
loggerOptions.serverSelection,
loggerOptions.connection,
loggerOptions.defaultSeverity
];

if (loggingComponents.some(severity => severity !== SeverityLevel.OFF)) {
mongoOptions.loggingApi = new LoggingApi(loggingApiOptions);
mongoOptions.mongoLogger = new Logger(loggerOptions);
}

return mongoOptions;
Expand Down Expand Up @@ -864,9 +864,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 @@ -878,11 +878,11 @@ 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 });
}
},
loggingApi: {
target: 'loggingApi'
mongoLogger: {
target: 'mongoLogger'
},
maxConnecting: {
default: 2,
Expand Down
2 changes: 0 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export interface LoggerOptions {

/**
* @public
*
* TODO(andymina): add docs
*/
export class Logger {
className: string;
Expand Down
153 changes: 0 additions & 153 deletions src/logging_api.ts

This file was deleted.

21 changes: 13 additions & 8 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +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 LoggingApi } from './logging_api';
import type { Logger as LegacyLogger, LoggerLevel as LegacyLoggerLevel } from './logger';
import type { Logger } 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 @@ -234,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 @@ -297,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 @@ -335,6 +335,8 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
s: MongoClientPrivate;
/** @internal */
topology?: Topology;
/** @internal */
readonly mongoLogger: Logger | null;

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

this[kOptions] = parseOptions(url, this, options);
this.mongoLogger = this[kOptions].mongoLogger ?? null;

// eslint-disable-next-line @typescript-eslint/no-this-alias
const client = this;
Expand Down Expand Up @@ -418,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 @@ -709,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 @@ -773,7 +776,6 @@ export interface MongoOptions
proxyPort?: number;
proxyUsername?: string;
proxyPassword?: string;
loggingApi?: LoggingApi;
/** @internal */
connectionType?: typeof Connection;

Expand Down Expand Up @@ -805,4 +807,7 @@ export interface MongoOptions

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

/** @internal */
mongoLogger?: Logger;
}

0 comments on commit 395da6c

Please sign in to comment.