Skip to content

Commit d7a8079

Browse files
authored
feat(NODE-4814): implement remaining severity logging methods (#3629)
1 parent 3b58ca1 commit d7a8079

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

src/mongo_logger.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,38 @@ export class MongoLogger {
442442
maxDocumentLength: number;
443443
logDestination: MongoDBLogWritable | Writable;
444444

445+
/**
446+
* This method should be used when logging errors that do not have a public driver API for
447+
* reporting errors.
448+
*/
449+
error = this.log.bind(this, 'error');
450+
/**
451+
* This method should be used to log situations where undesirable application behaviour might
452+
* occur. For example, failing to end sessions on `MongoClient.close`.
453+
*/
454+
warn = this.log.bind(this, 'warn');
455+
/**
456+
* This method should be used to report high-level information about normal driver behaviour.
457+
* For example, the creation of a `MongoClient`.
458+
*/
459+
info = this.log.bind(this, 'info');
460+
/**
461+
* This method should be used to report information that would be helpful when debugging an
462+
* application. For example, a command starting, succeeding or failing.
463+
*/
464+
debug = this.log.bind(this, 'debug');
465+
/**
466+
* This method should be used to report fine-grained details related to logic flow. For example,
467+
* entering and exiting a function body.
468+
*/
469+
trace = this.log.bind(this, 'trace');
470+
445471
constructor(options: MongoLoggerOptions) {
446472
this.componentSeverities = options.componentSeverities;
447473
this.maxDocumentLength = options.maxDocumentLength;
448474
this.logDestination = options.logDestination;
449475
}
450476

451-
emergency = this.log.bind(this, 'emergency');
452-
453477
private log(
454478
severity: SeverityLevel,
455479
component: MongoLoggableComponent,

test/unit/mongo_logger.test.ts

+36-49
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
MongoDBLogWritable,
2525
MongoLogger,
2626
MongoLoggerOptions,
27-
SEVERITY_LEVEL_MAP,
2827
SeverityLevel,
2928
stringifyWithMaxLen
3029
} from '../mongodb';
@@ -81,11 +80,11 @@ describe('class MongoLogger', function () {
8180
}
8281
} as { buffer: any[]; write: (log: Log) => void };
8382
const logger = new MongoLogger({
84-
componentSeverities: { command: 'emergency' } as any,
83+
componentSeverities: { command: 'error' } as any,
8584
logDestination
8685
} as any);
8786

88-
logger.emergency('command', 'Hello world!');
87+
logger.error('command', 'Hello world!');
8988
expect(logDestination.buffer).to.have.lengthOf(1);
9089
});
9190
});
@@ -101,11 +100,11 @@ describe('class MongoLogger', function () {
101100
});
102101

103102
const logger = new MongoLogger({
104-
componentSeverities: { command: 'emergency' } as any,
103+
componentSeverities: { command: 'error' } as any,
105104
logDestination
106105
} as any);
107106

108-
logger.emergency('command', 'Hello world!');
107+
logger.error('command', 'Hello world!');
109108
expect(buffer).to.have.lengthOf(1);
110109
});
111110
});
@@ -121,8 +120,8 @@ describe('class MongoLogger', function () {
121120
]);
122121

