Skip to content

Commit f1163e0

Browse files
committed
feat(cli-repl): add configuration to enable log compression
1 parent 27a51ae commit f1163e0

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ describe('CliRepl', function () {
324324
'disableLogging',
325325
'logLocation',
326326
'logRetentionDays',
327+
'logCompression',
327328
] satisfies (keyof CliUserConfig)[]);
328329
});
329330

@@ -1457,6 +1458,14 @@ describe('CliRepl', function () {
14571458
testRetentionDays
14581459
);
14591460
});
1461+
1462+
it('can set log compression', async function () {
1463+
cliRepl.config.logCompression = true;
1464+
await cliRepl.start(await testServer.connectionString(), {});
1465+
1466+
expect(await cliRepl.getConfig('logCompression')).equals(true);
1467+
expect(cliRepl.logManager?._options.gzip).equals(true);
1468+
});
14601469
});
14611470

14621471
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('logCompression'),
263264
maxLogFileCount: +(
264265
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100
265266
),

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 logCompression: 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
@@ -31,6 +31,13 @@ describe('config validation', function () {
3131
expect(await validate('logRetentionDays', -1)).to.equal(
3232
'logRetentionDays must be a positive integer'
3333
);
34+
expect(await validate('logCompression', 'foo')).to.equal(
35+
'logCompression must be a boolean'
36+
);
37+
expect(await validate('logCompression', -1)).to.equal(
38+
'logCompression must be a boolean'
39+
);
40+
expect(await validate('logCompression', false)).to.equal(null);
3441
expect(await validate('showStackTraces', 'foo')).to.equal(
3542
'showStackTraces must be a boolean'
3643
);

packages/types/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
510510
disableLogging = false;
511511
logLocation: string | undefined = undefined;
512512
logRetentionDays = 30;
513+
logCompression = false;
513514
}
514515

515516
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -540,6 +541,7 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
540541
case 'disableLogging':
541542
case 'forceDisableTelemetry':
542543
case 'showStackTraces':
544+
case 'logCompression':
543545
if (typeof value !== 'boolean') {
544546
return `${key} must be a boolean`;
545547
}

0 commit comments

Comments
 (0)