-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add logger class util in @metamask/snap-networks-utils #136
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,178 @@ | ||
| import { Logger, LogLevel } from './Logger'; | ||
|
|
||
| const loggerMethodConfigurations = [ | ||
| { method: 'log', consoleMethod: 'info', filteredAt: LogLevel.WARN }, | ||
| { method: 'info', consoleMethod: 'info', filteredAt: LogLevel.WARN }, | ||
| { method: 'warn', consoleMethod: 'warn', filteredAt: LogLevel.ERROR }, | ||
| { method: 'error', consoleMethod: 'error', filteredAt: LogLevel.SILENT }, | ||
| { method: 'debug', consoleMethod: 'debug', filteredAt: LogLevel.INFO }, | ||
| { method: 'trace', consoleMethod: 'trace', filteredAt: LogLevel.DEBUG }, | ||
| ] as const; | ||
|
|
||
| type MockConsole = Record< | ||
| (typeof loggerMethodConfigurations)[number]['consoleMethod'], | ||
| jest.SpyInstance | ||
| >; | ||
|
|
||
| type SetupTestResult = { | ||
| loggerMethods: typeof loggerMethodConfigurations; | ||
| mockConsole: MockConsole; | ||
| }; | ||
|
|
||
| const setupTest = (): SetupTestResult => { | ||
| jest.restoreAllMocks(); | ||
|
|
||
| return { | ||
| loggerMethods: loggerMethodConfigurations, | ||
| mockConsole: { | ||
| debug: jest.spyOn(console, 'debug').mockImplementation(), | ||
| error: jest.spyOn(console, 'error').mockImplementation(), | ||
| info: jest.spyOn(console, 'info').mockImplementation(), | ||
| trace: jest.spyOn(console, 'trace').mockImplementation(), | ||
| warn: jest.spyOn(console, 'warn').mockImplementation(), | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| describe('Logger', () => { | ||
| it('forwards calls to the matching console method', () => { | ||
| const { loggerMethods, mockConsole } = setupTest(); | ||
|
|
||
| const logger = new Logger({ level: LogLevel.TRACE }); | ||
|
|
||
| for (const { method, consoleMethod } of loggerMethods) { | ||
| logger[method]('hello', 42); | ||
|
|
||
| expect(mockConsole[consoleMethod]).toHaveBeenCalledWith('hello', 42); | ||
| } | ||
| }); | ||
|
|
||
| it('prefixes messages with a derived logger', () => { | ||
| const { mockConsole } = setupTest(); | ||
|
|
||
| const logger = new Logger({ level: LogLevel.TRACE }); | ||
|
|
||
| const prefixed = logger.withPrefix('[snap-networks-utils]'); | ||
|
|
||
| prefixed.info('a'); | ||
| prefixed.warn('b'); | ||
| prefixed.error('c'); | ||
| prefixed.debug('d'); | ||
| prefixed.trace('e'); | ||
|
|
||
| expect(mockConsole.info).toHaveBeenCalledWith('[snap-networks-utils]', 'a'); | ||
| expect(mockConsole.warn).toHaveBeenCalledWith('[snap-networks-utils]', 'b'); | ||
| expect(mockConsole.error).toHaveBeenCalledWith( | ||
| '[snap-networks-utils]', | ||
| 'c', | ||
| ); | ||
| expect(mockConsole.debug).toHaveBeenCalledWith( | ||
| '[snap-networks-utils]', | ||
| 'd', | ||
| ); | ||
| expect(mockConsole.trace).toHaveBeenCalledWith( | ||
| '[snap-networks-utils]', | ||
| 'e', | ||
| ); | ||
| }); | ||
|
|
||
| it('combines prefixes from derived loggers', () => { | ||
| const { mockConsole } = setupTest(); | ||
|
|
||
| const logger = new Logger({ level: LogLevel.TRACE }); | ||
|
|
||
| const parentLogger = logger.withPrefix('[parent]'); | ||
| const childLogger = parentLogger.withPrefix('[child]'); | ||
|
|
||
| childLogger.info('message'); | ||
|
|
||
| expect(mockConsole.info).toHaveBeenCalledWith( | ||
| '[parent] [child]', | ||
| 'message', | ||
| ); | ||
| }); | ||
|
|
||
| it('logs trace messages at the trace level', () => { | ||
| const { mockConsole } = setupTest(); | ||
|
|
||
| const logger = new Logger({ level: LogLevel.TRACE }); | ||
|
|
||
| logger.trace('trace'); | ||
|
|
||
| expect(mockConsole.trace).toHaveBeenCalledWith('trace'); | ||
| }); | ||
|
|
||
| it('throws when given an invalid log level', () => { | ||
| expect(() => new Logger({ level: '' as LogLevel })).toThrow( | ||
| 'Expected one of `"silent","error","warn","info","debug","trace"`, but received: ""', | ||
| ); | ||
| expect(() => new Logger({ level: 'verbose' as LogLevel })).toThrow( | ||
| 'Expected one of `"silent","error","warn","info","debug","trace"`, but received: "verbose"', | ||
| ); | ||
| }); | ||
|
|
||
| it('runs decorators through the configured output', () => { | ||
| const decorator = jest.fn((next: (...args: unknown[]) => void) => { | ||
| next('decoded error'); | ||
| }); | ||
| const { mockConsole } = setupTest(); | ||
|
|
||
| const baseLogger = new Logger({ | ||
| level: LogLevel.TRACE, | ||
| decorators: { error: decorator }, | ||
| }); | ||
|
|
||
| const logger = baseLogger.withPrefix('[Solana]'); | ||
|
|
||
| logger.error('original error'); | ||
|
|
||
| expect(decorator).toHaveBeenCalledWith( | ||
| expect.any(Function), | ||
| 'original error', | ||
| ); | ||
| expect(mockConsole.error).toHaveBeenCalledWith('[Solana]', 'decoded error'); | ||
| }); | ||
|
|
||
| it('does not run decorators at the silent level', () => { | ||
| const decorator = jest.fn(); | ||
| const { mockConsole } = setupTest(); | ||
|
|
||
| const logger = new Logger({ | ||
| level: LogLevel.SILENT, | ||
| decorators: { error: decorator }, | ||
| }); | ||
|
|
||
| logger.error('silent'); | ||
|
|
||
| expect(decorator).not.toHaveBeenCalled(); | ||
| expect(mockConsole.error).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not log calls at the silent level', () => { | ||
| const { loggerMethods, mockConsole } = setupTest(); | ||
|
|
||
| const logger = new Logger({ | ||
| level: LogLevel.SILENT, | ||
| }); | ||
|
|
||
| for (const { method, consoleMethod } of loggerMethods) { | ||
| logger[method]('silent'); | ||
|
|
||
| expect(mockConsole[consoleMethod]).not.toHaveBeenCalled(); | ||
| } | ||
| }); | ||
|
|
||
| it('filters messages above the configured level', () => { | ||
| const { loggerMethods, mockConsole } = setupTest(); | ||
|
|
||
| for (const { method, consoleMethod, filteredAt } of loggerMethods) { | ||
| const logger = new Logger({ | ||
| level: filteredAt, | ||
| }); | ||
|
|
||
| logger[method]('filtered'); | ||
|
|
||
| expect(mockConsole[consoleMethod]).not.toHaveBeenCalled(); | ||
| } | ||
| }); | ||
| }); |
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.
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 believe you are going to have to export it here for projects to be able to use it
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'm not sure. I was going to split this up to make it easier to review.