@@ -14,7 +14,7 @@ import {
14
14
MongoMissingCredentialsError ,
15
15
MongoParseError
16
16
} from './error' ;
17
- import { Logger , LoggerLevel } from './logger' ;
17
+ import { Logger as LegacyLogger , LoggerLevel as LegacyLoggerLevel } from './logger' ;
18
18
import {
19
19
DriverInfo ,
20
20
MongoClient ,
@@ -24,6 +24,7 @@ import {
24
24
ServerApi ,
25
25
ServerApiVersion
26
26
} from './mongo_client' ;
27
+ import { MongoLogger , MongoLoggerEnvOptions , MongoLoggerMongoClientOptions } from './mongo_logger' ;
27
28
import { PromiseProvider } from './promise_provider' ;
28
29
import { ReadConcern , ReadConcernLevel } from './read_concern' ;
29
30
import { ReadPreference , ReadPreferenceMode } from './read_preference' ;
@@ -35,6 +36,7 @@ import {
35
36
HostAddress ,
36
37
isRecord ,
37
38
makeClientMetadata ,
39
+ parseInteger ,
38
40
setDifference
39
41
} from './utils' ;
40
42
import { W , WriteConcern } from './write_concern' ;
@@ -199,15 +201,16 @@ function getBoolean(name: string, value: unknown): boolean {
199
201
throw new MongoParseError ( `Expected ${ name } to be stringified boolean value, got: ${ value } ` ) ;
200
202
}
201
203
202
- function getInt ( name : string , value : unknown ) : number {
203
- if ( typeof value === 'number' ) return Math . trunc ( value ) ;
204
- const parsedValue = Number . parseInt ( String ( value ) , 10 ) ;
205
- if ( ! Number . isNaN ( parsedValue ) ) return parsedValue ;
204
+ function getIntFromOptions ( name : string , value : unknown ) : number {
205
+ const parsedInt = parseInteger ( value ) ;
206
+ if ( parsedInt != null ) {
207
+ return parsedInt ;
208
+ }
206
209
throw new MongoParseError ( `Expected ${ name } to be stringified int value, got: ${ value } ` ) ;
207
210
}
208
211
209
- function getUint ( name : string , value : unknown ) : number {
210
- const parsedValue = getInt ( name , value ) ;
212
+ function getUIntFromOptions ( name : string , value : unknown ) : number {
213
+ const parsedValue = getIntFromOptions ( name , value ) ;
211
214
if ( parsedValue < 0 ) {
212
215
throw new MongoParseError ( `${ name } can only be a positive int value, got: ${ value } ` ) ;
213
216
}
@@ -507,6 +510,30 @@ export function parseOptions(
507
510
) ;
508
511
}
509
512
513
+ const loggerFeatureFlag = Symbol . for ( '@@mdb.enableMongoLogger' ) ;
514
+ mongoOptions [ loggerFeatureFlag ] = mongoOptions [ loggerFeatureFlag ] ?? false ;
515
+
516
+ let loggerEnvOptions : MongoLoggerEnvOptions = { } ;
517
+ let loggerClientOptions : MongoLoggerMongoClientOptions = { } ;
518
+ if ( mongoOptions [ loggerFeatureFlag ] ) {
519
+ loggerEnvOptions = {
520
+ MONGODB_LOG_COMMAND : process . env . MONGODB_LOG_COMMAND ,
521
+ MONGODB_LOG_TOPOLOGY : process . env . MONGODB_LOG_TOPOLOGY ,
522
+ MONGODB_LOG_SERVER_SELECTION : process . env . MONGODB_LOG_SERVER_SELECTION ,
523
+ MONGODB_LOG_CONNECTION : process . env . MONGODB_LOG_CONNECTION ,
524
+ MONGODB_LOG_ALL : process . env . MONGODB_LOG_ALL ,
525
+ MONGODB_LOG_MAX_DOCUMENT_LENGTH : process . env . MONGODB_LOG_MAX_DOCUMENT_LENGTH ,
526
+ MONGODB_LOG_PATH : process . env . MONGODB_LOG_PATH
527
+ } ;
528
+ loggerClientOptions = {
529
+ mongodbLogPath : mongoOptions . mongodbLogPath
530
+ } ;
531
+ }
532
+ mongoOptions . mongoLoggerOptions = MongoLogger . resolveOptions (
533
+ loggerEnvOptions ,
534
+ loggerClientOptions
535
+ ) ;
536
+
510
537
return mongoOptions ;
511
538
}
512
539
@@ -561,10 +588,10 @@ function setOption(
561
588
mongoOptions [ name ] = getBoolean ( name , values [ 0 ] ) ;
562
589
break ;
563
590
case 'int' :
564
- mongoOptions [ name ] = getInt ( name , values [ 0 ] ) ;
591
+ mongoOptions [ name ] = getIntFromOptions ( name , values [ 0 ] ) ;
565
592
break ;
566
593
case 'uint' :
567
- mongoOptions [ name ] = getUint ( name , values [ 0 ] ) ;
594
+ mongoOptions [ name ] = getUIntFromOptions ( name , values [ 0 ] ) ;
568
595
break ;
569
596
case 'string' :
570
597
if ( values [ 0 ] == null ) {
@@ -770,7 +797,7 @@ export const OPTIONS = {
770
797
enableUtf8Validation : { type : 'boolean' , default : true } ,
771
798
family : {
772
799
transform ( { name, values : [ value ] } ) : 4 | 6 {
773
- const transformValue = getInt ( name , value ) ;
800
+ const transformValue = getIntFromOptions ( name , value ) ;
774
801
if ( transformValue === 4 || transformValue === 6 ) {
775
802
return transformValue ;
776
803
}
@@ -849,9 +876,9 @@ export const OPTIONS = {
849
876
type : 'uint'
850
877
} ,
851
878
logger : {
852
- default : new Logger ( 'MongoClient' ) ,
879
+ default : new LegacyLogger ( 'MongoClient' ) ,
853
880
transform ( { values : [ value ] } ) {
854
- if ( value instanceof Logger ) {
881
+ if ( value instanceof LegacyLogger ) {
855
882
return value ;
856
883
}
857
884
emitWarning ( 'Alternative loggers might not be supported' ) ;
@@ -863,13 +890,13 @@ export const OPTIONS = {
863
890
loggerLevel : {
864
891
target : 'logger' ,
865
892
transform ( { values : [ value ] } ) {
866
- return new Logger ( 'MongoClient' , { loggerLevel : value as LoggerLevel } ) ;
893
+ return new LegacyLogger ( 'MongoClient' , { loggerLevel : value as LegacyLoggerLevel } ) ;
867
894
}
868
895
} ,
869
896
maxConnecting : {
870
897
default : 2 ,
871
898
transform ( { name, values : [ value ] } ) : number {
872
- const maxConnecting = getUint ( name , value ) ;
899
+ const maxConnecting = getUIntFromOptions ( name , value ) ;
873
900
if ( maxConnecting === 0 ) {
874
901
throw new MongoInvalidArgumentError ( 'maxConnecting must be > 0 if specified' ) ;
875
902
}
@@ -887,7 +914,7 @@ export const OPTIONS = {
887
914
maxStalenessSeconds : {
888
915
target : 'readPreference' ,
889
916
transform ( { name, options, values : [ value ] } ) {
890
- const maxStalenessSeconds = getUint ( name , value ) ;
917
+ const maxStalenessSeconds = getUIntFromOptions ( name , value ) ;
891
918
if ( options . readPreference ) {
892
919
return ReadPreference . fromOptions ( {
893
920
readPreference : { ...options . readPreference , maxStalenessSeconds }
@@ -1206,7 +1233,7 @@ export const OPTIONS = {
1206
1233
const wc = WriteConcern . fromOptions ( {
1207
1234
writeConcern : {
1208
1235
...options . writeConcern ,
1209
- wtimeout : getUint ( 'wtimeout' , value )
1236
+ wtimeout : getUIntFromOptions ( 'wtimeout' , value )
1210
1237
}
1211
1238
} ) ;
1212
1239
if ( wc ) return wc ;
@@ -1219,7 +1246,7 @@ export const OPTIONS = {
1219
1246
const wc = WriteConcern . fromOptions ( {
1220
1247
writeConcern : {
1221
1248
...options . writeConcern ,
1222
- wtimeoutMS : getUint ( 'wtimeoutMS' , value )
1249
+ wtimeoutMS : getUIntFromOptions ( 'wtimeoutMS' , value )
1223
1250
}
1224
1251
} ) ;
1225
1252
if ( wc ) return wc ;
@@ -1274,4 +1301,7 @@ export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
1274
1301
* Set of permitted feature flags
1275
1302
* @internal
1276
1303
*/
1277
- export const FEATURE_FLAGS = new Set ( [ Symbol . for ( '@@mdb.skipPingOnConnect' ) ] ) ;
1304
+ export const FEATURE_FLAGS = new Set ( [
1305
+ Symbol . for ( '@@mdb.skipPingOnConnect' ) ,
1306
+ Symbol . for ( '@@mdb.enableMongoLogger' )
1307
+ ] ) ;
0 commit comments