Skip to content

Commit 51ef2b7

Browse files
committed
fix: wrap the bus for hookLogger
Wrapping the bus can allow us to inject onBus onto the logger which can help us prevent logs being created from there. That said, we still have the problem of having to either re-register those hooks each time, so we may want to instead update hookLogger to take function for getting the latest log rather than just a variable.
1 parent c0dd05a commit 51ef2b7

File tree

3 files changed

+228
-134
lines changed

3 files changed

+228
-134
lines changed

packages/logging/src/helpers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { MongoshBus, MongoshBusEventsMap } from '@mongosh/types';
2+
import { once } from 'events';
3+
14
/**
25
* A helper class for keeping track of how often specific events occurred.
36
*/
@@ -47,3 +50,10 @@ export function toSnakeCase(str: string): string {
4750

4851
return matches.map((x) => x.toLowerCase()).join('_');
4952
}
53+
54+
export async function waitBus<K extends keyof MongoshBusEventsMap>(
55+
bus: MongoshBus,
56+
event: K
57+
): Promise<void> {
58+
await once(bus, event);
59+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,29 @@ describe('MongoshLoggingAndTelemetry', function () {
221221
expect(analyticsOutput).to.have.lengthOf(1);
222222
});
223223

224+
it('detaching logger applies to devtools-connect cases', function () {
225+
loggingAndTelemetry.setup();
226+
loggingAndTelemetry.attachLogger(logger);
227+
228+
bus.emit('devtools-connect:connect-fail-early');
229+
bus.emit('devtools-connect:connect-fail-early');
230+
231+
expect(logOutput).to.have.lengthOf(2);
232+
// No analytics event attached to this
233+
expect(analyticsOutput).to.have.lengthOf(0);
234+
235+
loggingAndTelemetry.detachLogger();
236+
bus.emit('devtools-connect:connect-fail-early');
237+
238+
expect(logOutput).to.have.lengthOf(2);
239+
expect(analyticsOutput).to.have.lengthOf(0);
240+
241+
loggingAndTelemetry.attachLogger(logger);
242+
243+
bus.emit('devtools-connect:connect-fail-early');
244+
expect(logOutput).to.have.lengthOf(3);
245+
});
246+
224247
it('detaching logger mid-way leads to no logging but persists analytics', function () {
225248
loggingAndTelemetry.setup();
226249
loggingAndTelemetry.attachLogger(logger);

0 commit comments

Comments
 (0)