Skip to content

Commit 3060ce3

Browse files
committed
WIP: add log location
1 parent 5147039 commit 3060ce3

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ describe('CliRepl', function () {
301301
'browser',
302302
'updateURL',
303303
'disableLogging',
304+
'logLocation',
304305
] satisfies (keyof CliUserConfig)[]);
305306
});
306307

@@ -1342,6 +1343,15 @@ describe('CliRepl', function () {
13421343
expect(emitSpy).not.calledWith('mongosh:logger-initialized');
13431344
expect(cliRepl.logWriter).is.undefined;
13441345
});
1346+
1347+
it('can set the log location', async function () {
1348+
const testPath = './test/path';
1349+
cliRepl.config.logLocation = testPath;
1350+
await cliRepl.start(await testServer.connectionString(), {});
1351+
1352+
expect(cliRepl.getConfig('logLocation')).is.true;
1353+
expect(cliRepl.logWriter?.logFilePath).equals(testPath);
1354+
});
13451355
});
13461356

13471357
context('analytics integration', function () {

packages/cli-repl/src/cli-repl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ export class CliRepl implements MongoshIOProvider {
190190
});
191191

192192
this.logManager = new MongoLogManager({
193-
directory: this.shellHomeDirectory.localPath('.'),
193+
directory:
194+
this.getConfig('logLocation') ?? this.shellHomeDirectory.localPath('.'),
194195
retentionDays: 30,
195196
maxLogFileCount: +(
196197
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100

packages/types/src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
502502
browser: undefined | false | string = undefined;
503503
updateURL = 'https://downloads.mongodb.com/compass/mongosh.json';
504504
disableLogging = false;
505+
logLocation: string | undefined = undefined;
505506
}
506507

507508
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -574,6 +575,15 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
574575
return `${key} must be a valid URL or empty`;
575576
}
576577
return null;
578+
case 'logLocation':
579+
if (
580+
value !== undefined &&
581+
typeof value !== 'string' &&
582+
!isValidPath(value)
583+
) {
584+
return `${key} must be a valid path or empty`;
585+
}
586+
return null;
577587
default:
578588
return super.validate(
579589
key as keyof SnippetShellUserConfig,
@@ -619,6 +629,15 @@ function isValidUrl(url: string): boolean {
619629
return true; // Currently no overlap between URL-less environments and environments with config options.
620630
}
621631

632+
function isValidPath(path: unknown): boolean {
633+
const pathRegex = /^(?:[a-zA-Z]:)?(?:[\\/][\w.-]+)+[\\/]?$/;
634+
635+
if (typeof path === 'string') {
636+
return pathRegex.test(path);
637+
}
638+
return false;
639+
}
640+
622641
export const TimingCategories = {
623642
REPLInstantiation: 'REPLInstantiation',
624643
UserConfigLoading: 'UserConfigLoading',

0 commit comments

Comments
 (0)