Skip to content

Commit 928349c

Browse files
committed
refactor: finalise pr
1 parent 3ae2980 commit 928349c

File tree

5 files changed

+67
-49
lines changed

5 files changed

+67
-49
lines changed

packages/e2e-tests/test/e2e.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,10 +1513,6 @@ describe('e2e', function () {
15131513
});
15141514

15151515
it('writes custom log directly', async function () {
1516-
const connectionString = await testServer.connectionString();
1517-
await shell.executeLine(
1518-
`connect(${JSON.stringify(connectionString)})`
1519-
);
15201516
await shell.executeLine("log.info('This is a custom entry')");
15211517
await eventually(async () => {
15221518
const log = await readLogfile();
@@ -1537,14 +1533,19 @@ describe('e2e', function () {
15371533
__dirname,
15381534
'..',
15391535
'..',
1540-
'cli-repl',
1536+
'e2e-tests',
15411537
'test',
15421538
'fixtures',
15431539
'custom-log-info.js'
15441540
);
1545-
await shell.executeLine(`load('${filename}')`);
1541+
await shell.executeLine(`load(${JSON.stringify(filename)})`);
15461542
await eventually(async () => {
15471543
const log = await readLogfile();
1544+
expect(
1545+
log.filter((logEntry) =>
1546+
logEntry.msg.includes('Initiating connection attemp')
1547+
)
1548+
).to.have.lengthOf(1);
15481549
expect(
15491550
log.filter((logEntry) => logEntry.msg.includes('Hi there'))
15501551
).to.have.lengthOf(1);
@@ -1975,7 +1976,7 @@ describe('e2e', function () {
19751976
__dirname,
19761977
'..',
19771978
'..',
1978-
'cli-repl',
1979+
'e2e-tests',
19791980
'test',
19801981
'fixtures',
19811982
'simple-console-log.js'

packages/shell-api/src/shell-instance-state.ts

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import NoDatabase from './no-db';
3535
import type { ShellBson } from './shell-bson';
3636
import constructShellBson from './shell-bson';
3737
import { Streams } from './streams';
38+
import constructShellLog, { type ShellLog } from './shell-log';
3839

3940
/**
4041
* The subset of CLI options that is relevant for the shell API's behavior itself.
@@ -159,6 +160,7 @@ export default class ShellInstanceState {
159160
public context: any;
160161
public mongos: Mongo[];
161162
public shellApi: ShellApi;
163+
public shellLog: ShellLog;
162164
public shellBson: ShellBson;
163165
public cliOptions: ShellCliOptions;
164166
public evaluationListener: EvaluationListener;
@@ -187,6 +189,7 @@ export default class ShellInstanceState {
187189
this.initialServiceProvider = initialServiceProvider;
188190
this.messageBus = messageBus;
189191
this.shellApi = new ShellApi(this);
192+
this.shellLog = constructShellLog(this.messageBus);
190193
this.shellBson = constructShellBson(
191194
initialServiceProvider.bsonLibrary,
192195
(msg: string) => {
@@ -362,48 +365,8 @@ export default class ShellInstanceState {
362365
});
363366
}
364367

365-
const bus = this.messageBus;
366-
if (contextObject.log === undefined) {
367-
contextObject.log = {
368-
info(message: string, attr?: unknown) {
369-
bus.emit('mongosh:write-custom-log', {
370-
method: 'info',
371-
message,
372-
attr,
373-
});
374-
},
375-
warn(message: string, attr?: unknown) {
376-
bus.emit('mongosh:write-custom-log', {
377-
method: 'warn',
378-
message,
379-
attr,
380-
});
381-
},
382-
error(message: string, attr?: unknown) {
383-
bus.emit('mongosh:write-custom-log', {
384-
method: 'error',
385-
message,
386-
attr,
387-
});
388-
},
389-
fatal(message: string, attr?: unknown) {
390-
bus.emit('mongosh:write-custom-log', {
391-
method: 'fatal',
392-
message,
393-
attr,
394-
});
395-
},
396-
debug(message: string, attr?: unknown, level?: 1 | 2 | 3 | 4 | 5) {
397-
bus.emit('mongosh:write-custom-log', {
398-
method: 'debug',
399-
message,
400-
attr,
401-
level,
402-
});
403-
},
404-
};
405-
}
406-
bus.emit('mongosh:setCtx', { method: 'setCtx', arguments: {} });
368+
Object.assign(contextObject, this.shellLog);
369+
this.messageBus.emit('mongosh:setCtx', { method: 'setCtx', arguments: {} });
407370
}
408371

409372
get currentServiceProvider(): ServiceProvider {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { MongoshBus } from '@mongosh/types';
2+
3+
export interface ShellLog {
4+
log: {
5+
info: (message: string, attr?: unknown) => void;
6+
warn: (message: string, attr?: unknown) => void;
7+
error: (message: string, attr?: unknown) => void;
8+
fatal: (message: string, attr?: unknown) => void;
9+
debug: (message: string, attr?: unknown, level?: 1 | 2 | 3 | 4 | 5) => void;
10+
};
11+
}
12+
13+
export default function constructShellLog(messageBus: MongoshBus): ShellLog {
14+
return {
15+
log: {
16+
info(message: string, attr?: unknown) {
17+
messageBus.emit('mongosh:write-custom-log', {
18+
method: 'info',
19+
message,
20+
attr,
21+
});
22+
},
23+
warn(message: string, attr?: unknown) {
24+
messageBus.emit('mongosh:write-custom-log', {
25+
method: 'warn',
26+
message,
27+
attr,
28+
});
29+
},
30+
error(message: string, attr?: unknown) {
31+
messageBus.emit('mongosh:write-custom-log', {
32+
method: 'error',
33+
message,
34+
attr,
35+
});
36+
},
37+
fatal(message: string, attr?: unknown) {
38+
messageBus.emit('mongosh:write-custom-log', {
39+
method: 'fatal',
40+
message,
41+
attr,
42+
});
43+
},
44+
debug(message: string, attr?: unknown, level?: 1 | 2 | 3 | 4 | 5) {
45+
messageBus.emit('mongosh:write-custom-log', {
46+
method: 'debug',
47+
message,
48+
attr,
49+
level,
50+
});
51+
},
52+
},
53+
};
54+
}

0 commit comments

Comments
 (0)