@@ -824,4 +824,112 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
824824 expect ( roleIndexes . find ( idx => idx . name === 'name_1' ) ) . toBeDefined ( ) ;
825825 } ) ;
826826 } ) ;
827+
828+ describe ( 'clientLogEvents' , ( ) => {
829+ it ( 'should log MongoDB client events when configured' , async ( ) => {
830+ const logger = require ( '../lib/logger' ) . logger ;
831+ const logSpy = spyOn ( logger , 'warn' ) ;
832+
833+ const clientLogEvents = [
834+ {
835+ name : 'serverDescriptionChanged' ,
836+ keys : [ 'address' ] ,
837+ logLevel : 'warn' ,
838+ } ,
839+ ] ;
840+
841+ const adapter = new MongoStorageAdapter ( {
842+ uri : databaseURI ,
843+ mongoOptions : { clientLogEvents } ,
844+ } ) ;
845+
846+ // Connect to trigger event listeners setup
847+ await adapter . connect ( ) ;
848+
849+ // Manually trigger the event to test the listener
850+ const mockEvent = {
851+ address : 'localhost:27017' ,
852+ previousDescription : { type : 'Unknown' } ,
853+ newDescription : { type : 'Standalone' } ,
854+ } ;
855+
856+ adapter . client . emit ( 'serverDescriptionChanged' , mockEvent ) ;
857+
858+ // Verify the log was called with the correct message
859+ expect ( logSpy ) . toHaveBeenCalledWith (
860+ jasmine . stringMatching ( / M o n g o D B c l i e n t e v e n t s e r v e r D e s c r i p t i o n C h a n g e d : .* " a d d r e s s " : " l o c a l h o s t : 2 7 0 1 7 " / )
861+ ) ;
862+
863+ await adapter . handleShutdown ( ) ;
864+ } ) ;
865+
866+ it ( 'should log entire event when keys are not specified' , async ( ) => {
867+ const logger = require ( '../lib/logger' ) . logger ;
868+ const logSpy = spyOn ( logger , 'info' ) ;
869+
870+ const clientLogEvents = [
871+ {
872+ name : 'connectionPoolReady' ,
873+ logLevel : 'info' ,
874+ } ,
875+ ] ;
876+
877+ const adapter = new MongoStorageAdapter ( {
878+ uri : databaseURI ,
879+ mongoOptions : { clientLogEvents } ,
880+ } ) ;
881+
882+ await adapter . connect ( ) ;
883+
884+ const mockEvent = {
885+ address : 'localhost:27017' ,
886+ options : { maxPoolSize : 100 } ,
887+ } ;
888+
889+ adapter . client . emit ( 'connectionPoolReady' , mockEvent ) ;
890+
891+ expect ( logSpy ) . toHaveBeenCalledWith (
892+ jasmine . stringMatching ( / M o n g o D B c l i e n t e v e n t c o n n e c t i o n P o o l R e a d y : .* " a d d r e s s " : " l o c a l h o s t : 2 7 0 1 7 " .* " o p t i o n s " / )
893+ ) ;
894+
895+ await adapter . handleShutdown ( ) ;
896+ } ) ;
897+
898+ it ( 'should extract nested keys using dot notation' , async ( ) => {
899+ const logger = require ( '../lib/logger' ) . logger ;
900+ const logSpy = spyOn ( logger , 'warn' ) ;
901+
902+ const clientLogEvents = [
903+ {
904+ name : 'topologyDescriptionChanged' ,
905+ keys : [ 'previousDescription.type' , 'newDescription.type' , 'newDescription.servers.size' ] ,
906+ logLevel : 'warn' ,
907+ } ,
908+ ] ;
909+
910+ const adapter = new MongoStorageAdapter ( {
911+ uri : databaseURI ,
912+ mongoOptions : { clientLogEvents } ,
913+ } ) ;
914+
915+ await adapter . connect ( ) ;
916+
917+ const mockEvent = {
918+ topologyId : 1 ,
919+ previousDescription : { type : 'Unknown' } ,
920+ newDescription : {
921+ type : 'ReplicaSetWithPrimary' ,
922+ servers : { size : 3 } ,
923+ } ,
924+ } ;
925+
926+ adapter . client . emit ( 'topologyDescriptionChanged' , mockEvent ) ;
927+
928+ expect ( logSpy ) . toHaveBeenCalledWith (
929+ jasmine . stringMatching ( / M o n g o D B c l i e n t e v e n t t o p o l o g y D e s c r i p t i o n C h a n g e d : .* " p r e v i o u s D e s c r i p t i o n .t y p e " : " U n k n o w n " .* " n e w D e s c r i p t i o n .t y p e " : " R e p l i c a S e t W i t h P r i m a r y " .* " n e w D e s c r i p t i o n .s e r v e r s .s i z e " : 3 / )
930+ ) ;
931+
932+ await adapter . handleShutdown ( ) ;
933+ } ) ;
934+ } ) ;
827935} ) ;
0 commit comments