@@ -13,6 +13,12 @@ const { ReadPreference } = require('../mongodb');
13
13
const { MongoCredentials } = require ( '../mongodb' ) ;
14
14
const { MongoClient, MongoParseError, ServerApiVersion } = require ( '../mongodb' ) ;
15
15
const { MongoLogger } = require ( '../mongodb' ) ;
16
+ const {
17
+ SeverityLevel,
18
+ MongoLoggableComponent,
19
+ MongoLoggerEnvOptions
20
+ // eslint-disable-next-line no-restricted-modules
21
+ } = require ( '../../src/mongo_logger' ) ;
16
22
const sinon = require ( 'sinon' ) ;
17
23
const { Writable } = require ( 'stream' ) ;
18
24
@@ -817,71 +823,126 @@ describe('MongoOptions', function () {
817
823
} ) ;
818
824
} ) ;
819
825
820
- context ( 'when mongodbLogPath is in options', function ( ) {
826
+ describe ( 'logging client options', function ( ) {
821
827
const loggerFeatureFlag = Symbol . for ( '@@mdb.enableMongoLogger' ) ;
828
+ context ( 'when mongodbLogPath is in options' , function ( ) {
829
+ let stderrStub ;
830
+ let stdoutStub ;
822
831
823
- let stderrStub ;
824
- let stdoutStub ;
825
-
826
- beforeEach ( ( ) => {
827
- stdoutStub = sinon . stub ( process . stdout ) ;
828
- stderrStub = sinon . stub ( process . stderr ) ;
829
- } ) ;
832
+ beforeEach ( ( ) => {
833
+ stdoutStub = sinon . stub ( process . stdout ) ;
834
+ stderrStub = sinon . stub ( process . stderr ) ;
835
+ } ) ;
830
836
831
- afterEach ( ( ) => {
832
- sinon . restore ( ) ;
833
- } ) ;
837
+ afterEach ( ( ) => {
838
+ sinon . restore ( ) ;
839
+ } ) ;
834
840
835
- context ( 'when option is `stderr`' , function ( ) {
836
- it ( 'it is accessible through mongoLogger.logDestination' , function ( ) {
837
- const client = new MongoClient ( 'mongodb://a/' , {
838
- [ loggerFeatureFlag ] : true ,
839
- mongodbLogPath : 'stderr'
841
+ context ( 'when option is `stderr`' , function ( ) {
842
+ it ( 'it is accessible through mongoLogger.logDestination' , function ( ) {
843
+ const client = new MongoClient ( 'mongodb://a/' , {
844
+ [ loggerFeatureFlag ] : true ,
845
+ mongodbLogPath : 'stderr'
846
+ } ) ;
847
+ const log = { t : new Date ( ) , c : 'constructorStdErr' , s : 'error' } ;
848
+ client . options . mongoLoggerOptions . logDestination . write ( log ) ;
849
+ expect ( stderrStub . write ) . calledWith ( inspect ( log , { breakLength : Infinity , compact : true } ) ) ;
840
850
} ) ;
841
- const log = { t : new Date ( ) , c : 'constructorStdErr' , s : 'error' } ;
842
- client . options . mongoLoggerOptions . logDestination . write ( log ) ;
843
- expect ( stderrStub . write ) . calledWith ( inspect ( log , { breakLength : Infinity , compact : true } ) ) ;
844
851
} ) ;
845
- } ) ;
846
852
847
- context ( 'when option is a MongoDBLogWritable stream' , function ( ) {
848
- it ( 'it is accessible through mongoLogger.logDestination' , function ( ) {
849
- const writable = {
850
- buffer : [ ] ,
851
- write ( log ) {
852
- this . buffer . push ( log ) ;
853
- }
854
- } ;
855
- const client = new MongoClient ( 'mongodb://a/' , {
856
- [ loggerFeatureFlag ] : true ,
857
- mongodbLogPath : writable
853
+ context ( 'when option is a MongoDBLogWritable stream' , function ( ) {
854
+ it ( 'it is accessible through mongoLogger.logDestination' , function ( ) {
855
+ const writable = {
856
+ buffer : [ ] ,
857
+ write ( log ) {
858
+ this . buffer . push ( log ) ;
859
+ }
860
+ } ;
861
+ const client = new MongoClient ( 'mongodb://a/' , {
862
+ [ loggerFeatureFlag ] : true ,
863
+ mongodbLogPath : writable
864
+ } ) ;
865
+ expect ( client . options . mongoLoggerOptions . logDestination ) . to . deep . equal ( writable ) ;
858
866
} ) ;
859
- expect ( client . options . mongoLoggerOptions . logDestination ) . to . deep . equal ( writable ) ;
860
867
} ) ;
861
- } ) ;
862
868
863
- context ( 'when option is `stdout`' , function ( ) {
864
- it ( 'it is accessible through mongoLogger.logDestination' , function ( ) {
865
- const client = new MongoClient ( 'mongodb://a/' , {
866
- [ loggerFeatureFlag ] : true ,
867
- mongodbLogPath : 'stdout'
869
+ context ( 'when option is `stdout`' , function ( ) {
870
+ it ( 'it is accessible through mongoLogger.logDestination' , function ( ) {
871
+ const client = new MongoClient ( 'mongodb://a/' , {
872
+ [ loggerFeatureFlag ] : true ,
873
+ mongodbLogPath : 'stdout'
874
+ } ) ;
875
+ const log = { t : new Date ( ) , c : 'constructorStdOut' , s : 'error' } ;
876
+ client . options . mongoLoggerOptions . logDestination . write ( log ) ;
877
+ expect ( stdoutStub . write ) . calledWith ( inspect ( log , { breakLength : Infinity , compact : true } ) ) ;
868
878
} ) ;
869
- const log = { t : new Date ( ) , c : 'constructorStdOut' , s : 'error' } ;
870
- client . options . mongoLoggerOptions . logDestination . write ( log ) ;
871
- expect ( stdoutStub . write ) . calledWith ( inspect ( log , { breakLength : Infinity , compact : true } ) ) ;
872
879
} ) ;
873
- } ) ;
874
880
875
- context ( 'when option is invalid' , function ( ) {
876
- it ( 'it defaults to stderr' , function ( ) {
877
- const invalidOption = 'stdnothing' ;
878
- const client = new MongoClient ( 'mongodb://a/' , {
879
- [ loggerFeatureFlag ] : true ,
880
- mongodbLogPath : invalidOption
881
+ context ( 'when option is invalid' , function ( ) {
882
+ it ( 'it defaults to stderr' , function ( ) {
883
+ const invalidOption = 'stdnothing' ;
884
+ const client = new MongoClient ( 'mongodb://a/' , {
885
+ [ loggerFeatureFlag ] : true ,
886
+ mongodbLogPath : invalidOption
887
+ } ) ;
888
+ const log = { t : new Date ( ) , c : 'constructorInvalidOption' , s : 'error' } ;
889
+ client . options . mongoLoggerOptions . logDestination . write ( log ) ;
890
+ expect ( stderrStub . write ) . calledWith (
891
+ inspect ( log , { breakLength : Infinity , compact : true } )
892
+ ) ;
881
893
} ) ;
882
- const log = { t : new Date ( ) , c : 'constructorInvalidOption' , s : 'error' } ;
883
- client . options . mongoLoggerOptions . logDestination . write ( log ) ;
884
- expect ( stderrStub . write ) . calledWith ( inspect ( log , { breakLength : Infinity , compact : true } ) ) ;
894
+ } ) ;
895
+ } ) ;
896
+ describe . only ( 'component severities' , function ( ) {
897
+ const components = Object . values ( MongoLoggableComponent ) ;
898
+ const env_component_names = [
899
+ 'MONGODB_LOG_COMMAND' ,
900
+ 'MONGODB_LOG_TOPOLOGY' ,
901
+ 'MONGODB_LOG_SERVER_SELECTION' ,
902
+ 'MONGODB_LOG_CONNECTION' ,
903
+ 'MONGODB_LOG_CLIENT'
904
+ ] ;
905
+ context ( 'when only client option is provided for' , function ( ) {
906
+ for ( let i = 0 ; i < components . length ; i ++ ) {
907
+ it ( `it stores severity levels for ${ components [ i ] } component correctly` , function ( ) {
908
+ for ( const severityLevel of Object . values ( SeverityLevel ) ) {
909
+ const client = new MongoClient ( 'mongodb://a/' , {
910
+ [ loggerFeatureFlag ] : true ,
911
+ mongodbLogComponentSeverities : {
912
+ [ components [ i ] ] : severityLevel
913
+ }
914
+ } ) ;
915
+ expect ( client . options . mongoLoggerOptions . componentSeverities [ components [ i ] ] ) . to . equal (
916
+ severityLevel
917
+ ) ;
918
+ }
919
+ } ) ;
920
+ }
921
+ } ) ;
922
+ context ( 'when both client and environment option is provided' , function ( ) {
923
+ for ( let i = 0 ; i < components . length ; i ++ ) {
924
+ beforeEach ( function ( ) {
925
+ process . env [ env_component_names [ i ] ] = 'emergency' ;
926
+ } ) ;
927
+
928
+ afterEach ( function ( ) {
929
+ process . env [ env_component_names [ i ] ] = undefined ;
930
+ } ) ;
931
+
932
+ it ( `it stores severity levels for ${ components [ i ] } component correctly (client options have precedence)` , function ( ) {
933
+ for ( const severityLevel of Object . values ( SeverityLevel ) ) {
934
+ const client = new MongoClient ( 'mongodb://a/' , {
935
+ [ loggerFeatureFlag ] : true ,
936
+ mongodbLogComponentSeverities : {
937
+ [ components [ i ] ] : severityLevel
938
+ }
939
+ } ) ;
940
+ expect ( client . options . mongoLoggerOptions . componentSeverities [ components [ i ] ] ) . to . equal (
941
+ severityLevel
942
+ ) ;
943
+ }
944
+ } ) ;
945
+ } ;
885
946
} ) ;
886
947
} ) ;
887
948
} ) ;
0 commit comments