@@ -53,6 +53,7 @@ import type {
5353 DevtoolsProxyOptions ,
5454} from '@mongodb-js/devtools-proxy-support' ;
5555import { useOrCreateAgent } from '@mongodb-js/devtools-proxy-support' ;
56+ import { setupMongoLogWriter } from '@mongosh/logging/lib/setup-logger-and-telemetry' ;
5657
5758/**
5859 * Connecting text key.
@@ -256,6 +257,69 @@ export class CliRepl implements MongoshIOProvider {
256257 ) ;
257258 }
258259
260+ /** Setup analytics, logging and telemetry. */
261+ private async startLoggingAndTelemetry ( version : string ) {
262+ // Read the global config with log settings, e.g. logLocation, disableLogging
263+ const disableLogging = this . getConfig ( 'disableLogging' ) ;
264+ if ( ! disableLogging ) {
265+ await this . logManager . cleanupOldLogFiles ( ) ;
266+ markTime ( TimingCategories . Logging , 'cleaned up log files' ) ;
267+
268+ const logger = await this . logManager . createLogWriter ( ) ;
269+ const { quiet } = CliRepl . getFileAndEvalInfo ( this . cliOptions ) ;
270+ if ( ! quiet ) {
271+ this . output . write ( `Current Mongosh Log ID:\t${ logger . logId } \n` ) ;
272+ }
273+
274+ this . logWriter = logger ;
275+ setupMongoLogWriter ( logger ) ;
276+ markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
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+
284+ markTime ( TimingCategories . Logging , 'logged initial message' ) ;
285+ }
286+
287+ // Create analytics instance
288+ let analyticsSetupError : Error | null = null ;
289+ try {
290+ await this . setupAnalytics ( ) ;
291+ } catch ( err : unknown ) {
292+ // Need to delay emitting the error on the bus so that logging is in place
293+ // as well
294+ analyticsSetupError = err as Error ;
295+ }
296+
297+ if ( analyticsSetupError ) {
298+ this . bus . emit ( 'mongosh:error' , analyticsSetupError , 'analytics' ) ;
299+ }
300+
301+ markTime ( TimingCategories . Telemetry , 'created analytics instance' ) ;
302+
303+ // Setup telemetry
304+ setupLoggerAndTelemetry (
305+ this . bus ,
306+ this . toggleableAnalytics ,
307+ {
308+ userId : this . config . userId ,
309+ telemetryAnonymousId : this . config . telemetryAnonymousId ,
310+ } ,
311+ {
312+ platform : process . platform ,
313+ arch : process . arch ,
314+ is_containerized : this . isContainerizedEnvironment ,
315+ ...( await getOsInfo ( ) ) ,
316+ } ,
317+ version
318+ ) ;
319+
320+ markTime ( TimingCategories . Telemetry , 'completed telemetry setup' ) ;
321+ }
322+
259323 /**
260324 * Setup CLI environment: serviceProvider, ShellEvaluator, log connection
261325 * information, external editor, and finally start the repl.
@@ -267,7 +331,8 @@ export class CliRepl implements MongoshIOProvider {
267331 driverUri : string ,
268332 driverOptions : DevtoolsConnectOptions
269333 ) : Promise < void > {
270- const { version } = require ( '../package.json' ) ;
334+ // eslint-disable-next-line @typescript-eslint/no-var-requires
335+ const { version } : { version : string } = require ( '../package.json' ) ;
271336 await this . verifyNodeVersion ( ) ;
272337 markTime ( TimingCategories . REPLInstantiation , 'verified node version' ) ;
273338
@@ -302,67 +367,25 @@ export class CliRepl implements MongoshIOProvider {
302367
303368 try {
304369 await this . shellHomeDirectory . ensureExists ( ) ;
305- } catch ( err : any ) {
306- this . warnAboutInaccessibleFile ( err ) ;
370+ } catch ( err : unknown ) {
371+ this . warnAboutInaccessibleFile ( err as Error ) ;
307372 }
308373 markTime ( TimingCategories . REPLInstantiation , 'ensured shell homedir' ) ;
309374
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- let analyticsSetupError : Error | null = null ;
328- try {
329- await this . setupAnalytics ( ) ;
330- } catch ( err : any ) {
331- // Need to delay emitting the error on the bus so that logging is in place
332- // as well
333- analyticsSetupError = err ;
334- }
335-
336- markTime ( TimingCategories . Telemetry , 'created analytics instance' ) ;
337- setupLoggerAndTelemetry (
338- this . bus ,
339- logger ,
340- this . toggleableAnalytics ,
341- {
342- platform : process . platform ,
343- arch : process . arch ,
344- is_containerized : this . isContainerizedEnvironment ,
345- ...( await getOsInfo ( ) ) ,
346- } ,
347- version
348- ) ;
349- markTime ( TimingCategories . Telemetry , 'completed telemetry setup' ) ;
350-
351- if ( analyticsSetupError ) {
352- this . bus . emit ( 'mongosh:error' , analyticsSetupError , 'analytics' ) ;
353- }
354-
375+ // Get configuration
355376 try {
356377 this . config = await this . configDirectory . generateOrReadConfig (
357378 this . config
358379 ) ;
359- } catch ( err : any ) {
360- this . warnAboutInaccessibleFile ( err ) ;
380+ } catch ( err : unknown ) {
381+ this . warnAboutInaccessibleFile ( err as Error ) ;
361382 }
362383
363384 this . globalConfig = await this . loadGlobalConfigFile ( ) ;
364385 markTime ( TimingCategories . UserConfigLoading , 'read global config files' ) ;
365386
387+ await this . startLoggingAndTelemetry ( version ) ;
388+
366389 // Needs to happen after loading the mongosh config file(s)
367390 void this . fetchMongoshUpdateUrl ( ) ;
368391
@@ -483,7 +506,7 @@ export class CliRepl implements MongoshIOProvider {
483506 if ( ! this . cliOptions . shell ) {
484507 // We flush the telemetry data as part of exiting. Make sure we have
485508 // the right config value.
486- this . setTelemetryEnabled ( await this . getConfig ( 'enableTelemetry' ) ) ;
509+ this . setTelemetryEnabled ( this . getConfig ( 'enableTelemetry' ) ) ;
487510 await this . exit ( 0 ) ;
488511 return ;
489512 }
@@ -516,7 +539,7 @@ export class CliRepl implements MongoshIOProvider {
516539
517540 // We only enable/disable here, since the rc file/command line scripts
518541 // can disable the telemetry setting.
519- this . setTelemetryEnabled ( await this . getConfig ( 'enableTelemetry' ) ) ;
542+ this . setTelemetryEnabled ( this . getConfig ( 'enableTelemetry' ) ) ;
520543 this . bus . emit ( 'mongosh:start-mongosh-repl' , { version } ) ;
521544 markTime ( TimingCategories . REPLInstantiation , 'starting repl' ) ;
522545 await this . mongoshRepl . startRepl ( initialized ) ;
@@ -856,9 +879,7 @@ export class CliRepl implements MongoshIOProvider {
856879 * Implements getConfig from the {@link ConfigProvider} interface.
857880 */
858881 // eslint-disable-next-line @typescript-eslint/require-await
859- async getConfig < K extends keyof CliUserConfig > (
860- key : K
861- ) : Promise < CliUserConfig [ K ] > {
882+ getConfig < K extends keyof CliUserConfig > ( key : K ) : CliUserConfig [ K ] {
862883 return (
863884 ( this . config as CliUserConfig ) [ key ] ??
864885 ( this . globalConfig as CliUserConfig ) ?. [ key ] ??
@@ -1259,7 +1280,7 @@ export class CliRepl implements MongoshIOProvider {
12591280 }
12601281
12611282 try {
1262- const updateURL = ( await this . getConfig ( 'updateURL' ) ) . trim ( ) ;
1283+ const updateURL = this . getConfig ( 'updateURL' ) . trim ( ) ;
12631284 if ( ! updateURL ) return ;
12641285
12651286 const localFilePath = this . shellHomeDirectory . localPath (
0 commit comments