Skip to content

Commit ba69f4c

Browse files
committed
feat!(mongodb-log-writer): add ability to disable logging MONGOSH-1988
1 parent b9ffc87 commit ba69f4c

File tree

5 files changed

+145
-89
lines changed

5 files changed

+145
-89
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,13 @@ describe('CliRepl', function () {
15191519
expect(totalEventsTracked).to.equal(4);
15201520
});
15211521

1522+
it('uses the disable logging setting', function () {
1523+
cliReplOptions.disableLogging = true;
1524+
cliRepl = new CliRepl(cliReplOptions);
1525+
1526+
expect(cliRepl.logWriter?.isDisabled).equals(true);
1527+
});
1528+
15221529
it('sends out telemetry data for command line scripts', async function () {
15231530
cliReplOptions.shellCliOptions.eval = ['db.hello()'];
15241531
cliRepl = new CliRepl(cliReplOptions);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export type CliReplOptions = {
9797
onExit: (code?: number) => never;
9898
/** Optional analytics override options. */
9999
analyticsOptions?: AnalyticsOptions;
100+
/** Disables writing logs. */
101+
disableLogging?: boolean;
100102
} & Pick<MongoshNodeReplOptions, 'nodeReplOptions'>;
101103

102104
/** The set of config options that is *always* available in config files stored on the file system. */

packages/logging/src/multi-set.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* A helper class for keeping track of how often specific events occurred.
3+
*/
4+
export class MultiSet<T extends Record<string, unknown>> {
5+
_entries: Map<string, number> = new Map();
6+
7+
add(entry: T): void {
8+
const key = JSON.stringify(Object.entries(entry).sort());
9+
this._entries.set(key, (this._entries.get(key) ?? 0) + 1);
10+
}
11+
12+
clear(): void {
13+
this._entries.clear();
14+
}
15+
16+
*[Symbol.iterator](): Iterator<[T, number]> {
17+
for (const [key, count] of this._entries) {
18+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
19+
yield [Object.fromEntries(JSON.parse(key)) as T, count];
20+
}
21+
}
22+
}

packages/logging/src/setup-logger-and-telemetry.spec.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { EventEmitter } from 'events';
66
import { MongoshInvalidInputError } from '@mongosh/errors';
77
import type { MongoshBus } from '@mongosh/types';
88
import { toSnakeCase } from './setup-logger-and-telemetry';
9+
import type { Writable } from 'stream';
910

1011
describe('toSnakeCase', function () {
1112
const useCases = [
@@ -37,15 +38,19 @@ describe('setupLoggerAndTelemetry', function () {
3738
const userId = '53defe995fa47e6c13102d9d';
3839
const logId = '5fb3c20ee1507e894e5340f3';
3940

40-
const logger = new MongoLogWriter(logId, `/tmp/${logId}_log`, {
41-
write(chunk: string, cb: () => void) {
42-
logOutput.push(JSON.parse(chunk));
43-
cb();
44-
},
45-
end(cb: () => void) {
46-
cb();
47-
},
48-
} as any);
41+
const logger = new MongoLogWriter({
42+
logId,
43+
logFilePath: `/tmp/${logId}_log`,
44+
target: {
45+
write(chunk: string, cb: () => void) {
46+
logOutput.push(JSON.parse(chunk));
47+
cb();
48+
},
49+
end(cb: () => void) {
50+
cb();
51+
},
52+
} as Writable,
53+
});
4954
const analytics = {
5055
identify(info: any) {
5156
analyticsOutput.push(['identify', info]);

0 commit comments

Comments
 (0)