Skip to content

Commit e6e9338

Browse files
committed
feat(cli-repl): add configuration to set max log files size MONGOSH-1985
1 parent 2d12af3 commit e6e9338

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

Diff for: packages/cli-repl/src/cli-repl.spec.ts

+14-1
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

@@ -1452,6 +1452,19 @@ describe('CliRepl', function () {
14521452
);
14531453
});
14541454

1455+
it('can set log retention GB', async function () {
1456+
const testLogRetentionGB = 10;
1457+
cliRepl.config.logRetentionGB = testLogRetentionGB;
1458+
await cliRepl.start(await testServer.connectionString(), {});
1459+
1460+
expect(cliRepl.getConfig('logRetentionGB')).equals(
1461+
testLogRetentionGB
1462+
);
1463+
expect(cliRepl.logManager?._options.logRetentionGB).equals(
1464+
testLogRetentionGB
1465+
);
1466+
});
1467+
14551468
it('can set log max file count', async function () {
14561469
const testMaxFileCount = 123;
14571470
cliRepl.config.logMaxFileCount = testMaxFileCount;

Diff for: packages/cli-repl/src/cli-repl.ts

+1
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'))

Diff for: packages/e2e-tests/test/e2e.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,59 @@ describe('e2e', function () {
17931793
});
17941794
});
17951795

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

Diff for: packages/types/src/index.spec.ts

+6
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
);

Diff for: packages/types/src/index.ts

+6
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)