1- import { type Document , EJSON , type EJSONOptions } from 'bson' ;
21import type { Writable } from 'stream' ;
32import { inspect } from 'util' ;
43
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' ;
106import type {
117 ConnectionCheckedInEvent ,
128 ConnectionCheckedOutEvent ,
@@ -295,6 +291,40 @@ function compareSeverity(s0: SeverityLevel, s1: SeverityLevel): 1 | 0 | -1 {
295291 return s0Num < s1Num ? - 1 : s0Num > s1Num ? 1 : 0 ;
296292}
297293
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+
298328/**
299329 * @internal
300330 * Must be separate from Events API due to differences in spec requirements for logging server heartbeat beginning
@@ -350,8 +380,8 @@ export type LoggableEvent =
350380 | ServerSelectionSucceededEvent
351381 | WaitingForSuitableServerEvent
352382 | CommandStartedEvent
353- | CommandSucceededEvent
354- | CommandFailedEvent
383+ | LoggableCommandSucceededEvent
384+ | LoggableCommandFailedEvent
355385 | ConnectionPoolCreatedEvent
356386 | ConnectionPoolReadyEvent
357387 | ConnectionPoolClosedEvent
@@ -383,7 +413,8 @@ export function stringifyWithMaxLen(
383413 maxDocumentLength : number ,
384414 options : EJSONOptions = { }
385415) : string {
386- let strToTruncate : string ;
416+ let strToTruncate = '' ;
417+
387418 if ( typeof value === 'function' ) {
388419 strToTruncate = value . toString ( ) ;
389420 } else {
@@ -420,7 +451,7 @@ function attachServerSelectionFields(
420451
421452function attachCommandFields (
422453 log : Record < string , any > ,
423- commandEvent : CommandStartedEvent | CommandSucceededEvent | CommandFailedEvent
454+ commandEvent : CommandStartedEvent | LoggableCommandSucceededEvent | LoggableCommandFailedEvent
424455) {
425456 log . commandName = commandEvent . commandName ;
426457 log . requestId = commandEvent . requestId ;
@@ -431,6 +462,8 @@ function attachCommandFields(
431462 if ( commandEvent ?. serviceId ) {
432463 log . serviceId = commandEvent . serviceId . toHexString ( ) ;
433464 }
465+ log . databaseName = commandEvent . databaseName ;
466+ log . serverConnectionId = commandEvent ?. serverConnectionId ;
434467
435468 return log ;
436469}
@@ -490,20 +523,20 @@ function defaultLogTransform(
490523 case COMMAND_STARTED :
491524 log = attachCommandFields ( log , logObject ) ;
492525 log . message = 'Command started' ;
493- log . command = stringifyWithMaxLen ( logObject . command , maxDocumentLength ) ;
526+ log . command = stringifyWithMaxLen ( logObject . command , maxDocumentLength , { relaxed : true } ) ;
494527 log . databaseName = logObject . databaseName ;
495528 return log ;
496529 case COMMAND_SUCCEEDED :
497530 log = attachCommandFields ( log , logObject ) ;
498531 log . message = 'Command succeeded' ;
499532 log . durationMS = logObject . duration ;
500- log . reply = stringifyWithMaxLen ( logObject . reply , maxDocumentLength ) ;
533+ log . reply = stringifyWithMaxLen ( logObject . reply , maxDocumentLength , { relaxed : true } ) ;
501534 return log ;
502535 case COMMAND_FAILED :
503536 log = attachCommandFields ( log , logObject ) ;
504537 log . message = 'Command failed' ;
505538 log . durationMS = logObject . duration ;
506- log . failure = logObject . failure ;
539+ log . failure = logObject . failure . message ?? '(redacted)' ;
507540 return log ;
508541 case CONNECTION_POOL_CREATED :
509542 log = attachConnectionFields ( log , logObject ) ;
@@ -701,12 +734,16 @@ export class MongoLogger {
701734 this . logDestination = options . logDestination ;
702735 }
703736
737+ willLog ( severity : SeverityLevel , component : MongoLoggableComponent ) : boolean {
738+ return compareSeverity ( severity , this . componentSeverities [ component ] ) <= 0 ;
739+ }
740+
704741 private log (
705742 severity : SeverityLevel ,
706743 component : MongoLoggableComponent ,
707744 message : Loggable | string
708745 ) : void {
709- if ( compareSeverity ( severity , this . componentSeverities [ component ] ) > 0 ) return ;
746+ if ( ! this . willLog ( severity , component ) ) return ;
710747
711748 let logMessage : Log = { t : new Date ( ) , c : component , s : severity } ;
712749 if ( typeof message === 'string' ) {
0 commit comments