@@ -53,6 +53,7 @@ import type {
53
53
DevtoolsProxyOptions ,
54
54
} from '@mongodb-js/devtools-proxy-support' ;
55
55
import { useOrCreateAgent } from '@mongodb-js/devtools-proxy-support' ;
56
+ import { setupMongoLogWriter } from '@mongosh/logging/lib/setup-logger-and-telemetry' ;
56
57
57
58
/**
58
59
* Connecting text key.
@@ -256,6 +257,32 @@ export class CliRepl implements MongoshIOProvider {
256
257
) ;
257
258
}
258
259
260
+ /** Setup log writer and start logging. */
261
+ private async startLogging ( ) {
262
+ await this . logManager . cleanupOldLogFiles ( ) ;
263
+ markTime ( TimingCategories . Logging , 'cleaned up log files' ) ;
264
+ const logger = await this . logManager . createLogWriter ( ) ;
265
+ const { quiet } = CliRepl . getFileAndEvalInfo ( this . cliOptions ) ;
266
+ if ( ! quiet ) {
267
+ this . output . write ( `Current Mongosh Log ID:\t${ logger . logId } \n` ) ;
268
+ }
269
+
270
+ this . logWriter = logger ;
271
+ this . logWriter = logger ;
272
+ setupMongoLogWriter ( logger ) ;
273
+ this . logWriter = logger ;
274
+ setupMongoLogWriter ( logger ) ;
275
+ markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
276
+ setupMongoLogWriter ( logger ) ;
277
+ this . bus . emit ( 'mongosh:log-initialized' ) ;
278
+ logger . info ( 'MONGOSH' , mongoLogId ( 1_000_000_000 ) , 'log' , 'Starting log' , {
279
+ execPath : process . execPath ,
280
+ envInfo : redactSensitiveData ( this . getLoggedEnvironmentVariables ( ) ) ,
281
+ ...( await buildInfo ( ) ) ,
282
+ } ) ;
283
+ markTime ( TimingCategories . Logging , 'logged initial message' ) ;
284
+ }
285
+
259
286
/**
260
287
* Setup CLI environment: serviceProvider, ShellEvaluator, log connection
261
288
* information, external editor, and finally start the repl.
@@ -267,7 +294,8 @@ export class CliRepl implements MongoshIOProvider {
267
294
driverUri : string ,
268
295
driverOptions : DevtoolsConnectOptions
269
296
) : Promise < void > {
270
- const { version } = require ( '../package.json' ) ;
297
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
298
+ const { version } : { version : string } = require ( '../package.json' ) ;
271
299
await this . verifyNodeVersion ( ) ;
272
300
markTime ( TimingCategories . REPLInstantiation , 'verified node version' ) ;
273
301
@@ -302,41 +330,24 @@ export class CliRepl implements MongoshIOProvider {
302
330
303
331
try {
304
332
await this . shellHomeDirectory . ensureExists ( ) ;
305
- } catch ( err : any ) {
306
- this . warnAboutInaccessibleFile ( err ) ;
333
+ } catch ( err : unknown ) {
334
+ this . warnAboutInaccessibleFile ( err as Error ) ;
307
335
}
308
336
markTime ( TimingCategories . REPLInstantiation , 'ensured shell homedir' ) ;
309
337
310
- await this . logManager . cleanupOldLogFiles ( ) ;
311
- markTime ( TimingCategories . Logging , 'cleaned up log files' ) ;
312
- const logger = await this . logManager . createLogWriter ( ) ;
313
- const { quiet } = CliRepl . getFileAndEvalInfo ( this . cliOptions ) ;
314
- if ( ! quiet ) {
315
- this . output . write ( `Current Mongosh Log ID:\t${ logger . logId } \n` ) ;
316
- }
317
- this . logWriter = logger ;
318
- markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
319
-
320
- logger . info ( 'MONGOSH' , mongoLogId ( 1_000_000_000 ) , 'log' , 'Starting log' , {
321
- execPath : process . execPath ,
322
- envInfo : redactSensitiveData ( this . getLoggedEnvironmentVariables ( ) ) ,
323
- ...( await buildInfo ( ) ) ,
324
- } ) ;
325
- markTime ( TimingCategories . Logging , 'logged initial message' ) ;
326
-
327
338
let analyticsSetupError : Error | null = null ;
328
339
try {
329
340
await this . setupAnalytics ( ) ;
330
- } catch ( err : any ) {
341
+ } catch ( err : unknown ) {
331
342
// Need to delay emitting the error on the bus so that logging is in place
332
343
// as well
333
- analyticsSetupError = err ;
344
+ analyticsSetupError = err as Error ;
334
345
}
335
346
336
347
markTime ( TimingCategories . Telemetry , 'created analytics instance' ) ;
348
+
337
349
setupLoggerAndTelemetry (
338
350
this . bus ,
339
- logger ,
340
351
this . toggleableAnalytics ,
341
352
{
342
353
platform : process . platform ,
@@ -352,17 +363,23 @@ export class CliRepl implements MongoshIOProvider {
352
363
this . bus . emit ( 'mongosh:error' , analyticsSetupError , 'analytics' ) ;
353
364
}
354
365
366
+ // Read local and global configuration
355
367
try {
356
368
this . config = await this . configDirectory . generateOrReadConfig (
357
369
this . config
358
370
) ;
359
- } catch ( err : any ) {
360
- this . warnAboutInaccessibleFile ( err ) ;
371
+ } catch ( err : unknown ) {
372
+ this . warnAboutInaccessibleFile ( err as Error ) ;
361
373
}
362
374
363
375
this . globalConfig = await this . loadGlobalConfigFile ( ) ;
364
376
markTime ( TimingCategories . UserConfigLoading , 'read global config files' ) ;
365
377
378
+ const disableLogging = this . getConfig ( 'disableLogging' ) ;
379
+ if ( disableLogging !== true ) {
380
+ await this . startLogging ( ) ;
381
+ }
382
+
366
383
// Needs to happen after loading the mongosh config file(s)
367
384
void this . fetchMongoshUpdateUrl ( ) ;
368
385
@@ -483,7 +500,7 @@ export class CliRepl implements MongoshIOProvider {
483
500
if ( ! this . cliOptions . shell ) {
484
501
// We flush the telemetry data as part of exiting. Make sure we have
485
502
// the right config value.
486
- this . setTelemetryEnabled ( await this . getConfig ( 'enableTelemetry' ) ) ;
503
+ this . setTelemetryEnabled ( this . getConfig ( 'enableTelemetry' ) ) ;
487
504
await this . exit ( 0 ) ;
488
505
return ;
489
506
}
@@ -516,10 +533,11 @@ export class CliRepl implements MongoshIOProvider {
516
533
517
534
// We only enable/disable here, since the rc file/command line scripts
518
535
// can disable the telemetry setting.
519
- this . setTelemetryEnabled ( await this . getConfig ( 'enableTelemetry' ) ) ;
536
+ this . setTelemetryEnabled ( this . getConfig ( 'enableTelemetry' ) ) ;
520
537
this . bus . emit ( 'mongosh:start-mongosh-repl' , { version } ) ;
521
538
markTime ( TimingCategories . REPLInstantiation , 'starting repl' ) ;
522
539
await this . mongoshRepl . startRepl ( initialized ) ;
540
+
523
541
this . bus . emit ( 'mongosh:start-session' , {
524
542
isInteractive : true ,
525
543
jsContext : this . mongoshRepl . jsContext ( ) ,
@@ -624,7 +642,7 @@ export class CliRepl implements MongoshIOProvider {
624
642
files : string [ ] ,
625
643
evalScripts : string [ ]
626
644
) : Promise < number > {
627
- let lastEvalResult : any ;
645
+ let lastEvalResult : unknown ;
628
646
let exitCode = 0 ;
629
647
try {
630
648
markTime ( TimingCategories . Eval , 'start eval scripts' ) ;
@@ -856,9 +874,7 @@ export class CliRepl implements MongoshIOProvider {
856
874
* Implements getConfig from the {@link ConfigProvider} interface.
857
875
*/
858
876
// eslint-disable-next-line @typescript-eslint/require-await
859
- async getConfig < K extends keyof CliUserConfig > (
860
- key : K
861
- ) : Promise < CliUserConfig [ K ] > {
877
+ getConfig < K extends keyof CliUserConfig > ( key : K ) : CliUserConfig [ K ] {
862
878
return (
863
879
( this . config as CliUserConfig ) [ key ] ??
864
880
( this . globalConfig as CliUserConfig ) ?. [ key ] ??
@@ -1259,7 +1275,7 @@ export class CliRepl implements MongoshIOProvider {
1259
1275
}
1260
1276
1261
1277
try {
1262
- const updateURL = ( await this . getConfig ( 'updateURL' ) ) . trim ( ) ;
1278
+ const updateURL = this . getConfig ( 'updateURL' ) . trim ( ) ;
1263
1279
if ( ! updateURL ) return ;
1264
1280
1265
1281
const localFilePath = this . shellHomeDirectory . localPath (
@@ -1284,7 +1300,8 @@ export class CliRepl implements MongoshIOProvider {
1284
1300
}
1285
1301
1286
1302
async getMoreRecentMongoshVersion ( ) : Promise < string | null > {
1287
- const { version } = require ( '../package.json' ) ;
1303
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
1304
+ const { version } : { version : string } = require ( '../package.json' ) ;
1288
1305
return await this . updateNotificationManager . getLatestVersionIfMoreRecent (
1289
1306
process . env
1290
1307
. MONGOSH_ASSUME_DIFFERENT_VERSION_FOR_UPDATE_NOTIFICATION_TEST ||
0 commit comments