@@ -14,7 +14,7 @@ import {
1414 MongoMissingCredentialsError ,
1515 MongoParseError
1616} from './error' ;
17- import { Logger , LoggerLevel } from './logger' ;
17+ import { Logger as LegacyLogger , LoggerLevel as LegacyLoggerLevel } from './logger' ;
1818import {
1919 DriverInfo ,
2020 MongoClient ,
@@ -24,6 +24,7 @@ import {
2424 ServerApi ,
2525 ServerApiVersion
2626} from './mongo_client' ;
27+ import { MongoLogger , MongoLoggerEnvOptions , MongoLoggerMongoClientOptions } from './mongo_logger' ;
2728import { PromiseProvider } from './promise_provider' ;
2829import { ReadConcern , ReadConcernLevel } from './read_concern' ;
2930import { ReadPreference , ReadPreferenceMode } from './read_preference' ;
@@ -35,6 +36,7 @@ import {
3536 HostAddress ,
3637 isRecord ,
3738 makeClientMetadata ,
39+ parseInteger ,
3840 setDifference
3941} from './utils' ;
4042import { W , WriteConcern } from './write_concern' ;
@@ -199,15 +201,16 @@ function getBoolean(name: string, value: unknown): boolean {
199201 throw new MongoParseError ( `Expected ${ name } to be stringified boolean value, got: ${ value } ` ) ;
200202}
201203
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+ }
206209 throw new MongoParseError ( `Expected ${ name } to be stringified int value, got: ${ value } ` ) ;
207210}
208211
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 ) ;
211214 if ( parsedValue < 0 ) {
212215 throw new MongoParseError ( `${ name } can only be a positive int value, got: ${ value } ` ) ;
213216 }
@@ -507,6 +510,30 @@ export function parseOptions(
507510 ) ;
508511 }
509512
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+
510537 return mongoOptions ;
511538}
512539
@@ -561,10 +588,10 @@ function setOption(
561588 mongoOptions [ name ] = getBoolean ( name , values [ 0 ] ) ;
562589 break ;
563590 case 'int' :
564- mongoOptions [ name ] = getInt ( name , values [ 0 ] ) ;
591+ mongoOptions [ name ] = getIntFromOptions ( name , values [ 0 ] ) ;
565592 break ;
566593 case 'uint' :
567- mongoOptions [ name ] = getUint ( name , values [ 0 ] ) ;
594+ mongoOptions [ name ] = getUIntFromOptions ( name , values [ 0 ] ) ;
568595 break ;
569596 case 'string' :
570597 if ( values [ 0 ] == null ) {
@@ -770,7 +797,7 @@ export const OPTIONS = {
770797 enableUtf8Validation : { type : 'boolean' , default : true } ,
771798 family : {
772799 transform ( { name, values : [ value ] } ) : 4 | 6 {
773- const transformValue = getInt ( name , value ) ;
800+ const transformValue = getIntFromOptions ( name , value ) ;
774801 if ( transformValue === 4 || transformValue === 6 ) {
775802 return transformValue ;
776803 }
@@ -849,9 +876,9 @@ export const OPTIONS = {
849876 type : 'uint'
850877 } ,
851878 logger : {
852- default : new Logger ( 'MongoClient' ) ,
879+ default : new LegacyLogger ( 'MongoClient' ) ,
853880 transform ( { values : [ value ] } ) {
854- if ( value instanceof Logger ) {
881+ if ( value instanceof LegacyLogger ) {
855882 return value ;
856883 }
857884 emitWarning ( 'Alternative loggers might not be supported' ) ;
@@ -863,13 +890,13 @@ export const OPTIONS = {
863890 loggerLevel : {
864891 target : 'logger' ,
865892 transform ( { values : [ value ] } ) {
866- return new Logger ( 'MongoClient' , { loggerLevel : value as LoggerLevel } ) ;
893+ return new LegacyLogger ( 'MongoClient' , { loggerLevel : value as LegacyLoggerLevel } ) ;
867894 }
868895 } ,
869896 maxConnecting : {
870897 default : 2 ,
871898 transform ( { name, values : [ value ] } ) : number {
872- const maxConnecting = getUint ( name , value ) ;
899+ const maxConnecting = getUIntFromOptions ( name , value ) ;
873900 if ( maxConnecting === 0 ) {
874901 throw new MongoInvalidArgumentError ( 'maxConnecting must be > 0 if specified' ) ;
875902 }
@@ -887,7 +914,7 @@ export const OPTIONS = {
887914 maxStalenessSeconds : {
888915 target : 'readPreference' ,
889916 transform ( { name, options, values : [ value ] } ) {
890- const maxStalenessSeconds = getUint ( name , value ) ;
917+ const maxStalenessSeconds = getUIntFromOptions ( name , value ) ;
891918 if ( options . readPreference ) {
892919 return ReadPreference . fromOptions ( {
893920 readPreference : { ...options . readPreference , maxStalenessSeconds }
@@ -1206,7 +1233,7 @@ export const OPTIONS = {
12061233 const wc = WriteConcern . fromOptions ( {
12071234 writeConcern : {
12081235 ...options . writeConcern ,
1209- wtimeout : getUint ( 'wtimeout' , value )
1236+ wtimeout : getUIntFromOptions ( 'wtimeout' , value )
12101237 }
12111238 } ) ;
12121239 if ( wc ) return wc ;
@@ -1219,7 +1246,7 @@ export const OPTIONS = {
12191246 const wc = WriteConcern . fromOptions ( {
12201247 writeConcern : {
12211248 ...options . writeConcern ,
1222- wtimeoutMS : getUint ( 'wtimeoutMS' , value )
1249+ wtimeoutMS : getUIntFromOptions ( 'wtimeoutMS' , value )
12231250 }
12241251 } ) ;
12251252 if ( wc ) return wc ;
@@ -1274,4 +1301,7 @@ export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
12741301 * Set of permitted feature flags
12751302 * @internal
12761303 */
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