Skip to content

Commit 15ee778

Browse files
committed
refactor: move getPath to the log object
1 parent 9baf8c1 commit 15ee778

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export type MongoshIOProvider = Omit<
7878
): Promise<{ contents: string; absolutePath: string }>;
7979
getCryptLibraryOptions(): Promise<AutoEncryptionOptions['extraOptions']>;
8080
bugReportErrorMessageInfo?(): string | undefined;
81+
getLogPath(): string | undefined;
8182
};
8283

8384
export type JSContext = 'repl' | 'plain-vm';

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,13 @@ describe('e2e', function () {
19841984
});
19851985
});
19861986

1987+
it('gets a path to the current log file', async function () {
1988+
await shell.executeLine('log.getPath()');
1989+
expect(shell.assertNoErrors());
1990+
const logPath = path.join(logBasePath, `${shell.logId}_log`);
1991+
expect(shell.output).to.include(logPath);
1992+
});
1993+
19871994
it('writes custom log directly', async function () {
19881995
await shell.executeLine("log.info('This is a custom entry')");
19891996
expect(shell.assertNoErrors());

packages/i18n/src/locales/en_US.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ const translations: Catalog = {
137137
description: 'Shell log methods',
138138
link: '#',
139139
attributes: {
140+
getPath: {
141+
description: 'Gets a path to the current log file',
142+
example: 'log.getPath()',
143+
},
140144
info: {
141145
description: 'Writes a custom info message to the log file',
142146
example: 'log.info("Custom info message")',
@@ -167,7 +171,8 @@ const translations: Catalog = {
167171
attributes: {
168172
log: {
169173
description:
170-
"'log.info(<msg>)': Write a custom info/warn/error/fatal/debug message to the log file",
174+
"'log.info(<msg>)': Write a custom info/warn/error/fatal/debug message to the log file\n" +
175+
"'log.getPath()': Gets a path to the current log file\n",
171176
},
172177
use: {
173178
description: 'Set current database',

packages/node-runtime-worker-thread/src/worker-runtime.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,15 @@ describe('worker-runtime', function () {
626626
});
627627

628628
describe('getLogPath', function () {
629-
it('should be called when shell evaluates `getLogPath()`', async function () {
629+
it('should be called when shell evaluates `log.getPath()`', async function () {
630630
const { init, evaluate } = caller;
631631
const evalListener = createSpiedEvaluationListener();
632632

633633
exposed = exposeAll(evalListener, worker);
634634

635635
await init('mongodb://nodb/', dummyOptions, { nodb: true });
636636

637-
await evaluate('getLogPath()');
637+
await evaluate('log.getPath()');
638638
expect(evalListener.getLogPath).to.have.been.called;
639639
});
640640
});

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,6 @@ export default class ShellApi extends ShellApiClass {
366366
}
367367
}
368368

369-
@platforms(['CLI'])
370-
getLogPath(): string | undefined {
371-
try {
372-
return this._instanceState.evaluationListener.getLogPath?.();
373-
} catch (err: unknown) {
374-
return String(err);
375-
}
376-
}
377-
378369
@returnsPromise
379370
@platforms(['CLI'])
380371
async passwordPrompt(): Promise<string> {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ export interface EvaluationListener
117117
* options used to access it.
118118
*/
119119
getCryptLibraryOptions?: () => Promise<AutoEncryptionOptions['extraOptions']>;
120+
121+
/**
122+
* Called when log.getPath() is used in the shell.
123+
*/
124+
getLogPath?: () => string | undefined;
120125
}
121126

122127
/**

packages/shell-api/src/shell-log.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,42 @@ export class ShellLog extends ShellApiClass {
2121
this[instanceStateSymbol] = instanceState;
2222
}
2323

24+
getPath(): string | undefined {
25+
return this._instanceState.evaluationListener.getLogPath?.();
26+
}
27+
2428
info(message: string, attr?: unknown) {
2529
this[instanceStateSymbol].messageBus.emit('mongosh:write-custom-log', {
2630
method: 'info',
2731
message,
2832
attr,
2933
});
3034
}
35+
3136
warn(message: string, attr?: unknown) {
3237
this[instanceStateSymbol].messageBus.emit('mongosh:write-custom-log', {
3338
method: 'warn',
3439
message,
3540
attr,
3641
});
3742
}
43+
3844
error(message: string, attr?: unknown) {
3945
this[instanceStateSymbol].messageBus.emit('mongosh:write-custom-log', {
4046
method: 'error',
4147
message,
4248
attr,
4349
});
4450
}
51+
4552
fatal(message: string, attr?: unknown) {
4653
this[instanceStateSymbol].messageBus.emit('mongosh:write-custom-log', {
4754
method: 'fatal',
4855
message,
4956
attr,
5057
});
5158
}
59+
5260
debug(message: string, attr?: unknown, level?: 1 | 2 | 3 | 4 | 5) {
5361
this[instanceStateSymbol].messageBus.emit('mongosh:write-custom-log', {
5462
method: 'debug',

packages/types/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ export interface ConfigProvider<T> {
630630
value: T[K]
631631
): Promise<string | null>;
632632
listConfigOptions(): string[] | undefined | Promise<string[]>;
633-
getLogPath(): string | undefined;
634633
}
635634

636635
function isValidUrl(url: string): boolean {

0 commit comments

Comments
 (0)