-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: revamp logger and set logger level from snap build (#342)
* chore: update logger to deine from build * chore: update default log level config * chore: update logger test * chore: fix test
- Loading branch information
1 parent
8f50a33
commit 98c0224
Showing
20 changed files
with
206 additions
and
212 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,10 @@ | ||
import { LogLevel } from './utils'; | ||
|
||
export type SnapConfig = { | ||
logLevel: string; | ||
}; | ||
|
||
export const Config: SnapConfig = { | ||
// eslint-disable-next-line no-restricted-globals | ||
logLevel: process.env.LOG_LEVEL ?? LogLevel.OFF.valueOf().toString(), | ||
}; |
This file contains 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 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 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 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 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 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,146 @@ | ||
import { logger, LogLevel } from './logger'; | ||
|
||
describe('Logger', () => { | ||
afterAll(() => { | ||
logger.logLevel = LogLevel.OFF; | ||
}); | ||
const createLogSpy = () => { | ||
return { | ||
log: jest.spyOn(console, 'log').mockReturnThis(), | ||
error: jest.spyOn(console, 'error').mockReturnThis(), | ||
warn: jest.spyOn(console, 'warn').mockReturnThis(), | ||
info: jest.spyOn(console, 'info').mockReturnThis(), | ||
trace: jest.spyOn(console, 'trace').mockReturnThis(), | ||
debug: jest.spyOn(console, 'debug').mockReturnThis(), | ||
}; | ||
}; | ||
|
||
const testLog = (message: string) => { | ||
logger.log(message); | ||
logger.error(message); | ||
logger.warn(message); | ||
logger.info(message); | ||
logger.trace(message); | ||
logger.debug(message); | ||
}; | ||
|
||
it.each([ | ||
{ | ||
logLevel: LogLevel.ALL, | ||
expected: { | ||
info: true, | ||
warn: true, | ||
error: true, | ||
debug: true, | ||
log: true, | ||
trace: true, | ||
}, | ||
}, | ||
{ | ||
logLevel: LogLevel.OFF, | ||
expected: { | ||
info: false, | ||
warn: false, | ||
error: false, | ||
debug: false, | ||
log: false, | ||
trace: false, | ||
}, | ||
}, | ||
{ | ||
logLevel: LogLevel.INFO, | ||
expected: { | ||
info: true, | ||
warn: true, | ||
error: true, | ||
debug: false, | ||
log: false, | ||
trace: false, | ||
}, | ||
}, | ||
{ | ||
logLevel: LogLevel.ERROR, | ||
expected: { | ||
info: false, | ||
warn: false, | ||
error: true, | ||
debug: false, | ||
log: false, | ||
trace: false, | ||
}, | ||
}, | ||
{ | ||
logLevel: LogLevel.WARN, | ||
expected: { | ||
info: false, | ||
warn: true, | ||
error: true, | ||
debug: false, | ||
log: false, | ||
trace: false, | ||
}, | ||
}, | ||
{ | ||
logLevel: LogLevel.DEBUG, | ||
expected: { | ||
info: true, | ||
warn: true, | ||
error: true, | ||
debug: true, | ||
log: false, | ||
trace: false, | ||
}, | ||
}, | ||
{ | ||
logLevel: LogLevel.TRACE, | ||
expected: { | ||
info: true, | ||
warn: true, | ||
error: true, | ||
debug: true, | ||
log: false, | ||
trace: true, | ||
}, | ||
}, | ||
])( | ||
'logs correctly when `logLevel` is `$logLevel`', | ||
({ | ||
logLevel, | ||
expected, | ||
}: { | ||
logLevel: LogLevel; | ||
expected: { | ||
info: boolean; | ||
warn: boolean; | ||
error: boolean; | ||
debug: boolean; | ||
log: boolean; | ||
trace: boolean; | ||
}; | ||
}) => { | ||
const spys = createLogSpy(); | ||
logger.logLevel = logLevel; | ||
const logMsg = 'log'; | ||
|
||
testLog(logMsg); | ||
|
||
Object.entries(expected) | ||
.filter(([_, value]) => !value) | ||
.forEach(([key]) => { | ||
expect(spys[key]).not.toHaveBeenCalled(); | ||
}); | ||
|
||
Object.entries(expected) | ||
.filter(([_, value]) => value) | ||
.forEach(([key]) => { | ||
expect(spys[key]).toHaveBeenCalledWith(logMsg); | ||
}); | ||
}, | ||
); | ||
|
||
it('return correct `LogLevel`', () => { | ||
logger.logLevel = LogLevel.INFO; | ||
|
||
expect(logger.logLevel).toStrictEqual(LogLevel.INFO); | ||
}); | ||
}); |
Oops, something went wrong.