Skip to content

Commit 6a6e590

Browse files
committed
feat(cli-repl): add ability to set log retention days MONGOSH-1984
1 parent 2f2937c commit 6a6e590

File tree

5 files changed

+25
-29
lines changed

5 files changed

+25
-29
lines changed

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

+14-27
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ describe('CliRepl', function () {
323323
'updateURL',
324324
'disableLogging',
325325
'logLocation',
326+
'logRetentionDays',
326327
] satisfies (keyof CliUserConfig)[]);
327328
});
328329

@@ -1427,33 +1428,6 @@ describe('CliRepl', function () {
14271428
)
14281429
).to.have.lengthOf(1);
14291430
});
1430-
});
1431-
1432-
context('logging configuration', function () {
1433-
it('logging is enabled by default and event is called', async function () {
1434-
const onLogInitialized = sinon.stub();
1435-
cliRepl.bus.on('mongosh:log-initialized', onLogInitialized);
1436-
1437-
await cliRepl.start(await testServer.connectionString(), {});
1438-
1439-
expect(await cliRepl.getConfig('disableLogging')).is.false;
1440-
1441-
expect(onLogInitialized).calledOnce;
1442-
expect(cliRepl.logWriter).is.instanceOf(MongoLogWriter);
1443-
});
1444-
1445-
it('does not initialize logging when it is disabled', async function () {
1446-
cliRepl.config.disableLogging = true;
1447-
const onLogInitialized = sinon.stub();
1448-
cliRepl.bus.on('mongosh:log-initialized', onLogInitialized);
1449-
1450-
await cliRepl.start(await testServer.connectionString(), {});
1451-
1452-
expect(await cliRepl.getConfig('disableLogging')).is.true;
1453-
expect(onLogInitialized).not.called;
1454-
1455-
expect(cliRepl.logWriter).is.undefined;
1456-
});
14571431

14581432
it('can set the log location', async function () {
14591433
const testPath = path.join('./test', 'path');
@@ -1463,6 +1437,19 @@ describe('CliRepl', function () {
14631437
expect(cliRepl.getConfig('logLocation')).is.true;
14641438
expect(cliRepl.logWriter?.logFilePath).equals(testPath);
14651439
});
1440+
1441+
it('can set log retention days', async function () {
1442+
const testRetentionDays = 123;
1443+
cliRepl.config.logRetentionDays = testRetentionDays;
1444+
await cliRepl.start(await testServer.connectionString(), {});
1445+
1446+
expect(cliRepl.getConfig('logRetentionDays')).equals(
1447+
testRetentionDays
1448+
);
1449+
expect(cliRepl.logManager?._options.retentionDays).equals(
1450+
testRetentionDays
1451+
);
1452+
});
14661453
});
14671454

14681455
it('times out fast', async function () {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export class CliRepl implements MongoshIOProvider {
259259
directory:
260260
(await this.getConfig('logLocation')) ||
261261
this.shellHomeDirectory.localPath('.'),
262-
retentionDays: 30,
262+
retentionDays: await this.getConfig('logRetentionDays'),
263263
maxLogFileCount: +(
264264
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100
265265
),

packages/e2e-tests/test/e2e.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { once } from 'events';
2727
import type { AddressInfo } from 'net';
2828
const { EJSON } = bson;
2929
import { sleep } from './util-helpers';
30+
import type { LogEntry } from '@mongosh/shell-api/src/log-entry';
3031

3132
const jsContextFlagCombinations: `--jsContext=${'plain-vm' | 'repl'}`[][] = [
3233
[],
@@ -1361,7 +1362,7 @@ describe('e2e', function () {
13611362
let historyPath: string;
13621363
let readConfig: () => Promise<any>;
13631364
let readLogFile: <T extends MongoLogEntryFromFile>(
1364-
customBasePath?: string
1365+
path?: string
13651366
) => Promise<T[]>;
13661367
let startTestShell: (...extraArgs: string[]) => Promise<TestShell>;
13671368

packages/types/src/index.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ describe('config validation', function () {
2525
expect(await validate('historyLength', 0)).to.equal(null);
2626
expect(await validate('historyLength', 1)).to.equal(null);
2727
expect(await validate('historyLength', Infinity)).to.equal(null);
28+
expect(await validate('logRetentionDays', 'foo')).to.equal(
29+
'logRetentionDays must be a positive integer'
30+
);
31+
expect(await validate('logRetentionDays', -1)).to.equal(
32+
'logRetentionDays must be a positive integer'
33+
);
2834
expect(await validate('showStackTraces', 'foo')).to.equal(
2935
'showStackTraces must be a boolean'
3036
);

packages/types/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
509509
updateURL = 'https://downloads.mongodb.com/compass/mongosh.json';
510510
disableLogging = false;
511511
logLocation: string | undefined = undefined;
512+
logRetentionDays = 30;
512513
}
513514

514515
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -531,6 +532,7 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
531532
return null;
532533
case 'inspectDepth':
533534
case 'historyLength':
535+
case 'logRetentionDays':
534536
if (typeof value !== 'number' || value < 0) {
535537
return `${key} must be a positive integer`;
536538
}

0 commit comments

Comments
 (0)