@@ -30,11 +30,11 @@ 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' ;
3334import {
3435 ToggleableAnalytics ,
3536 ThrottledAnalytics ,
3637 SampledAnalytics ,
37- MongoshLoggingAndTelemetry ,
3838} from '@mongosh/logging' ;
3939import type { MongoshBus } from '@mongosh/types' ;
4040import {
@@ -119,7 +119,7 @@ export class CliRepl implements MongoshIOProvider {
119119 config : CliUserConfigOnDisk ;
120120 globalConfig : Partial < CliUserConfig > | null = null ;
121121 globalConfigPaths : string [ ] ;
122- logManager : MongoLogManager ;
122+ logManager ? : MongoLogManager ;
123123 logWriter ?: MongoLogWriter ;
124124 input : Readable ;
125125 output : Writable ;
@@ -140,6 +140,8 @@ export class CliRepl implements MongoshIOProvider {
140140 fetchMongoshUpdateUrlRegardlessOfCiEnvironment = false ; // for testing
141141 cachedGlibcVersion : null | string | undefined = null ;
142142
143+ private loggingAndTelemetry : MongoshLoggingAndTelemetry | undefined ;
144+
143145 /**
144146 * Instantiate the new CLI Repl.
145147 */
@@ -189,16 +191,6 @@ export class CliRepl implements MongoshIOProvider {
189191 } ) ;
190192 } ) ;
191193
192- this . logManager = new MongoLogManager ( {
193- directory : this . shellHomeDirectory . localPath ( '.' ) ,
194- retentionDays : 30 ,
195- maxLogFileCount : + (
196- process . env . MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100
197- ) ,
198- onerror : ( err : Error ) => this . bus . emit ( 'mongosh:error' , err , 'log' ) ,
199- onwarn : ( err : Error , path : string ) =>
200- this . warnAboutInaccessibleFile ( err , path ) ,
201- } ) ;
202194 this . agent = useOrCreateAgent ( this . proxyOptions ) ;
203195 this . updateNotificationManager = new UpdateNotificationManager ( {
204196 proxyOptions : this . agent ,
@@ -257,9 +249,24 @@ export class CliRepl implements MongoshIOProvider {
257249 }
258250
259251 /** Setup log writer and start logging. */
260- private async startLogging (
261- loggingAndTelemetry : MongoshLoggingAndTelemetry
262- ) : Promise < void > {
252+ private async startLogging ( ) : Promise < void > {
253+ if ( ! this . loggingAndTelemetry ) {
254+ throw new Error ( 'Logging and telemetry not setup' ) ;
255+ }
256+
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+ }
269+
263270 await this . logManager . cleanupOldLogFiles ( ) ;
264271 markTime ( TimingCategories . Logging , 'cleaned up log files' ) ;
265272 const logger = await this . logManager . createLogWriter ( ) ;
@@ -269,7 +276,7 @@ export class CliRepl implements MongoshIOProvider {
269276 }
270277
271278 this . logWriter = logger ;
272- loggingAndTelemetry . setupLogger ( logger ) ;
279+ this . loggingAndTelemetry . attachLogger ( logger ) ;
273280
274281 markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
275282 logger . info ( 'MONGOSH' , mongoLogId ( 1_000_000_000 ) , 'log' , 'Starting log' , {
@@ -343,7 +350,7 @@ export class CliRepl implements MongoshIOProvider {
343350
344351 markTime ( TimingCategories . Telemetry , 'created analytics instance' ) ;
345352
346- const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
353+ this . loggingAndTelemetry = new MongoshLoggingAndTelemetry (
347354 this . bus ,
348355 this . toggleableAnalytics ,
349356 {
@@ -354,7 +361,7 @@ export class CliRepl implements MongoshIOProvider {
354361 } ,
355362 version
356363 ) ;
357- loggingAndTelemetry . setup ( ) ;
364+ this . loggingAndTelemetry . setup ( ) ;
358365
359366 markTime ( TimingCategories . Telemetry , 'completed telemetry setup' ) ;
360367
@@ -374,10 +381,7 @@ export class CliRepl implements MongoshIOProvider {
374381 this . globalConfig = await this . loadGlobalConfigFile ( ) ;
375382 markTime ( TimingCategories . UserConfigLoading , 'read global config files' ) ;
376383
377- const disableLogging = await this . getConfig ( 'disableLogging' ) ;
378- if ( disableLogging !== true ) {
379- await this . startLogging ( loggingAndTelemetry ) ;
380- }
384+ await this . setLoggingEnabled ( ! ( await this . getConfig ( 'disableLogging' ) ) ) ;
381385
382386 // Needs to happen after loading the mongosh config file(s)
383387 void this . fetchMongoshUpdateUrl ( ) ;
@@ -622,6 +626,19 @@ export class CliRepl implements MongoshIOProvider {
622626 ) ;
623627 }
624628
629+ async setLoggingEnabled ( enabled : boolean ) : Promise < void > {
630+ if ( enabled ) {
631+ if ( ! this . logWriter ) {
632+ await this . startLogging ( ) ;
633+ }
634+ // Do nothing if there is already an active log writer.
635+ } else {
636+ this . loggingAndTelemetry ?. detachLogger ( ) ;
637+ this . logWriter ?. destroy ( ) ;
638+ this . logWriter = undefined ;
639+ }
640+ }
641+
625642 setTelemetryEnabled ( enabled : boolean ) : void {
626643 if ( this . globalConfig === null ) {
627644 // This happens when the per-user config file is loaded before we have
@@ -908,6 +925,9 @@ export class CliRepl implements MongoshIOProvider {
908925 anonymousId : this . config . telemetryAnonymousId ,
909926 } ) ;
910927 }
928+ if ( key === 'disableLogging' ) {
929+ await this . setLoggingEnabled ( ! value ) ;
930+ }
911931 try {
912932 await this . configDirectory . writeConfigFile ( this . config ) ;
913933 } catch ( err : any ) {
0 commit comments