Skip to content

Commit 33f85ff

Browse files
feat(logging): print help for the global log property MONGOSH-1995 (#2363)
* feat(logging): Print help for the global log property MONGOSH-1995 * Update packages/i18n/src/locales/en_US.ts Co-authored-by: Anna Henningsen <anna.henningsen@mongodb.com> --------- Co-authored-by: Anna Henningsen <anna.henningsen@mongodb.com>
1 parent f3f10e4 commit 33f85ff

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

packages/i18n/src/locales/en_US.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ const translations: Catalog = {
137137
description: 'Shell log methods',
138138
link: '#',
139139
attributes: {
140-
// TODO(MONGOSH-1995): Print help for the global log property.
141140
info: {
142141
description: 'Writes a custom info message to the log file',
143142
example: 'log.info("Custom info message")',
@@ -166,6 +165,10 @@ const translations: Catalog = {
166165
description: 'Shell Help',
167166
link: 'https://docs.mongodb.com/manual/reference/method',
168167
attributes: {
168+
log: {
169+
description:
170+
"'log.info(<msg>)': Write a custom info/warn/error/fatal/debug message to the log file",
171+
},
169172
use: {
170173
description: 'Set current database',
171174
},
@@ -181,13 +184,13 @@ const translations: Catalog = {
181184
},
182185
show: {
183186
description:
184-
"'show databases'/'show dbs': Print a list of all available databases.\n" +
185-
"'show collections'/'show tables': Print a list of all collections for current database.\n" +
186-
"'show profile': Prints system.profile information.\n" +
187-
"'show users': Print a list of all users for current database.\n" +
188-
"'show roles': Print a list of all roles for current database.\n" +
187+
"'show databases'/'show dbs': Print a list of all available databases\n" +
188+
"'show collections'/'show tables': Print a list of all collections for current database\n" +
189+
"'show profile': Prints system.profile information\n" +
190+
"'show users': Print a list of all users for current database\n" +
191+
"'show roles': Print a list of all roles for current database\n" +
189192
"'show log <type>': log for current connection, if type is not set uses 'global'\n" +
190-
"'show logs': Print all logs.\n",
193+
"'show logs': Print all logs",
191194
},
192195
connect: {
193196
description:

packages/shell-api/src/decorators.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,22 @@ function shellApiClassGeneric<T extends { prototype: any }>(
473473
constructor.prototype,
474474
propertyName
475475
);
476+
477+
if (toIgnore.includes(propertyName) || propertyName.startsWith('_')) {
478+
continue;
479+
}
480+
476481
const isMethod =
477482
descriptor?.value && typeof descriptor.value === 'function';
478-
if (
479-
!isMethod ||
480-
toIgnore.includes(propertyName) ||
481-
propertyName.startsWith('_')
482-
)
483+
if (!isMethod) {
484+
const attributeHelpKeyPrefix = `${classHelpKeyPrefix}.attributes.${propertyName}`;
485+
classHelp.attr.push({
486+
name: propertyName,
487+
description: `${attributeHelpKeyPrefix}.description`,
488+
});
483489
continue;
490+
}
491+
484492
let method: any = (descriptor as any).value;
485493

486494
if ((constructor as any)[addSourceToResultsSymbol]) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { dirname } from 'path';
3636
import { ShellUserConfig } from '@mongosh/types';
3737
import i18n from '@mongosh/i18n';
3838
import { MONGOSH_VERSION } from './mongosh-version';
39+
import type { ShellLog } from './shell-log';
3940

4041
const instanceStateSymbol = Symbol.for('@@mongosh.instanceState');
4142
const loadCallNestingLevelSymbol = Symbol.for('@@mongosh.loadCallNestingLevel');
@@ -188,6 +189,10 @@ export default class ShellApi extends ShellApiClass {
188189
this.config = new ShellConfig(instanceState);
189190
}
190191

192+
get log(): ShellLog {
193+
return this[instanceStateSymbol].shellLog;
194+
}
195+
191196
get _instanceState(): ShellInstanceState {
192197
return this[instanceStateSymbol];
193198
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export default class ShellInstanceState {
316316
setCtx(contextObject: any): void {
317317
this.context = contextObject;
318318
Object.assign(contextObject, this.shellApi);
319+
contextObject.log = this.shellLog;
319320
for (const name of Object.getOwnPropertyNames(ShellApi.prototype)) {
320321
const { shellApi } = this;
321322
if (
@@ -365,8 +366,6 @@ export default class ShellInstanceState {
365366
});
366367
}
367368

368-
contextObject.log = this.shellLog;
369-
370369
this.messageBus.emit('mongosh:setCtx', { method: 'setCtx', arguments: {} });
371370
}
372371

0 commit comments

Comments
 (0)