Skip to content

Commit

Permalink
feat(cli-repl): use mongodb log format for log files MONGOSH-944 (#1066)
Browse files Browse the repository at this point in the history
Switch the log format to a mongod-style log format, and split out
the implementation parts that could be shared by another application
(specifically, Compass).

This uses log message ids starting from 1000000 to avoid conflicts
with mongod log ids.
  • Loading branch information
addaleax authored Aug 11, 2021
1 parent 94ca4d9 commit fa90217
Show file tree
Hide file tree
Showing 14 changed files with 683 additions and 303 deletions.
108 changes: 27 additions & 81 deletions packages/cli-repl/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/cli-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
"ansi-escape-sequences": "^5.1.2",
"askcharacter": "^1.0.0",
"askpassword": "^1.2.4",
"bson": "^4.4.1",
"is-recoverable-error": "^1.0.2",
"lodash.set": "^4.3.2",
"mongodb-redact": "^0.2.2",
"nanobus": "^4.4.0",
"pino": "^5.16.0",
"pretty-bytes": "^5.3.0",
"pretty-repl": "^3.1.1",
"semver": "^7.1.2",
Expand All @@ -73,7 +73,6 @@
"@types/chai-as-promised": "^7.1.3",
"@types/lodash.set": "^4.3.6",
"@types/node": "^14.14.5",
"@types/pino": "^6.3.3",
"@types/text-table": "^0.2.1",
"@types/yargs-parser": "^15.0.0",
"chai-as-promised": "^7.1.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('CliRepl', () => {
const tmpdir = useTmpdir();

async function log(): Promise<any[]> {
return readReplLogfile(path.join(tmpdir.path, `${cliRepl.logId}_log`));
return readReplLogfile(path.join(tmpdir.path, `${cliRepl.logWriter.logId}_log`));
}

async function startWithExpectedImmediateExit(cliRepl: CliRepl, host: string): Promise<void> {
Expand Down Expand Up @@ -165,22 +165,22 @@ describe('CliRepl', () => {
});

it('writes syntax errors to the log file', async() => {
expect((await log()).filter(entry => entry.stack?.startsWith('SyntaxError:'))).to.have.lengthOf(0);
expect((await log()).filter(entry => entry.attr?.stack?.startsWith('SyntaxError:'))).to.have.lengthOf(0);
input.write('<cat>\n');
await waitBus(cliRepl.bus, 'mongosh:error');
expect((await log()).filter(entry => entry.stack?.startsWith('SyntaxError:'))).to.have.lengthOf(1);
expect((await log()).filter(entry => entry.attr?.stack?.startsWith('SyntaxError:'))).to.have.lengthOf(1);
});

it('writes JS errors to the log file', async() => {
input.write('throw new Error("plain js error")\n');
await waitBus(cliRepl.bus, 'mongosh:error');
expect((await log()).filter(entry => entry.stack?.startsWith('Error: plain js error'))).to.have.lengthOf(1);
expect((await log()).filter(entry => entry.attr?.stack?.startsWith('Error: plain js error'))).to.have.lengthOf(1);
});

it('writes Mongosh errors to the log file', async() => {
input.write('db.auth()\n');
await waitBus(cliRepl.bus, 'mongosh:error');
expect((await log()).filter(entry => entry.stack?.startsWith('MongoshInvalidInputError:'))).to.have.lengthOf(1);
expect((await log()).filter(entry => entry.attr?.stack?.startsWith('MongoshInvalidInputError:'))).to.have.lengthOf(1);
});

it('emits the error event when exit() fails', async() => {
Expand All @@ -191,7 +191,7 @@ describe('CliRepl', () => {
} catch (e) {
const [emitted] = await onerror;
expect(emitted).to.be.instanceOf(MongoshInternalError);
expect((await log()).filter(entry => entry.stack?.startsWith('MongoshInternalError:'))).to.have.lengthOf(1);
expect((await log()).filter(entry => entry.attr?.stack?.startsWith('MongoshInternalError:'))).to.have.lengthOf(1);
return;
}
expect.fail('expected error');
Expand Down
Loading

0 comments on commit fa90217

Please sign in to comment.