@@ -28,7 +28,9 @@ import {
28
28
type HostAddress ,
29
29
type Log ,
30
30
MongoClient ,
31
+ type MongoClientOptions ,
31
32
type MongoCredentials ,
33
+ type MongoDBLogWritable ,
32
34
ReadConcern ,
33
35
ReadPreference ,
34
36
SENSITIVE_COMMANDS ,
@@ -126,7 +128,6 @@ export class UnifiedMongoClient extends MongoClient {
126
128
cmapEvents : CmapEvent [ ] = [ ] ;
127
129
sdamEvents : SdamEvent [ ] = [ ] ;
128
130
failPoints : Document [ ] = [ ] ;
129
- logCollector : { buffer : LogMessage [ ] ; write : ( log : Log ) => void } ;
130
131
131
132
ignoredEvents : string [ ] ;
132
133
observeSensitiveCommands : boolean ;
@@ -197,34 +198,44 @@ export class UnifiedMongoClient extends MongoClient {
197
198
topology : 'MONGODB_LOG_TOPOLOGY'
198
199
} as const ;
199
200
200
- constructor ( uri : string , description : ClientEntity ) {
201
- const logCollector : { buffer : LogMessage [ ] ; write : ( log : Log ) => void } = {
202
- buffer : [ ] ,
203
- write ( log : Log ) : void {
204
- const transformedLog = {
205
- level : log . s ,
206
- component : log . c ,
207
- data : { ...log }
208
- } ;
209
-
210
- this . buffer . push ( transformedLog ) ;
211
- }
212
- } ;
213
- const mongodbLogComponentSeverities = description . observeLogMessages ;
214
-
215
- super ( uri , {
201
+ constructor (
202
+ uri : string ,
203
+ description : ClientEntity ,
204
+ config : {
205
+ loggingEnabled ?: boolean ;
206
+ setupLogging ?: ( options : Record < string , any > ) => Record < string , any > ;
207
+ }
208
+ ) {
209
+ const options : MongoClientOptions = {
216
210
monitorCommands : true ,
217
211
__skipPingOnConnect : true ,
218
- mongodbLogComponentSeverities,
219
212
...getEnvironmentalOptions ( ) ,
220
213
...( description . serverApi ? { serverApi : description . serverApi } : { } ) ,
221
- mongodbLogPath : logCollector ,
222
214
// TODO(NODE-5785): We need to increase the truncation length because signature.hash is a Buffer making hellos too long
223
215
mongodbLogMaxDocumentLength : 1250
224
- } as any ) ;
216
+ } ;
217
+
218
+ if ( description . observeLogMessages != null ) {
219
+ options . mongodbLogComponentSeverities = description . observeLogMessages ;
220
+ options . mongodbLogPath = {
221
+ buffer : [ ] ,
222
+ write ( log : Log ) : void {
223
+ const transformedLog = {
224
+ level : log . s ,
225
+ component : log . c ,
226
+ data : { ...log }
227
+ } ;
228
+
229
+ this . buffer . push ( transformedLog ) ;
230
+ }
231
+ } as MongoDBLogWritable ;
232
+ } else if ( config . loggingEnabled ) {
233
+ config . setupLogging ?.( options ) ;
234
+ }
235
+
236
+ super ( uri , options ) ;
225
237
226
238
this . observedEventEmitter . on ( 'error' , ( ) => null ) ;
227
- this . logCollector = logCollector ;
228
239
this . observeSensitiveCommands = description . observeSensitiveCommands ?? false ;
229
240
230
241
this . ignoredEvents = [
@@ -337,7 +348,7 @@ export class UnifiedMongoClient extends MongoClient {
337
348
}
338
349
339
350
get collectedLogs ( ) : LogMessage [ ] {
340
- return this . logCollector . buffer ;
351
+ return ( this . options . mongodbLogPath as unknown as { buffer : any [ ] } ) ?. buffer ?? [ ] ;
341
352
}
342
353
}
343
354
@@ -578,7 +589,8 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
578
589
} else {
579
590
uri = makeConnectionString ( config . url ( { useMultipleMongoses } ) , entity . client . uriOptions ) ;
580
591
}
581
- const client = new UnifiedMongoClient ( uri , entity . client ) ;
592
+
593
+ const client = new UnifiedMongoClient ( uri , entity . client , config ) ;
582
594
try {
583
595
new EntityEventRegistry ( client , entity . client , map ) . register ( ) ;
584
596
await client . connect ( ) ;
0 commit comments