2022-11-27 16:14:56 +01:00
|
|
|
import { Logger, LogLevel } from '../../../src/common/logger'
|
2021-11-28 19:40:16 +01:00
|
|
|
|
|
|
|
|
describe('Logger', () => {
|
2022-11-27 16:09:30 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.resetAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
2021-11-28 19:40:16 +01:00
|
|
|
test('should log messages', () => {
|
|
|
|
|
const consoleLogSpy = jest.fn()
|
|
|
|
|
const consoleErrorSpy = jest.fn()
|
|
|
|
|
jest.spyOn(console, 'log').mockImplementation(consoleLogSpy)
|
|
|
|
|
jest.spyOn(console, 'error').mockImplementation(consoleErrorSpy)
|
|
|
|
|
|
2022-11-30 11:56:39 +01:00
|
|
|
const logger = new Logger('tag', 'abc', LogLevel.DEBUG)
|
|
|
|
|
logger.debug('first')
|
|
|
|
|
logger.info('second')
|
|
|
|
|
logger.error('third', { errorMessage: 'test' })
|
2021-11-28 19:40:16 +01:00
|
|
|
|
|
|
|
|
expect(consoleLogSpy).toHaveBeenCalledTimes(2)
|
|
|
|
|
expect(consoleLogSpy).toHaveBeenNthCalledWith(
|
2022-10-23 11:55:47 +02:00
|
|
|
1,
|
|
|
|
|
'<%s> [%s] %s: %s',
|
|
|
|
|
'tag',
|
|
|
|
|
'abc',
|
|
|
|
|
'DEBUG',
|
|
|
|
|
'first'
|
2021-11-28 19:40:16 +01:00
|
|
|
)
|
|
|
|
|
expect(consoleLogSpy).toHaveBeenNthCalledWith(
|
2022-10-23 11:55:47 +02:00
|
|
|
2,
|
|
|
|
|
'<%s> [%s] %s: %s',
|
|
|
|
|
'tag',
|
|
|
|
|
'abc',
|
|
|
|
|
'INFO',
|
|
|
|
|
'second'
|
2021-11-28 19:40:16 +01:00
|
|
|
)
|
|
|
|
|
expect(consoleErrorSpy).toHaveBeenCalledTimes(1)
|
|
|
|
|
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
2022-10-23 11:55:47 +02:00
|
|
|
'<%s> [%s] %s: %s - %O',
|
|
|
|
|
'tag',
|
|
|
|
|
'abc',
|
|
|
|
|
'ERROR',
|
|
|
|
|
'third',
|
|
|
|
|
{ errorMessage: 'test' }
|
2021-11-28 19:40:16 +01:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('should log only when level is >= LogLevel of LoggerInstance', () => {
|
|
|
|
|
const consoleLogSpy = jest.fn()
|
|
|
|
|
jest.spyOn(console, 'log').mockImplementationOnce(consoleLogSpy)
|
2022-10-23 11:55:47 +02:00
|
|
|
|
2022-11-30 11:56:39 +01:00
|
|
|
const logger = new Logger('tag', 'abc', LogLevel.INFO)
|
|
|
|
|
logger.debug("shouldn't appear")
|
2021-11-28 19:40:16 +01:00
|
|
|
|
|
|
|
|
expect(consoleLogSpy).not.toHaveBeenCalled()
|
|
|
|
|
})
|
2022-11-27 16:09:30 +01:00
|
|
|
|
|
|
|
|
test('should obfuscate password from message', () => {
|
|
|
|
|
const consoleLogSpy = jest.fn()
|
|
|
|
|
const consoleErrorSpy = jest.fn()
|
|
|
|
|
jest.spyOn(console, 'log').mockImplementation(consoleLogSpy)
|
|
|
|
|
jest.spyOn(console, 'error').mockImplementation(consoleErrorSpy)
|
|
|
|
|
|
2022-11-30 11:56:39 +01:00
|
|
|
const logger = new Logger('tag', 'abc', LogLevel.DEBUG)
|
|
|
|
|
logger.info('first', { password: 'test' })
|
2022-11-27 16:09:30 +01:00
|
|
|
|
|
|
|
|
expect(consoleLogSpy).toHaveBeenCalledTimes(1)
|
|
|
|
|
expect(consoleLogSpy).toHaveBeenNthCalledWith(
|
|
|
|
|
1,
|
|
|
|
|
'<%s> [%s] %s: %s - %O',
|
|
|
|
|
'tag',
|
|
|
|
|
'abc',
|
|
|
|
|
'INFO',
|
|
|
|
|
'first',
|
2022-11-29 22:51:28 +01:00
|
|
|
{ password: '***' }
|
2022-11-27 16:09:30 +01:00
|
|
|
)
|
|
|
|
|
})
|
2022-10-23 11:55:47 +02:00
|
|
|
})
|