1
- import { type Document , EJSON , type EJSONOptions } from 'bson' ;
2
1
import type { Writable } from 'stream' ;
3
2
import { inspect } from 'util' ;
4
3
5
- import type {
6
- CommandFailedEvent ,
7
- CommandStartedEvent ,
8
- CommandSucceededEvent
9
- } from './cmap/command_monitoring_events' ;
4
+ import { type Document , EJSON , type EJSONOptions , type ObjectId } from './bson' ;
5
+ import type { CommandStartedEvent } from './cmap/command_monitoring_events' ;
10
6
import type {
11
7
ConnectionCheckedInEvent ,
12
8
ConnectionCheckedOutEvent ,
@@ -295,6 +291,40 @@ function compareSeverity(s0: SeverityLevel, s1: SeverityLevel): 1 | 0 | -1 {
295
291
return s0Num < s1Num ? - 1 : s0Num > s1Num ? 1 : 0 ;
296
292
}
297
293
294
+ /**
295
+ * @internal
296
+ * Must be separate from Events API due to differences in spec requirements for logging a command success
297
+ */
298
+ export type LoggableCommandSucceededEvent = {
299
+ address : string ;
300
+ connectionId ?: string | number ;
301
+ requestId : number ;
302
+ duration : number ;
303
+ commandName : string ;
304
+ reply : Document | undefined ;
305
+ serviceId ?: ObjectId ;
306
+ name : typeof COMMAND_SUCCEEDED ;
307
+ serverConnectionId : bigint | null ;
308
+ databaseName : string ;
309
+ } ;
310
+
311
+ /**
312
+ * @internal
313
+ * Must be separate from Events API due to differences in spec requirements for logging a command failure
314
+ */
315
+ export type LoggableCommandFailedEvent = {
316
+ address : string ;
317
+ connectionId ?: string | number ;
318
+ requestId : number ;
319
+ duration : number ;
320
+ commandName : string ;
321
+ failure : Error ;
322
+ serviceId ?: ObjectId ;
323
+ name : typeof COMMAND_FAILED ;
324
+ serverConnectionId : bigint | null ;
325
+ databaseName : string ;
326
+ } ;
327
+
298
328
/**
299
329
* @internal
300
330
* Must be separate from Events API due to differences in spec requirements for logging server heartbeat beginning
@@ -350,8 +380,8 @@ export type LoggableEvent =
350
380
| ServerSelectionSucceededEvent
351
381
| WaitingForSuitableServerEvent
352
382
| CommandStartedEvent
353
- | CommandSucceededEvent
354
- | CommandFailedEvent
383
+ | LoggableCommandSucceededEvent
384
+ | LoggableCommandFailedEvent
355
385
| ConnectionPoolCreatedEvent
356
386
| ConnectionPoolReadyEvent
357
387
| ConnectionPoolClosedEvent
@@ -383,7 +413,8 @@ export function stringifyWithMaxLen(
383
413
maxDocumentLength : number ,
384
414
options : EJSONOptions = { }
385
415
) : string {
386
- let strToTruncate : string ;
416
+ let strToTruncate = '' ;
417
+
387
418
if ( typeof value === 'function' ) {
388
419
strToTruncate = value . toString ( ) ;
389
420
} else {
@@ -420,7 +451,7 @@ function attachServerSelectionFields(
420
451
421
452
function attachCommandFields (
422
453
log : Record < string , any > ,
423
- commandEvent : CommandStartedEvent | CommandSucceededEvent | CommandFailedEvent
454
+ commandEvent : CommandStartedEvent | LoggableCommandSucceededEvent | LoggableCommandFailedEvent
424
455
) {
425
456
log . commandName = commandEvent . commandName ;
426
457
log . requestId = commandEvent . requestId ;
@@ -431,6 +462,8 @@ function attachCommandFields(
431
462
if ( commandEvent ?. serviceId ) {
432
463
log . serviceId = commandEvent . serviceId . toHexString ( ) ;
433
464
}
465
+ log . databaseName = commandEvent . databaseName ;
466
+ log . serverConnectionId = commandEvent ?. serverConnectionId ;
434
467
435
468
return log ;
436
469
}
@@ -490,20 +523,20 @@ function defaultLogTransform(
490
523
case COMMAND_STARTED :
491
524
log = attachCommandFields ( log , logObject ) ;
492
525
log . message = 'Command started' ;
493
- log . command = stringifyWithMaxLen ( logObject . command , maxDocumentLength ) ;
526
+ log . command = stringifyWithMaxLen ( logObject . command , maxDocumentLength , { relaxed : true } ) ;
494
527
log . databaseName = logObject . databaseName ;
495
528
return log ;
496
529
case COMMAND_SUCCEEDED :
497
530
log = attachCommandFields ( log , logObject ) ;
498
531
log . message = 'Command succeeded' ;
499
532
log . durationMS = logObject . duration ;
500
- log . reply = stringifyWithMaxLen ( logObject . reply , maxDocumentLength ) ;
533
+ log . reply = stringifyWithMaxLen ( logObject . reply , maxDocumentLength , { relaxed : true } ) ;
501
534
return log ;
502
535
case COMMAND_FAILED :
503
536
log = attachCommandFields ( log , logObject ) ;
504
537
log . message = 'Command failed' ;
505
538
log . durationMS = logObject . duration ;
506
- log . failure = logObject . failure ;
539
+ log . failure = logObject . failure . message ?? '(redacted)' ;
507
540
return log ;
508
541
case CONNECTION_POOL_CREATED :
509
542
log = attachConnectionFields ( log , logObject ) ;
@@ -701,12 +734,16 @@ export class MongoLogger {
701
734
this . logDestination = options . logDestination ;
702
735
}
703
736
737
+ willLog ( severity : SeverityLevel , component : MongoLoggableComponent ) : boolean {
738
+ return compareSeverity ( severity , this . componentSeverities [ component ] ) <= 0 ;
739
+ }
740
+
704
741
private log (
705
742
severity : SeverityLevel ,
706
743
component : MongoLoggableComponent ,
707
744
message : Loggable | string
708
745
) : void {
709
- if ( compareSeverity ( severity , this . componentSeverities [ component ] ) > 0 ) return ;
746
+ if ( ! this . willLog ( severity , component ) ) return ;
710
747
711
748
let logMessage : Log = { t : new Date ( ) , c : component , s : severity } ;
712
749
if ( typeof message === 'string' ) {
0 commit comments