123122
function* makeValidOptions(): Generator<[string, string]> {
124-
const validOptions = Object.values(SeverityLevel).filter(
125-
option => option !== SeverityLevel.OFF
123+
const validOptions = Object.values(SeverityLevel).filter(option =>
124+
['error', 'warn', 'info', 'debug', 'trace'].includes(option)
126125
);
127126
for (const option of validOptions) {
128127
yield [option, option];
@@ -429,11 +428,11 @@ describe('class MongoLogger', function () {
429428
const options = MongoLogger.resolveOptions(
430429
{
431430
MONGODB_LOG_PATH: unsetEnvironmentOption,
432-
MONGODB_LOG_COMMAND: 'emergency'
431+
MONGODB_LOG_COMMAND: 'error'
433432
},
434433
{ mongodbLogPath: unsetOption as any }
435434
);
436-
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
435+
const log: Log = { t: new Date(), c: 'command', s: 'error' };
437436
options.logDestination.write(log);
438437

439438
expect(stderrStub.write).to.have.been.calledOnceWith(
@@ -451,11 +450,11 @@ describe('class MongoLogger', function () {
451450
const options = MongoLogger.resolveOptions(
452451
{
453452
MONGODB_LOG_PATH: unsetEnvironmentOption,
454-
MONGODB_LOG_COMMAND: 'emergency'
453+
MONGODB_LOG_COMMAND: 'error'
455454
},
456455
{ mongodbLogPath: invalidOption as any }
457456
);
458-
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
457+
const log: Log = { t: new Date(), c: 'command', s: 'error' };
459458
options.logDestination.write(log);
460459

461460
expect(stderrStub.write).to.have.been.calledOnceWith(
@@ -473,12 +472,12 @@ describe('class MongoLogger', function () {
473472
const options = MongoLogger.resolveOptions(
474473
{
475474
MONGODB_LOG_PATH: unsetEnvironmentOption,
476-
MONGODB_LOG_COMMAND: 'emergency'
475+
MONGODB_LOG_COMMAND: 'error'
477476
},
478477
{ mongodbLogPath: validOption as any }
479478
);
480479

481-
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
480+
const log: Log = { t: new Date(), c: 'command', s: 'error' };
482481
options.logDestination.write(log);
483482
const correctDestination = validOptions.get(validOption);
484483
expect(correctDestination?.write).to.have.been.calledOnce;
@@ -498,11 +497,11 @@ describe('class MongoLogger', function () {
498497
const options = MongoLogger.resolveOptions(
499498
{
500499
MONGODB_LOG_PATH: invalidEnvironmentOption,
501-
MONGODB_LOG_COMMAND: 'emergency'
500+
MONGODB_LOG_COMMAND: 'error'
502501
},
503502
{ mongodbLogPath: unsetClientOption as any }
504503
);
505-
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
504+
const log: Log = { t: new Date(), c: 'command', s: 'error' };
506505
options.logDestination.write(log);
507506

508507
expect(stderrStub.write).to.have.been.calledOnceWith(
@@ -522,11 +521,11 @@ describe('class MongoLogger', function () {
522521
const options = MongoLogger.resolveOptions(
523522
{
524523
MONGODB_LOG_PATH: invalidEnvironmentOption,
525-
MONGODB_LOG_COMMAND: 'emergency'
524+
MONGODB_LOG_COMMAND: 'error'
526525
},
527526
{ mongodbLogPath: invalidOption as any }
528527
);
529-
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
528+
const log: Log = { t: new Date(), c: 'command', s: 'error' };
530529
options.logDestination.write(log);
531530

532531
expect(stderrStub.write).to.have.been.calledOnceWith(
@@ -545,12 +544,12 @@ describe('class MongoLogger', function () {
545544
const options = MongoLogger.resolveOptions(
546545
{
547546
MONGODB_LOG_PATH: invalidEnvironmentOption,
548-
MONGODB_LOG_COMMAND: 'emergency'
547+
MONGODB_LOG_COMMAND: 'error'
549548
},
550549
{ mongodbLogPath: validOption as any }
551550
);
552551
const correctDestination = validOptions.get(validOption);
553-
const log: Log = { t: new Date(), c: 'command', s: 'emergency' };
552+
const log: Log = { t: new Date(), c: 'command', s: 'error' };
554553
options.logDestination.write(log);
555554
expect(correctDestination?.write).to.have.been.calledOnce;
556555
});
@@ -568,12 +567,12 @@ describe('class MongoLogger', function () {
568567
const options = MongoLogger.resolveOptions(
569568
{
570569
MONGODB_LOG_PATH: validEnvironmentOption,
571-
MONGODB_LOG_COMMAND: 'emergency'
570+
MONGODB_LOG_COMMAND: 'error'
572571
},
573572
{ mongodbLogPath: unsetOption as any }
574573
);
575574
const correctDestination = validOptions.get(validEnvironmentOption);
576-
options.logDestination.write({ t: new Date(), c: 'command', s: 'emergency' });
575+
options.logDestination.write({ t: new Date(), c: 'command', s: 'error' });
577576

578577
expect(correctDestination?.write).to.have.been.calledOnce;
579578
});
@@ -590,13 +589,13 @@ describe('class MongoLogger', function () {
590589
const options = MongoLogger.resolveOptions(
591590
{
592591
MONGODB_LOG_PATH: validEnvironmentOption,
593-
MONGODB_LOG_COMMAND: 'emergency'
592+
MONGODB_LOG_COMMAND: 'error'
594593
},
595594
{ mongodbLogPath: invalidValue as any }
596595
);
597596

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

602601
expect(correctDestination?.write).to.have.been.calledOnce;
@@ -617,12 +616,12 @@ describe('class MongoLogger', function () {
617616
const options = MongoLogger.resolveOptions(
618617
{
619618
MONGODB_LOG_PATH: validEnvironmentOption,
620-
MONGODB_LOG_COMMAND: 'emergency'
619+
MONGODB_LOG_COMMAND: 'error'
621620
},
622621
{ mongodbLogPath: validValue as any }
623622
);
624623
const correctDestination = validOptions.get(validValue);
625-
options.logDestination.write({ t: new Date(), c: 'command', s: 'emergency' });
624+
options.logDestination.write({ t: new Date(), c: 'command', s: 'error' });
626625
expect(correctDestination?.write).to.have.been.calledOnce;
627626
});
628627
}
@@ -633,9 +632,10 @@ describe('class MongoLogger', function () {
633632
});
634633

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

653-
// TODO(NODE-4814): Unskip this test
654-
context.skip('when the log severity is greater than what was configured', function () {
653+
context('when the log severity is greater than what was configured', function () {
655654
it('does not write to logDestination', function () {
656655
const stream = new BufferingStream();
657656
const logger = new MongoLogger({
@@ -661,14 +660,9 @@ describe('class MongoLogger', function () {
661660
logDestination: stream
662661
} as any);
663662

664-
const TRACE = 8;
665-
for (
666-
let l = SEVERITY_LEVEL_MAP.getNumericSeverityLevel(severityLevel) + 1;
667-
l <= TRACE;
668-
l++
669-
) {
670-
const severity = SEVERITY_LEVEL_MAP.getSeverityLevelName(l);
671-
logger[severity as SeverityLevel]('command', 'Hello');
663+
for (let i = index + 1; i < severities.length; i++) {
664+
const severity = severities[i];
665+
logger[severity]('command', 'Hello');
672666
}
673667

674668
expect(stream.buffer).to.have.lengthOf(0);
@@ -685,20 +679,13 @@ describe('class MongoLogger', function () {
685679
logDestination: stream
686680
} as any);
687681

688-
const EMERGENCY = 0;
689682
// Calls all severity logging methods with a level less than or equal to what severityLevel
690-
for (
691-
let l = SEVERITY_LEVEL_MAP.getNumericSeverityLevel(severityLevel);
692-
l >= EMERGENCY;
693-
l--
694-
) {
695-
const severity = SEVERITY_LEVEL_MAP.getSeverityLevelName(l);
696-
logger[severity as SeverityLevel]('command', 'Hello');
683+
for (let i = index; i >= 0; i--) {
684+
const severity = severities[i];
685+
logger[severity]('command', 'Hello');
697686
}
698687

699-
expect(stream.buffer).to.have.lengthOf(
700-
SEVERITY_LEVEL_MAP.getNumericSeverityLevel(severityLevel) + 1
701-
);
688+
expect(stream.buffer).to.have.lengthOf(index + 1);
702689
});
703690
});
704691

0 commit comments

Comments
 (0)