-
Notifications
You must be signed in to change notification settings - Fork 19
feat: introduce logger #367
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
21 commits
Select commit
Hold shift + click to select a range
81aae59
feat: create logger module
araujogui 1da6ac1
fix: correct return type
araujogui 5c8cef5
feat: remove console calls
araujogui f5bf533
feat: replace linter reporters to transports
araujogui 40cf9f5
feat: change default logger level
araujogui ee2f830
fix: init logger on list command
araujogui b29a9a3
feat: add array logging
araujogui 327f0ae
test: fix logger init
araujogui 7bee26b
chore: remove .tool-versions
araujogui fd3039f
fix: missing logger init on generate command
araujogui 4ed8c89
fix: github actions fn mapping
araujogui 1bb3b00
fix: use utc timezone
araujogui 086b951
test: remove it.only
araujogui a3a10b5
refactor: use default exports
araujogui fc29c54
refactor: remove namespace gh import
araujogui 806803d
refactor: remove @ts-check
araujogui 1df5c54
refactor: remove module check
araujogui c9d92a5
test: fix imports
araujogui 7830dc7
refactor: use datetimeformat
araujogui 2e7f020
refactor: add default logger
araujogui c29dd1a
refactor: delete singleton
araujogui 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { deepStrictEqual, strictEqual } from 'node:assert'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { LogLevel } from '../constants.mjs'; | ||
| import { createLogger } from '../logger.mjs'; | ||
|
|
||
| /** | ||
| * @type {import('../types').Metadata} | ||
| */ | ||
| const metadata = { | ||
| file: { | ||
| path: 'test.md', | ||
| position: { | ||
| start: { line: 1 }, | ||
| end: { line: 1 }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| describe('createLogger', () => { | ||
| describe('DEBUG', () => { | ||
| it('should log DEBUG messages when logger level is set to DEBUG', t => { | ||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
|
|
||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, LogLevel.debug); | ||
|
|
||
| logger.debug('Hello, World!', metadata); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 1); | ||
|
|
||
| const call = transport.mock.calls[0]; | ||
| deepStrictEqual(call.arguments, [ | ||
| { | ||
| level: LogLevel.debug, | ||
| message: 'Hello, World!', | ||
| metadata, | ||
| module: undefined, | ||
| timestamp: 0, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should filter DEBUG messages when logger level is set to INFO or higher', t => { | ||
| [LogLevel.info, LogLevel.warn, LogLevel.error, LogLevel.fatal].forEach( | ||
| loggerLevel => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, loggerLevel); | ||
|
|
||
| logger.debug('Hello, World!'); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 0); | ||
| } | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('INFO', () => { | ||
| it('should log INFO messages when logger level is set to INFO or lower', t => { | ||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
| [LogLevel.info, LogLevel.debug].forEach(loggerLevel => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, loggerLevel); | ||
|
|
||
| logger.info('Hello, World!', metadata); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 1); | ||
|
|
||
| const call = transport.mock.calls[0]; | ||
| deepStrictEqual(call.arguments, [ | ||
| { | ||
| level: LogLevel.info, | ||
| message: 'Hello, World!', | ||
| metadata, | ||
| module: undefined, | ||
| timestamp: 0, | ||
| }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| it('should filter INFO messages when logger level is set to WARN or higher', t => { | ||
| [LogLevel.warn, LogLevel.error, LogLevel.fatal].forEach(loggerLevel => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, loggerLevel); | ||
|
|
||
| logger.info('Hello, World!'); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('WARN', () => { | ||
| it('should log WARN messages when logger level is set to WARN or lower', t => { | ||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
|
|
||
| [LogLevel.warn, LogLevel.info, LogLevel.debug].forEach(loggerLevel => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, loggerLevel); | ||
|
|
||
| logger.warn('Hello, World!', metadata); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 1); | ||
|
|
||
| const call = transport.mock.calls[0]; | ||
| deepStrictEqual(call.arguments, [ | ||
| { | ||
| level: LogLevel.warn, | ||
| message: 'Hello, World!', | ||
| metadata, | ||
| module: undefined, | ||
| timestamp: 0, | ||
| }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| it('should filter WARN messages when logger level is set to ERROR or higher', t => { | ||
| [LogLevel.error, LogLevel.fatal].forEach(loggerLevel => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, loggerLevel); | ||
|
|
||
| logger.warn('Hello, World!'); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERROR', () => { | ||
| it('should log ERROR messages when logger level is set to ERROR or lower', t => { | ||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
|
|
||
| [LogLevel.error, LogLevel.warn, LogLevel.info, LogLevel.debug].forEach( | ||
| loggerLevel => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, loggerLevel); | ||
|
|
||
| logger.error('Hello, World!', metadata); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 1); | ||
|
|
||
| const call = transport.mock.calls[0]; | ||
| deepStrictEqual(call.arguments, [ | ||
| { | ||
| level: LogLevel.error, | ||
| message: 'Hello, World!', | ||
| metadata, | ||
| module: undefined, | ||
| timestamp: 0, | ||
| }, | ||
| ]); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| it('should filter ERROR messages when logger level is set to FATAL', t => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, LogLevel.fatal); | ||
|
|
||
| logger.warn('Hello, World!'); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 0); | ||
| }); | ||
| }); | ||
|
|
||
| it('should filter all messages when minimum level is set above FATAL', t => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| // silent logs | ||
| const logger = createLogger(transport, 100); | ||
|
|
||
| Object.keys(LogLevel).forEach(level => { | ||
| logger[level]('Hello, World!'); | ||
| }); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 0); | ||
| }); | ||
|
|
||
| it('should log all messages if message is a string array', t => { | ||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, LogLevel.info); | ||
|
|
||
| logger.info(['Hello, 1!', 'Hello, 2!', 'Hello, 3!']); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 3); | ||
| }); | ||
|
|
||
| it('should log error message', t => { | ||
| t.mock.timers.enable({ apis: ['Date'] }); | ||
|
|
||
| const transport = t.mock.fn(); | ||
|
|
||
| const logger = createLogger(transport, LogLevel.error); | ||
|
|
||
| logger.error(new Error('Hello, World!')); | ||
|
|
||
| strictEqual(transport.mock.callCount(), 1); | ||
|
|
||
| const call = transport.mock.calls[0]; | ||
| deepStrictEqual(call.arguments, [ | ||
| { | ||
| level: LogLevel.error, | ||
| message: 'Hello, World!', | ||
| metadata: {}, | ||
| module: undefined, | ||
| timestamp: 0, | ||
| }, | ||
| ]); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.