Skip to content
8 changes: 6 additions & 2 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,18 @@ export class MongoLogger {
maxDocumentLength: number;
logDestination: MongoDBLogWritable | Writable;

error = this.log.bind(this, 'error');
warn = this.log.bind(this, 'warn');
info = this.log.bind(this, 'info');
debug = this.log.bind(this, 'debug');
trace = this.log.bind(this, 'trace');

constructor(options: MongoLoggerOptions) {
this.componentSeverities = options.componentSeverities;
this.maxDocumentLength = options.maxDocumentLength;
this.logDestination = options.logDestination;
}

emergency = this.log.bind(this, 'emergency');

private log(
severity: SeverityLevel,
component: MongoLoggableComponent,
Expand Down
85 changes: 36 additions & 49 deletions test/unit/mongo_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
MongoDBLogWritable,
MongoLogger,
MongoLoggerOptions,
SEVERITY_LEVEL_MAP,
SeverityLevel
} from '../mongodb';

Expand Down Expand Up @@ -79,11 +78,11 @@ describe('class MongoLogger', function () {
}
} as { buffer: any[]; write: (log: Log) => void };
const logger = new MongoLogger({
componentSeverities: { command: 'emergency' } as any,
componentSeverities: { command: 'error' } as any,
logDestination
} as any);

logger.emergency('command', 'Hello world!');
logger.error('command', 'Hello world!');
expect(logDestination.buffer).to.have.lengthOf(1);
});
});
Expand All @@ -99,11 +98,11 @@ describe('class MongoLogger', function () {
});

const logger = new MongoLogger({
componentSeverities: { command: 'emergency' } as any,
componentSeverities: { command: 'error' } as any,
logDestination
} as any);

logger.emergency('command', 'Hello world!');
logger.error('command', 'Hello world!');
expect(buffer).to.have.lengthOf(1);
});
});
Expand All @@ -119,8 +118,8 @@ describe('class MongoLogger', function () {
]);

function* makeValidOptions(): Generator<[string, string]> {
const validOptions = Object.values(SeverityLevel).filter(
option => option !== SeverityLevel.OFF
const validOptions = Object.values(SeverityLevel).filter(option =>
['error', 'warn', 'info', 'debug', 'trace'].includes(option)
);
for (const option of validOptions) {
yield [option, option];
Expand Down Expand Up @@ -427,11 +426,11 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: unsetEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: unsetOption as any }
);
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);

expect(stderrStub.write).to.have.been.calledOnceWith(
Expand All @@ -449,11 +448,11 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: unsetEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: invalidOption as any }
);
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);

expect(stderrStub.write).to.have.been.calledOnceWith(
Expand All @@ -471,12 +470,12 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: unsetEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: validOption as any }
);

const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);
const correctDestination = validOptions.get(validOption);
expect(correctDestination?.write).to.have.been.calledOnce;
Expand All @@ -496,11 +495,11 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: invalidEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: unsetClientOption as any }
);
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);

expect(stderrStub.write).to.have.been.calledOnceWith(
Expand All @@ -520,11 +519,11 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: invalidEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: invalidOption as any }
);
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);

expect(stderrStub.write).to.have.been.calledOnceWith(
Expand All @@ -543,12 +542,12 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: invalidEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: validOption as any }
);
const correctDestination = validOptions.get(validOption);
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);
expect(correctDestination?.write).to.have.been.calledOnce;
});
Expand All @@ -566,12 +565,12 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: validEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: unsetOption as any }
);
const correctDestination = validOptions.get(validEnvironmentOption);
options.logDestination.write({ t: new Date(), c: 'command', s: 'emergency' });
options.logDestination.write({ t: new Date(), c: 'command', s: 'error' });

expect(correctDestination?.write).to.have.been.calledOnce;
});
Expand All @@ -588,13 +587,13 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: validEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: invalidValue as any }
);

const correctDestination = validOptions.get(validEnvironmentOption);
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
const log: Log = { t: new Date(), c: 'command', s: 'error' };
options.logDestination.write(log);

expect(correctDestination?.write).to.have.been.calledOnce;
Expand All @@ -615,12 +614,12 @@ describe('class MongoLogger', function () {
const options = MongoLogger.resolveOptions(
{
MONGODB_LOG_PATH: validEnvironmentOption,
MONGODB_LOG_COMMAND: 'emergency'
MONGODB_LOG_COMMAND: 'error'
},
{ mongodbLogPath: validValue as any }
);
const correctDestination = validOptions.get(validValue);
options.logDestination.write({ t: new Date(), c: 'command', s: 'emergency' });
options.logDestination.write({ t: new Date(), c: 'command', s: 'error' });
expect(correctDestination?.write).to.have.been.calledOnce;
});
}
Expand All @@ -631,9 +630,10 @@ describe('class MongoLogger', function () {
});

describe('severity helpers', function () {
// TODO(NODE-4814): Ensure we test on all valid severity levels
const severities = Object.values(SeverityLevel).filter(severity => severity === 'emergency');
for (const severityLevel of severities) {
const severities: SeverityLevel[] = Object.values(SeverityLevel).filter(severity =>
['error', 'warn', 'info', 'debug', 'trace'].includes(severity)
);
for (const [index, severityLevel] of severities.entries()) {
describe(`${severityLevel}()`, function () {
it('does not log when logging for the component is disabled', () => {
const stream = new BufferingStream();
Expand All @@ -648,8 +648,7 @@ describe('class MongoLogger', function () {
expect(stream.buffer).to.have.lengthOf(0);
});

// TODO(NODE-4814): Unskip this test
context.skip('when the log severity is greater than what was configured', function () {
context('when the log severity is greater than what was configured', function () {
it('does not write to logDestination', function () {
const stream = new BufferingStream();
const logger = new MongoLogger({
Expand All @@ -659,14 +658,9 @@ describe('class MongoLogger', function () {
logDestination: stream
} as any);

const TRACE = 8;
for (
let l = SEVERITY_LEVEL_MAP.getNumericSeverityLevel(severityLevel) + 1;
l <= TRACE;
l++
) {
const severity = SEVERITY_LEVEL_MAP.getSeverityLevelName(l);
logger[severity as SeverityLevel]('command', 'Hello');
for (let i = index + 1; i < severities.length; i++) {
const severity = severities[i];
logger[severity]('command', 'Hello');
}

expect(stream.buffer).to.have.lengthOf(0);
Expand All @@ -683,20 +677,13 @@ describe('class MongoLogger', function () {
logDestination: stream
} as any);

const EMERGENCY = 0;
// Calls all severity logging methods with a level less than or equal to what severityLevel
for (
let l = SEVERITY_LEVEL_MAP.getNumericSeverityLevel(severityLevel);
l >= EMERGENCY;
l--
) {
const severity = SEVERITY_LEVEL_MAP.getSeverityLevelName(l);
logger[severity as SeverityLevel]('command', 'Hello');
for (let i = index; i >= 0; i--) {
const severity = severities[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we type severities as SeverityLevel[], we should be able to remove the cast in line 683

logger[severity]('command', 'Hello');
}

expect(stream.buffer).to.have.lengthOf(
SEVERITY_LEVEL_MAP.getNumericSeverityLevel(severityLevel) + 1
);
expect(stream.buffer).to.have.lengthOf(index + 1);
});
});

Expand Down