Skip to content

Commit 7c166bb

Browse files
committed
chore(compass-web): do not expose internal logger details to web onLog hook
Instead, pass the processed log contents to it.
1 parent 3eae890 commit 7c166bb

File tree

3 files changed

+33
-90
lines changed

3 files changed

+33
-90
lines changed

packages/compass-web/src/logger-and-telemetry.spec.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ describe('useCompassWebLoggerAndTelemetry', function () {
4242
beforeEach(cleanup);
4343

4444
it('should call callback props when logger is called', function () {
45-
const onLog = Sinon.stub();
45+
const logs: any[] = [];
46+
const onLog = Sinon.stub().callsFake((entry) => logs.push(entry));
4647
const onTrack = Sinon.stub();
4748
const onDebug = Sinon.stub();
4849

@@ -54,13 +55,16 @@ describe('useCompassWebLoggerAndTelemetry', function () {
5455
loggerAndTelemetry.log.info(mongoLogId(123), 'Ctx', 'msg', { attr: 1 });
5556

5657
expect(onDebug).to.have.been.calledOnceWith('TEST', 'foo bar');
57-
expect(onLog).to.have.been.calledOnceWith(
58-
'info',
59-
'TEST',
60-
mongoLogId(123),
61-
'Ctx',
62-
'msg',
63-
{ attr: 1 }
64-
);
58+
expect(logs).to.deep.equal([
59+
{
60+
t: logs[0].t,
61+
s: 'I',
62+
c: 'TEST',
63+
id: 123,
64+
ctx: 'Ctx',
65+
msg: 'msg',
66+
attr: { attr: 1 },
67+
},
68+
]);
6569
});
6670
});

packages/compass-web/src/logger-and-telemetry.tsx

Lines changed: 17 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import type {
2-
Logger,
3-
MongoLogWriter,
4-
} from '@mongodb-js/compass-logging/provider';
5-
import { Writable } from 'stream';
1+
import type { Logger } from '@mongodb-js/compass-logging/provider';
2+
import { MongoLogWriter } from 'mongodb-log-writer/mongo-log-writer';
3+
import { PassThrough } from 'stream';
64
import { mongoLogId } from '@mongodb-js/compass-logging/provider';
75
import { useRef } from 'react';
86

@@ -11,83 +9,18 @@ export type TrackFunction = (
119
properties: Record<string, any>
1210
) => void;
1311

14-
export type LogFunction = (
15-
type: 'debug' | 'info' | 'warn' | 'error' | 'fatal',
16-
...args: any[]
17-
) => void;
12+
export type LogFunction = (message: {
13+
id: number;
14+
t?: { $date: string };
15+
s: 'F' | 'E' | 'W' | 'I' | 'D1' | 'D2' | 'D3' | 'D4' | 'D5';
16+
c: string;
17+
ctx: string;
18+
msg: string;
19+
attr?: any;
20+
}) => void;
1821

1922
export type DebugFunction = (...args: any[]) => void;
2023

21-
class CompassWebLogWriter extends Writable implements MongoLogWriter {
22-
_logId = '';
23-
get logId() {
24-
return this._logId;
25-
}
26-
_logFilePath = null;
27-
get logFilePath() {
28-
return this._logFilePath;
29-
}
30-
_target = {} as Writable;
31-
get target() {
32-
return this._target;
33-
}
34-
_now = () => {
35-
return new Date();
36-
};
37-
constructor(private callbackRef: { current: { onLog?: LogFunction } }) {
38-
super();
39-
}
40-
private _log = (
41-
type: 'debug' | 'info' | 'warn' | 'error' | 'fatal',
42-
...args: any[]
43-
) => {
44-
this.callbackRef?.current.onLog?.(type, ...args);
45-
};
46-
mongoLogId = mongoLogId;
47-
flush = () => {
48-
return Promise.resolve();
49-
};
50-
debug = (...args: any[]) => {
51-
this._log('debug', ...args);
52-
};
53-
info = (...args: any[]) => {
54-
this._log('info', ...args);
55-
};
56-
warn = (...args: any[]) => {
57-
this._log('warn', ...args);
58-
};
59-
error = (...args: any[]) => {
60-
this._log('error', ...args);
61-
};
62-
fatal = (...args: any[]) => {
63-
this._log('fatal', ...args);
64-
};
65-
bindComponent = (component: string) => {
66-
return {
67-
component,
68-
unbound: this as CompassWebLogWriter,
69-
debug: (...args: any[]) => {
70-
return this.info(component, ...args);
71-
},
72-
info: (...args: any[]) => {
73-
return this.info(component, ...args);
74-
},
75-
warn: (...args: any[]) => {
76-
return this.warn(component, ...args);
77-
},
78-
error: (...args: any[]) => {
79-
return this.error(component, ...args);
80-
},
81-
fatal: (...args: any[]) => {
82-
return this.fatal(component, ...args);
83-
},
84-
write: () => {
85-
return true;
86-
},
87-
};
88-
};
89-
}
90-
9124
type Debugger = Logger['debug'];
9225

9326
function createCompassWebDebugger(
@@ -133,9 +66,13 @@ export class CompassWebLoggerAndTelemetry implements Logger {
13366
};
13467
}
13568
) {
136-
this.log = new CompassWebLogWriter(this.callbackRef).bindComponent(
69+
const passThrough = new PassThrough({ objectMode: true });
70+
this.log = new MongoLogWriter('', '', passThrough).bindComponent(
13771
this.component
13872
);
73+
passThrough.on('data', (line) => {
74+
callbackRef.current.onLog?.(JSON.parse(line));
75+
});
13976

14077
this.debug = createCompassWebDebugger(this.component, this.callbackRef);
14178
}

packages/compass-web/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"extends": "@mongodb-js/tsconfig-compass/tsconfig.react.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"moduleResolution": "node16",
6+
"module": "node16"
57
},
68
"include": [
79
"src/**/*",

0 commit comments

Comments
 (0)