-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logs): Added an option to control log level
- Loading branch information
1 parent
dcb5f0a
commit d9f1689
Showing
11 changed files
with
131 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { LogLevel } from '../../internal'; | ||
import { Logger } from '../../internal/base/logger'; | ||
import { type Response } from 'node-fetch'; | ||
|
||
describe('logger', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test.each([ | ||
[LogLevel.verbose, LogLevel.verbose, true], | ||
[LogLevel.verbose, LogLevel.warn, true], | ||
[LogLevel.verbose, LogLevel.error, true], | ||
[LogLevel.verbose, LogLevel.none, false], | ||
[LogLevel.warn, LogLevel.verbose, false], | ||
[LogLevel.warn, LogLevel.warn, true], | ||
[LogLevel.warn, LogLevel.error, true], | ||
[LogLevel.warn, LogLevel.none, false], | ||
[LogLevel.error, LogLevel.verbose, false], | ||
[LogLevel.error, LogLevel.warn, false], | ||
[LogLevel.error, LogLevel.error, true], | ||
[LogLevel.error, LogLevel.none, false], | ||
[LogLevel.none, LogLevel.verbose, false], | ||
[LogLevel.none, LogLevel.warn, false], | ||
[LogLevel.none, LogLevel.error, false], | ||
[LogLevel.none, LogLevel.none, false], | ||
])('shouldLog(%s) with logLevel %s should return %s', (logLevel, level, expected) => { | ||
Logger.logLevel = logLevel; | ||
// @ts-expect-error - testing private method | ||
expect(Logger.shouldLog(level)).toBe(expected); | ||
}); | ||
test('log', () => { | ||
Logger.logLevel = LogLevel.verbose; | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
Logger.log('test'); | ||
expect(consoleSpy).toHaveBeenCalledWith('[Paddle] [LOG]', 'test'); | ||
}); | ||
test('warn', () => { | ||
Logger.logLevel = LogLevel.warn; | ||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(); | ||
Logger.warn('test'); | ||
expect(consoleSpy).toHaveBeenCalledWith('[Paddle] [WARN]', 'test'); | ||
}); | ||
test('error', () => { | ||
Logger.logLevel = LogLevel.error; | ||
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(); | ||
Logger.error('test'); | ||
expect(consoleSpy).toHaveBeenCalledWith('[Paddle] [ERROR]', 'test'); | ||
}); | ||
test('logRequest', () => { | ||
Logger.logLevel = LogLevel.verbose; | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
Logger.logRequest('GET', 'https://example.com', { 'X-Transaction-ID': '123' }); | ||
expect(consoleSpy).toHaveBeenCalledWith( | ||
'[Paddle] [LOG]', | ||
'[Request]', | ||
'GET', | ||
'https://example.com', | ||
'Transaction ID:', | ||
'123', | ||
); | ||
}); | ||
test('logResponse', () => { | ||
Logger.logLevel = LogLevel.verbose; | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
const response = { | ||
status: 200, | ||
headers: new Map([['Request-Id', '456']]), | ||
}; | ||
Logger.logResponse('GET', 'https://example.com', { 'X-Transaction-ID': '123' }, response as unknown as Response); | ||
expect(consoleSpy).toHaveBeenCalledWith( | ||
'[Paddle] [LOG]', | ||
'[Response]', | ||
'GET', | ||
'https://example.com', | ||
'200', | ||
'Transaction ID:', | ||
'123', | ||
'Request ID:', | ||
'456', | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { Environment } from './environment'; | ||
export { convertToSnakeCase } from './case-helpers'; | ||
export { LogLevel } from './log-level'; |
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,6 @@ | ||
export enum LogLevel { | ||
verbose = 'verbose', | ||
warn = 'warn', | ||
error = 'error', | ||
none = 'none', | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { type Environment } from '../api'; | ||
import { type Environment, LogLevel } from '../api'; | ||
|
||
export interface PaddleOptions { | ||
environment?: Environment; | ||
logLevel?: LogLevel; | ||
} |
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