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' ;
64import { mongoLogId } from '@mongodb-js/compass-logging/provider' ;
75import { useRef } from 'react' ;
86
@@ -11,83 +9,19 @@ 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 LogMessage = {
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+ } ;
21+ export type LogFunction = ( message : LogMessage ) => void ;
1822
1923export type DebugFunction = ( ...args : any [ ] ) => void ;
2024
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-
9125type Debugger = Logger [ 'debug' ] ;
9226
9327function createCompassWebDebugger (
@@ -133,9 +67,13 @@ export class CompassWebLoggerAndTelemetry implements Logger {
13367 } ;
13468 }
13569 ) {
136- this . log = new CompassWebLogWriter ( this . callbackRef ) . bindComponent (
70+ const passThrough = new PassThrough ( { objectMode : true } ) ;
71+ this . log = new MongoLogWriter ( '' , '' , passThrough ) . bindComponent (
13772 this . component
13873 ) ;
74+ passThrough . on ( 'data' , ( line ) => {
75+ callbackRef . current . onLog ?.( JSON . parse ( line ) ) ;
76+ } ) ;
13977
14078 this . debug = createCompassWebDebugger ( this . component , this . callbackRef ) ;
14179 }
0 commit comments