Skip to content

Commit 0aec4d5

Browse files
committed
feat(cli-repl): add configuration to set max log files size MONGOSH-1985
1 parent 9a0b368 commit 0aec4d5

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ describe('CliRepl', function () {
324324
'disableLogging',
325325
'logLocation',
326326
'logRetentionDays',
327-
'logMaxFileCount',
327+
'logRetentionGB',
328328
] satisfies (keyof CliUserConfig)[]);
329329
});
330330

@@ -1459,6 +1459,19 @@ describe('CliRepl', function () {
14591459
);
14601460
});
14611461

1462+
it('can set log retention GB', async function () {
1463+
const testLogRetentionGB = 10;
1464+
cliRepl.config.logRetentionGB = testLogRetentionGB;
1465+
await cliRepl.start(await testServer.connectionString(), {});
1466+
1467+
expect(cliRepl.getConfig('logRetentionGB')).equals(
1468+
testLogRetentionGB
1469+
);
1470+
expect(cliRepl.logManager?._options.logRetentionGB).equals(
1471+
testLogRetentionGB
1472+
);
1473+
});
1474+
14621475
it('can set log max file count', async function () {
14631476
const testMaxFileCount = 123;
14641477
cliRepl.config.logMaxFileCount = testMaxFileCount;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ export class CliRepl implements MongoshIOProvider {
260260
(await this.getConfig('logLocation')) ||
261261
this.shellHomeDirectory.localPath('.'),
262262
retentionDays: await this.getConfig('logRetentionDays'),
263+
retentionGB: await this.getConfig('logRetentionGB'),
263264
maxLogFileCount: +(
264265
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT ||
265266
(await this.getConfig('logMaxFileCount'))

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,59 @@ describe('e2e', function () {
17951795
});
17961796
});
17971797

1798+
describe('with custom log retention max logs size', function () {
1799+
const customLogDir = useTmpdir();
1800+
1801+
it('should delete files once it is above the logs retention GB', async function () {
1802+
const globalConfig = path.join(homedir, 'globalconfig.conf');
1803+
await fs.writeFile(
1804+
globalConfig,
1805+
// Set logRetentionGB to 4 KB
1806+
`mongosh:\n logLocation: "${
1807+
customLogDir.path
1808+
}"\n logRetentionGB: ${4 / 1024 / 1024}`
1809+
);
1810+
const paths: string[] = [];
1811+
const offset = Math.floor(Date.now() / 1000);
1812+
1813+
// Create 10 log files, 1kb each
1814+
for (let i = 9; i >= 0; i--) {
1815+
const filename = path.join(
1816+
customLogDir.path,
1817+
ObjectId.createFromTime(offset - i).toHexString() + '_log'
1818+
);
1819+
await fs.writeFile(filename, '0'.repeat(1024));
1820+
paths.push(filename);
1821+
}
1822+
1823+
// All 10 existing log files exist.
1824+
expect(await getFilesState(paths)).to.equal('1111111111');
1825+
shell = this.startTestShell({
1826+
args: ['--nodb'],
1827+
env: {
1828+
...env,
1829+
MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT: '',
1830+
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
1831+
},
1832+
forceTerminal: true,
1833+
});
1834+
1835+
await shell.waitForPrompt();
1836+
1837+
// Add the newly created log to the file list.
1838+
paths.push(
1839+
path.join(customLogDir.path, `${shell.logId as string}_log`)
1840+
);
1841+
1842+
expect(
1843+
await shell.executeLine('config.get("logRetentionGB")')
1844+
).contains(`${4 / 1024 / 1024}`);
1845+
1846+
// Expect 6 files to be deleted and 5 to remain (including the new log file)
1847+
expect(await getFilesState(paths)).to.equal('00000011111');
1848+
});
1849+
});
1850+
17981851
it('creates a log file that keeps track of session events', async function () {
17991852
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
18001853
const log = await readLogFile();

packages/types/src/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ describe('config validation', function () {
3131
expect(await validate('logRetentionDays', -1)).to.equal(
3232
'logRetentionDays must be a positive integer'
3333
);
34+
expect(await validate('logRetentionGB', 'foo')).to.equal(
35+
'logRetentionGB must be a positive integer'
36+
);
37+
expect(await validate('logRetentionDays', -1)).to.equal(
38+
'logRetentionGB must be a positive integer'
39+
);
3440
expect(await validate('logMaxFileCount', 'foo')).to.equal(
3541
'logMaxFileCount must be a positive integer'
3642
);

packages/types/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
511511
logLocation: string | undefined = undefined;
512512
logRetentionDays = 30;
513513
logMaxFileCount = 100;
514+
logRetentionGB: number | undefined = undefined;
514515
}
515516

516517
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -539,6 +540,11 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
539540
return `${key} must be a positive integer`;
540541
}
541542
return null;
543+
case 'logRetentionGB':
544+
if (value !== undefined && (typeof value !== 'number' || value < 0)) {
545+
return `${key} must be a positive number or empty`;
546+
}
547+
return null;
542548
case 'disableLogging':
543549
case 'forceDisableTelemetry':
544550
case 'showStackTraces':

0 commit comments

Comments
 (0)