-
Notifications
You must be signed in to change notification settings - Fork 85
fix(logging): fix log flushing on exit MONGOSH-2883 #2547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -323,6 +323,18 @@ export class CliRepl implements MongoshIOProvider { | |
| async start( | ||
| driverUri: string, | ||
| driverOptions: DevtoolsConnectOptions | ||
| ): Promise<void> { | ||
| try { | ||
| return await this._start(driverUri, driverOptions); | ||
| } catch (err) { | ||
| await this.close(); | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| private async _start( | ||
| driverUri: string, | ||
| driverOptions: DevtoolsConnectOptions | ||
| ): Promise<void> { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const { version }: { version: string } = require('../package.json'); | ||
|
|
@@ -659,7 +671,7 @@ export class CliRepl implements MongoshIOProvider { | |
| if (enabled) { | ||
| await this.startLogging(); | ||
| } else { | ||
| this.loggingAndTelemetry?.detachLogger(); | ||
| await this.loggingAndTelemetry?.detachLogger(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for future, we should probably figure out why we don't have a lint rule for this
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gagik We do 🙂 This is because I changed the type of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -1172,7 +1184,7 @@ export class CliRepl implements MongoshIOProvider { | |
| const analytics = this.toggleableAnalytics; | ||
| let flushError: string | null = null; | ||
| let flushDuration: number | null = null; | ||
| this.loggingAndTelemetry?.flush(); | ||
| await this.loggingAndTelemetry?.flush(); | ||
|
|
||
| if (analytics) { | ||
| const flushStart = Date.now(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2142,12 +2142,7 @@ describe('e2e', function () { | |||||||||||||||||||||
| await shell.executeLine("log.info('This is a custom entry')"); | ||||||||||||||||||||||
| expect(shell.assertNoErrors()); | ||||||||||||||||||||||
| await eventually(async () => { | ||||||||||||||||||||||
| const log = await readLogFile< | ||||||||||||||||||||||
| MongoLogEntryFromFile & { | ||||||||||||||||||||||
| c: string; | ||||||||||||||||||||||
| ctx: string; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| >(); | ||||||||||||||||||||||
| const log = await readLogFile<MongoLogEntryFromFile>(); | ||||||||||||||||||||||
| const customLogEntry = log.filter((logEntry) => | ||||||||||||||||||||||
| logEntry.msg.includes('This is a custom entry') | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
|
@@ -2182,6 +2177,46 @@ describe('e2e', function () { | |||||||||||||||||||||
| ).to.have.lengthOf(1); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('flushes log file (normal exit)', async function () { | ||||||||||||||||||||||
| const shell = this.startTestShell({ | ||||||||||||||||||||||
| args: [ | ||||||||||||||||||||||
| '--nodb', | ||||||||||||||||||||||
| '--eval', | ||||||||||||||||||||||
| 'for(let i=0; i < 10; i++) log.info("logging", {i}); log.getPath();', | ||||||||||||||||||||||
| ], | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| expect(await shell.waitForAnyExit()).to.equal(0); | ||||||||||||||||||||||
| const log = await readReplLogFile(shell.output.trim()); | ||||||||||||||||||||||
| for (let i = 0; i < 10; i++) { | ||||||||||||||||||||||
| expect( | ||||||||||||||||||||||
| log.some( | ||||||||||||||||||||||
| (entry) => entry.msg === 'logging' && entry.attr?.i === i | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| ).to.be.true; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('flushes log file (exception thrown)', async function () { | ||||||||||||||||||||||
| const shell = this.startTestShell({ | ||||||||||||||||||||||
| args: [ | ||||||||||||||||||||||
| '--nodb', | ||||||||||||||||||||||
| '--eval', | ||||||||||||||||||||||
| 'for(let i=0; i < 10; i++) log.info("logging", {i}); print(log.getPath()); throw new Error("uh oh")', | ||||||||||||||||||||||
| ], | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| expect(await shell.waitForAnyExit()).to.equal(1); | ||||||||||||||||||||||
| const log = await readReplLogFile( | ||||||||||||||||||||||
| shell.output.replace(/Error: uh oh/g, '').trim() | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
Comment on lines
+2209
to
+2211
|
||||||||||||||||||||||
| const log = await readReplLogFile( | |
| shell.output.replace(/Error: uh oh/g, '').trim() | |
| ); | |
| // Extract the log path from the shell output by finding the line that looks like a log file path | |
| const logPathLine = shell.output | |
| .split('\n') | |
| .map(line => line.trim()) | |
| .find(line => line.length > 0 && /\.log$/.test(line)); | |
| expect(logPathLine, 'Could not find log path in shell output').to.be.a('string'); | |
| const log = await readReplLogFile(logPathLine); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, I had similar thoughts here as well, although I was ultimately feeling like this is just fine for a test
Uh oh!
There was an error while loading. Please reload this page.