-
Notifications
You must be signed in to change notification settings - Fork 736
test(logger): test logger does not format objects as expected #6083
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9ff8a3
fix test logger
Hweinstock b7f15a8
remove added assertion
Hweinstock 09cf562
add link to internal format implementation
Hweinstock 9fc22c4
assert object is properly formatted
Hweinstock 8bea54e
remove entries from debugging
Hweinstock f1a0a8d
Merge branch 'master' into fixTestLogger
Hweinstock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import assert from 'assert' | ||
| import { getLogger } from '../shared' | ||
| import { assertLogsContain, getTestLogger } from './globalSetup.test' | ||
|
|
||
| describe('TestLogger', function () { | ||
| describe('assertLogsContain', function () { | ||
| it('checks only at specified log level', function () { | ||
| const logger = getLogger() | ||
| logger.info('here is some info') | ||
| logger.debug('here is some debug') | ||
|
|
||
| assertLogsContain('here is some info', true, 'info') | ||
| assert.throws(() => assertLogsContain('here is some info', true, 'debug')) | ||
|
|
||
| assertLogsContain('here is some debug', true, 'debug') | ||
| assert.throws(() => assertLogsContain('here is some debug', true, 'info')) | ||
| }) | ||
|
|
||
| it('only requires substring without exactMatch=true', function () { | ||
| const logger = getLogger() | ||
| logger.info('here is some info') | ||
| logger.debug('here is some debug') | ||
|
|
||
| assertLogsContain('some info', false, 'info') | ||
| assertLogsContain('some debug', false, 'debug') | ||
| }) | ||
| }) | ||
|
|
||
| it('formats objects into logs', function () { | ||
| const testObj = { | ||
| info: 'some info', | ||
| } | ||
|
|
||
| getLogger().debug('here is my testObj: %O', testObj) | ||
| assertLogsContain(`here is my testObj: { info: 'some info' }`, true, 'debug') | ||
| }) | ||
|
|
||
| it('has one logging entry for each logging statement', function () { | ||
| const logger = getLogger() | ||
| const startingEntries = getTestLogger().getLoggedEntries().length | ||
| logger.info('here is some info %O', { info: 'this is info' }) | ||
| logger.debug('here is some debug %O', { debug: 'this is debug' }) | ||
| assert.strictEqual(getTestLogger().getLoggedEntries().length - startingEntries, 2) | ||
| }) | ||
|
|
||
| it('returns entry number on each log statement', function () { | ||
| const logger = getLogger() | ||
| const startingEntryNumber = getTestLogger().getLoggedEntries().length | ||
| const entry1 = logger.info('here is some info %O', { info: 'this is info' }) | ||
| const entry2 = logger.debug('here is some debug %O', { debug: 'this is debug' }) | ||
| const entry3 = logger.debug('here is some warn %O', { warn: 'this is warn' }) | ||
| assert.strictEqual(entry1, startingEntryNumber) | ||
| assert.strictEqual(entry2, startingEntryNumber + 1) | ||
| assert.strictEqual(entry3, startingEntryNumber + 2) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,64 +3,48 @@ | |
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { Loggable, Logger, LogLevel } from '../shared/logger' | ||
| import { compareLogLevel } from '../shared/logger/logger' | ||
| import { Loggable, LogLevel } from '../shared/logger' | ||
| import { BaseLogger, compareLogLevel } from '../shared/logger/logger' | ||
| import { Uri } from 'vscode' | ||
| import util from 'util' | ||
| import { isWeb } from '../shared' | ||
| import { inspect } from '../shared/utilities/collectionUtils' | ||
|
|
||
| /** | ||
| * In-memory Logger implementation suitable for use by tests. | ||
| */ | ||
| export class TestLogger implements Logger { | ||
| export class TestLogger extends BaseLogger { | ||
|
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. Nice, was just thinking about this! |
||
| private readonly loggedEntries: { | ||
| logLevel: LogLevel | ||
| entry: Loggable | ||
| }[] = [] | ||
| private count: number = 0 | ||
| public constructor(private logLevel: LogLevel = 'debug') {} | ||
| logFile?: Uri | undefined | ||
|
|
||
| public enableDebugConsole(): void {} | ||
|
|
||
| public log(logLevel: LogLevel, ...message: Loggable[]): number { | ||
| return this.addLoggedEntries(logLevel, message) | ||
| } | ||
|
|
||
| public debug(...message: Loggable[]): number { | ||
| return this.addLoggedEntries('debug', message) | ||
| } | ||
|
|
||
| public verbose(...message: Loggable[]): number { | ||
| return this.addLoggedEntries('verbose', message) | ||
| public constructor(private logLevel: LogLevel = 'debug') { | ||
| super() | ||
| } | ||
|
|
||
| public info(...message: Loggable[]): number { | ||
| return this.addLoggedEntries('info', message) | ||
| } | ||
|
|
||
| public warn(...message: Loggable[]): number { | ||
| return this.addLoggedEntries('warn', message) | ||
| } | ||
|
|
||
| public error(...message: Loggable[]): number { | ||
| return this.addLoggedEntries('error', message) | ||
| } | ||
| public enableDebugConsole(): void {} | ||
|
|
||
| public getLoggedEntries(...logLevels: LogLevel[]): Loggable[] { | ||
| return this.loggedEntries | ||
| .filter((loggedEntry) => logLevels.length === 0 || logLevels.includes(loggedEntry.logLevel)) | ||
| .map((loggedEntry) => loggedEntry.entry) | ||
| } | ||
|
|
||
| public sendToLog(logLevel: LogLevel, msg: string, ...entries: Loggable[]): number { | ||
| return this.addLoggedEntries(logLevel, [msg, ...entries]) | ||
| public sendToLog(logLevel: LogLevel, msg: string, ...meta: any[]): number { | ||
| return this.addLoggedEntries(logLevel, msg, ...meta) | ||
| } | ||
|
|
||
| private formatString(message: string, ...meta: any[]): string { | ||
| // Want to avoid reimplementing nodes `format` so instead concat to end of string | ||
| // Node's format implementation: https://github.com/nodejs/node/blob/3178a762d6a2b1a37b74f02266eea0f3d86603be/lib/internal/util/inspect.js#L2191-L2315 | ||
| return isWeb() ? [message, meta.map((s) => inspect(s))].join(' ') : util.format(message, ...meta) | ||
| } | ||
|
|
||
| private addLoggedEntries(logLevel: LogLevel, entries: Loggable[]): number { | ||
| entries.forEach((entry) => { | ||
| this.loggedEntries.push({ | ||
| logLevel, | ||
| entry, | ||
| }) | ||
| private addLoggedEntries(logLevel: LogLevel, message: Loggable, ...meta: any[]): number { | ||
| this.loggedEntries.push({ | ||
| logLevel, | ||
| entry: typeof message === 'string' && meta.length > 0 ? this.formatString(message, ...meta) : message, | ||
| }) | ||
|
|
||
| return this.count++ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
when passing some objects through
util.formatI noticed it set a property_formatted(perhaps as a cache?), so without these changes the test fails. Also avoids some small code dupe.