Skip to content

Commit 88cf836

Browse files
authored
feat(cli-repl): add configuration to enable log compression MONGOSH-1986 (#2362)
1 parent 8aed4c6 commit 88cf836

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ describe('CliRepl', function () {
325325
'logLocation',
326326
'logRetentionDays',
327327
'logMaxFileCount',
328+
'logCompressionEnabled',
328329
] satisfies (keyof CliUserConfig)[]);
329330
});
330331

@@ -1477,6 +1478,16 @@ describe('CliRepl', function () {
14771478
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT =
14781479
oldEnvironmentLimit;
14791480
});
1481+
1482+
it('can set log compression', async function () {
1483+
cliRepl.config.logCompressionEnabled = true;
1484+
await cliRepl.start(await testServer.connectionString(), {});
1485+
1486+
expect(await cliRepl.getConfig('logCompressionEnabled')).equals(
1487+
true
1488+
);
1489+
expect(cliRepl.logManager?._options.gzip).equals(true);
1490+
});
14801491
});
14811492

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

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+
gzip: await this.getConfig('logCompressionEnabled'),
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,49 @@ describe('e2e', function () {
16911691
).join('');
16921692
};
16931693

1694+
describe('with custom log compression', function () {
1695+
const customLogDir = useTmpdir();
1696+
1697+
it('should created compressed files when enabled', async function () {
1698+
const globalConfig = path.join(homedir, 'globalconfig.conf');
1699+
await fs.writeFile(
1700+
globalConfig,
1701+
`mongosh:\n logLocation: ${JSON.stringify(
1702+
customLogDir.path
1703+
)}\n logCompressionEnabled: true`
1704+
);
1705+
1706+
shell = this.startTestShell({
1707+
args: ['--nodb'],
1708+
env: {
1709+
...env,
1710+
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
1711+
},
1712+
forceTerminal: true,
1713+
});
1714+
1715+
await shell.waitForPrompt();
1716+
1717+
const logFile = path.join(
1718+
customLogDir.path,
1719+
`${shell.logId as string}_log`
1720+
);
1721+
const logFileGzip = path.join(
1722+
customLogDir.path,
1723+
`${shell.logId as string}_log.gz`
1724+
);
1725+
1726+
// Only the gzipped file should exist
1727+
expect(await getFilesState([logFile, logFileGzip])).equals('01');
1728+
1729+
const logContent = await fs.readFile(logFileGzip);
1730+
1731+
// gzipped files start with 0x1f 0x8b
1732+
expect(logContent[0]).equals(0x1f);
1733+
expect(logContent[1]).equals(0x8b);
1734+
});
1735+
});
1736+
16941737
describe('with custom log retention days', function () {
16951738
const customLogDir = useTmpdir();
16961739

packages/types/src/index.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ describe('config validation', function () {
3737
expect(await validate('logMaxFileCount', -1)).to.equal(
3838
'logMaxFileCount must be a positive integer'
3939
);
40+
expect(await validate('logCompressionEnabled', 'foo')).to.equal(
41+
'logCompressionEnabled must be a boolean'
42+
);
43+
expect(await validate('logCompressionEnabled', -1)).to.equal(
44+
'logCompressionEnabled must be a boolean'
45+
);
46+
expect(await validate('logCompressionEnabled', false)).to.equal(null);
4047
expect(await validate('showStackTraces', 'foo')).to.equal(
4148
'showStackTraces must be a boolean'
4249
);

packages/types/src/index.ts

Lines changed: 2 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+
logCompressionEnabled = false;
514515
}
515516

516517
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -542,6 +543,7 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
542543
case 'disableLogging':
543544
case 'forceDisableTelemetry':
544545
case 'showStackTraces':
546+
case 'logCompressionEnabled':
545547
if (typeof value !== 'boolean') {
546548
return `${key} must be a boolean`;
547549
}

0 commit comments

Comments
 (0)