Skip to content

Commit 1105d8b

Browse files
basic testing added, need default testing
1 parent e3801c6 commit 1105d8b

File tree

3 files changed

+134
-53
lines changed

3 files changed

+134
-53
lines changed

src/connection_string.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ export function parseOptions(
531531
};
532532
loggerClientOptions = {
533533
mongodbLogPath: mongoOptions.mongodbLogPath,
534-
mongodbLogComponentSeverities: mongoOptions.LogComponentSeverities,
534+
mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities,
535535
mongodblogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength
536536
};
537537
}
@@ -1231,7 +1231,17 @@ export const OPTIONS = {
12311231
* @internal
12321232
* TODO: NODE-5671 - remove internal flag
12331233
*/
1234-
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: 'string' }
12351245
} as Record<keyof MongoClientOptions, OptionDescriptor>;
12361246

12371247
export const DEFAULT_OPTIONS = new CaseInsensitiveMap(

src/mongo_client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
262262
* TODO: NODE-5671 - remove internal flag
263263
*/
264264
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
265+
/**
266+
* @internal
267+
* TODO: NODE-5671 - remove internal flag
268+
*/
269+
mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions;
270+
/**
271+
* @internal
272+
* TODO: NODE-5671 - remove internal flag
273+
*/
274+
mongodbLogMaxDocumentLength?: string;
265275

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

test/unit/mongo_client.test.js

Lines changed: 112 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const { ReadPreference } = require('../mongodb');
1313
const { MongoCredentials } = require('../mongodb');
1414
const { MongoClient, MongoParseError, ServerApiVersion } = require('../mongodb');
1515
const { MongoLogger } = require('../mongodb');
16+
const {
17+
SeverityLevel,
18+
MongoLoggableComponent,
19+
MongoLoggerEnvOptions
20+
// eslint-disable-next-line no-restricted-modules
21+
} = require('../../src/mongo_logger');
1622
const sinon = require('sinon');
1723
const { Writable } = require('stream');
1824

@@ -817,71 +823,126 @@ describe('MongoOptions', function () {
817823
});
818824
});
819825

