Skip to content
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

feat(logging): support logger instances #5941

Merged
merged 2 commits into from
Nov 13, 2024
Merged

Conversation

justinmk3
Copy link
Contributor

Problem

getLogger('foo') returns a global singleton, so the "current topic" only is stored until the next getLogger call.

Solution

Modify getLogger('foo') to return a wrapper which stores its topic. This allows modules to declare their own module-local shared logger:

const logger = getLogger('foo')

License: I confirm that my contribution is made under the terms of the Apache 2.0 license.

@justinmk3 justinmk3 requested a review from a team as a code owner November 6, 2024 22:43

This comment was marked as resolved.

Problem:
`getLogger('foo')` returns a global singleton, so the "current topic"
only is stored until the next `getLogger` call.

Solution:
Modify `getLogger('foo')` to return a wrapper which stores its topic.
This allows modules to declare their own module-local shared logger:

    const logger = getLogger('foo')
@justinmk3 justinmk3 force-pushed the logtopic branch 2 times, most recently from 9925427 to 11f8839 Compare November 13, 2024 23:11
@justinmk3 justinmk3 requested a review from a team as a code owner November 13, 2024 23:11
Comment on lines -199 to +197
)
assert.strictEqual(loggerErrorStub.calledOnce, true)
assert.strictEqual(loggerErrorStub.firstCall.args[0], 'readFileSync: Failed to read file at path %s %O')
assert.strictEqual(loggerErrorStub.firstCall.args[1], fileUri.fsPath)
assert(loggerErrorStub.firstCall.args[2] instanceof Error)
} finally {
loggerErrorStub.restore()
}
await assert.rejects(
async () => await remoteInvokeWebview.promptFile(),
new Error('Failed to read selected file')
)
assertLogsContain('readFileSync: Failed to read file at path %s %O', true, 'error')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test actual state instead of mocks. Saves lots of code, finds actual bugs, and gives meaningful tests instead of tests that break for no reason.


it('logs the provided error, but is wrapped in ToolkitErrors for more context', function () {
// The method is being tested due to its fragile implementation. This test
// protects against changes in the underlying logAndShowError() implementation.

const inputError = new Error('Random Error')

handleWebviewError(inputError, myWebviewId, myCommand)
const err = handleWebviewError(inputError, myWebviewId, myCommand)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the actual state! No mocks needed.

@justinmk3 justinmk3 merged commit c138cb2 into aws:master Nov 13, 2024
23 of 25 checks passed
@justinmk3 justinmk3 deleted the logtopic branch November 13, 2024 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants