@@ -30,7 +30,8 @@ import type { MongoLogWriter } from 'mongodb-log-writer';
3030import { MongoLogManager , mongoLogId } from 'mongodb-log-writer' ;
3131import type { MongoshNodeReplOptions , MongoshIOProvider } from './mongosh-repl' ;
3232import MongoshNodeRepl from './mongosh-repl' ;
33- import { MongoshLoggingAndTelemetry } from '@mongosh/logging' ;
33+ import type { MongoshLoggingAndTelemetry } from '@mongosh/logging' ;
34+ import { setupLoggingAndTelemetry } from '@mongosh/logging' ;
3435import {
3536 ToggleableAnalytics ,
3637 ThrottledAnalytics ,
@@ -254,36 +255,44 @@ export class CliRepl implements MongoshIOProvider {
254255 throw new Error ( 'Logging and telemetry not setup' ) ;
255256 }
256257
257- if ( ! this . logManager ) {
258- this . logManager = new MongoLogManager ( {
259- directory : this . shellHomeDirectory . localPath ( '.' ) ,
260- retentionDays : 30 ,
261- maxLogFileCount : + (
262- process . env . MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100
263- ) ,
264- onerror : ( err : Error ) => this . bus . emit ( 'mongosh:error' , err , 'log' ) ,
265- onwarn : ( err : Error , path : string ) =>
266- this . warnAboutInaccessibleFile ( err , path ) ,
267- } ) ;
268- }
258+ this . logManager ??= new MongoLogManager ( {
259+ directory : this . shellHomeDirectory . localPath ( '.' ) ,
260+ retentionDays : 30 ,
261+ maxLogFileCount : + (
262+ process . env . MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100
263+ ) ,
264+ onerror : ( err : Error ) => this . bus . emit ( 'mongosh:error' , err , 'log' ) ,
265+ onwarn : ( err : Error , path : string ) =>
266+ this . warnAboutInaccessibleFile ( err , path ) ,
267+ } ) ;
269268
270269 await this . logManager . cleanupOldLogFiles ( ) ;
271270 markTime ( TimingCategories . Logging , 'cleaned up log files' ) ;
272- const logger = await this . logManager . createLogWriter ( ) ;
273- const { quiet } = CliRepl . getFileAndEvalInfo ( this . cliOptions ) ;
274- if ( ! quiet ) {
275- this . output . write ( `Current Mongosh Log ID:\t${ logger . logId } \n` ) ;
271+
272+ if ( ! this . logWriter ) {
273+ this . logWriter ??= await this . logManager . createLogWriter ( ) ;
274+
275+ const { quiet } = CliRepl . getFileAndEvalInfo ( this . cliOptions ) ;
276+ if ( ! quiet ) {
277+ this . output . write ( `Current Mongosh Log ID:\t${ this . logWriter . logId } \n` ) ;
278+ }
279+
280+ markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
276281 }
277282
278- this . logWriter = logger ;
279- this . loggingAndTelemetry . attachLogger ( logger ) ;
283+ this . loggingAndTelemetry . attachLogger ( this . logWriter ) ;
280284
281- markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
282- logger . info ( 'MONGOSH' , mongoLogId ( 1_000_000_000 ) , 'log' , 'Starting log' , {
283- execPath : process . execPath ,
284- envInfo : redactSensitiveData ( this . getLoggedEnvironmentVariables ( ) ) ,
285- ...( await buildInfo ( ) ) ,
286- } ) ;
285+ this . logWriter . info (
286+ 'MONGOSH' ,
287+ mongoLogId ( 1_000_000_000 ) ,
288+ 'log' ,
289+ 'Starting log' ,
290+ {
291+ execPath : process . execPath ,
292+ envInfo : redactSensitiveData ( this . getLoggedEnvironmentVariables ( ) ) ,
293+ ...( await buildInfo ( ) ) ,
294+ }
295+ ) ;
287296 markTime ( TimingCategories . Logging , 'logged initial message' ) ;
288297 }
289298
@@ -350,18 +359,17 @@ export class CliRepl implements MongoshIOProvider {
350359
351360 markTime ( TimingCategories . Telemetry , 'created analytics instance' ) ;
352361
353- this . loggingAndTelemetry = new MongoshLoggingAndTelemetry (
354- this . bus ,
355- this . toggleableAnalytics ,
356- {
362+ this . loggingAndTelemetry = setupLoggingAndTelemetry ( {
363+ bus : this . bus ,
364+ analytics : this . toggleableAnalytics ,
365+ userTraits : {
357366 platform : process . platform ,
358367 arch : process . arch ,
359368 is_containerized : this . isContainerizedEnvironment ,
360369 ...( await getOsInfo ( ) ) ,
361370 } ,
362- version
363- ) ;
364- this . loggingAndTelemetry . setup ( ) ;
371+ mongoshVersion : version ,
372+ } ) ;
365373
366374 markTime ( TimingCategories . Telemetry , 'completed telemetry setup' ) ;
367375
@@ -628,14 +636,9 @@ export class CliRepl implements MongoshIOProvider {
628636
629637 async setLoggingEnabled ( enabled : boolean ) : Promise < void > {
630638 if ( enabled ) {
631- if ( ! this . logWriter ) {
632- await this . startLogging ( ) ;
633- }
634- // Do nothing if there is already an active log writer.
639+ await this . startLogging ( ) ;
635640 } else {
636641 this . loggingAndTelemetry ?. detachLogger ( ) ;
637- this . logWriter ?. destroy ( ) ;
638- this . logWriter = undefined ;
639642 }
640643 }
641644
0 commit comments