820-
context('when mongodbLogPath is in options', function () {
826+
describe('logging client options', function () {
821827
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
828+
context('when mongodbLogPath is in options', function () {
829+
let stderrStub;
830+
let stdoutStub;
822831

823-
let stderrStub;
824-
let stdoutStub;
825-
826-
beforeEach(() => {
827-
stdoutStub = sinon.stub(process.stdout);
828-
stderrStub = sinon.stub(process.stderr);
829-
});
832+
beforeEach(() => {
833+
stdoutStub = sinon.stub(process.stdout);
834+
stderrStub = sinon.stub(process.stderr);
835+
});
830836

831-
afterEach(() => {
832-
sinon.restore();
833-
});
837+
afterEach(() => {
838+
sinon.restore();
839+
});
834840

835-
context('when option is `stderr`', function () {
836-
it('it is accessible through mongoLogger.logDestination', function () {
837-
const client = new MongoClient('mongodb://a/', {
838-
[loggerFeatureFlag]: true,
839-
mongodbLogPath: 'stderr'
841+
context('when option is `stderr`', function () {
842+
it('it is accessible through mongoLogger.logDestination', function () {
843+
const client = new MongoClient('mongodb://a/', {
844+
[loggerFeatureFlag]: true,
845+
mongodbLogPath: 'stderr'
846+
});
847+
const log = { t: new Date(), c: 'constructorStdErr', s: 'error' };
848+
client.options.mongoLoggerOptions.logDestination.write(log);
849+
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
840850
});
841-
const log = { t: new Date(), c: 'constructorStdErr', s: 'error' };
842-
client.options.mongoLoggerOptions.logDestination.write(log);
843-
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
844851
});
845-
});
846852

847-
context('when option is a MongoDBLogWritable stream', function () {
848-
it('it is accessible through mongoLogger.logDestination', function () {
849-
const writable = {
850-
buffer: [],
851-
write(log) {
852-
this.buffer.push(log);
853-
}
854-
};
855-
const client = new MongoClient('mongodb://a/', {
856-
[loggerFeatureFlag]: true,
857-
mongodbLogPath: writable
853+
context('when option is a MongoDBLogWritable stream', function () {
854+
it('it is accessible through mongoLogger.logDestination', function () {
855+
const writable = {
856+
buffer: [],
857+
write(log) {
858+
this.buffer.push(log);
859+
}
860+
};
861+
const client = new MongoClient('mongodb://a/', {
862+
[loggerFeatureFlag]: true,
863+
mongodbLogPath: writable
864+
});
865+
expect(client.options.mongoLoggerOptions.logDestination).to.deep.equal(writable);
858866
});
859-
expect(client.options.mongoLoggerOptions.logDestination).to.deep.equal(writable);
860867
});
861-
});
862868

863-
context('when option is `stdout`', function () {
864-
it('it is accessible through mongoLogger.logDestination', function () {
865-
const client = new MongoClient('mongodb://a/', {
866-
[loggerFeatureFlag]: true,
867-
mongodbLogPath: 'stdout'
869+
context('when option is `stdout`', function () {
870+
it('it is accessible through mongoLogger.logDestination', function () {
871+
const client = new MongoClient('mongodb://a/', {
872+
[loggerFeatureFlag]: true,
873+
mongodbLogPath: 'stdout'
874+
});
875+
const log = { t: new Date(), c: 'constructorStdOut', s: 'error' };
876+
client.options.mongoLoggerOptions.logDestination.write(log);
877+
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
868878
});
869-
const log = { t: new Date(), c: 'constructorStdOut', s: 'error' };
870-
client.options.mongoLoggerOptions.logDestination.write(log);
871-
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
872879
});
873-
});
874880

875-
context('when option is invalid', function () {
876-
it('it defaults to stderr', function () {
877-
const invalidOption = 'stdnothing';
878-
const client = new MongoClient('mongodb://a/', {
879-
[loggerFeatureFlag]: true,
880-
mongodbLogPath: invalidOption
881+
context('when option is invalid', function () {
882+
it('it defaults to stderr', function () {
883+
const invalidOption = 'stdnothing';
884+
const client = new MongoClient('mongodb://a/', {
885+
[loggerFeatureFlag]: true,
886+
mongodbLogPath: invalidOption
887+
});
888+
const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' };
889+
client.options.mongoLoggerOptions.logDestination.write(log);
890+
expect(stderrStub.write).calledWith(
891+
inspect(log, { breakLength: Infinity, compact: true })
892+
);
881893
});
882-
const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' };
883-
client.options.mongoLoggerOptions.logDestination.write(log);
884-
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
894+
});
895+
});
896+
describe.only('component severities', function () {
897+
const components = Object.values(MongoLoggableComponent);
898+
const env_component_names = [
899+
'MONGODB_LOG_COMMAND',
900+
'MONGODB_LOG_TOPOLOGY',
901+
'MONGODB_LOG_SERVER_SELECTION',
902+
'MONGODB_LOG_CONNECTION',
903+
'MONGODB_LOG_CLIENT'
904+
];
905+
context('when only client option is provided for', function () {
906+
for (let i = 0; i < components.length; i++) {
907+
it(`it stores severity levels for ${components[i]} component correctly`, function () {
908+
for (const severityLevel of Object.values(SeverityLevel)) {
909+
const client = new MongoClient('mongodb://a/', {
910+
[loggerFeatureFlag]: true,
911+
mongodbLogComponentSeverities: {
912+
[components[i]]: severityLevel
913+
}
914+
});
915+
expect(client.options.mongoLoggerOptions.componentSeverities[components[i]]).to.equal(
916+
severityLevel
917+
);
918+
}
919+
});
920+
}
921+
});
922+
context('when both client and environment option is provided', function () {
923+
for (let i = 0; i < components.length; i++) {
924+
beforeEach(function () {
925+
process.env[env_component_names[i]] = 'emergency';
926+
});
927+
928+
afterEach(function () {
929+
process.env[env_component_names[i]] = undefined;
930+
});
931+
932+
it(`it stores severity levels for ${components[i]} component correctly (client options have precedence)`, function () {
933+
for (const severityLevel of Object.values(SeverityLevel)) {
934+
const client = new MongoClient('mongodb://a/', {
935+
[loggerFeatureFlag]: true,
936+
mongodbLogComponentSeverities: {
937+
[components[i]]: severityLevel
938+
}
939+
});
940+
expect(client.options.mongoLoggerOptions.componentSeverities[components[i]]).to.equal(
941+
severityLevel
942+
);
943+
}
944+
});
945+
};
885946
});
886947
});
887948
});

0 commit comments

Comments
 (0)