-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js,shared): Custom logger to enable logging once per sessi…
…on (#3383)
- Loading branch information
Showing
9 changed files
with
76 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/shared': patch | ||
--- | ||
|
||
Add a custom logger to allow logging a message or warning to the console once per session, in order to avoid consecutive identical logs due to component rerenders. |
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,33 @@ | ||
import { logger } from '../logger'; | ||
|
||
describe('logger', () => { | ||
describe('warnOnce', () => { | ||
const warnMock = jest.spyOn(global.console, 'warn').mockImplementation(); | ||
|
||
beforeEach(() => warnMock.mockClear()); | ||
afterAll(() => warnMock.mockRestore()); | ||
|
||
test('warns only once per session', () => { | ||
logger.warnOnce('testwarn'); | ||
logger.warnOnce('testwarn'); | ||
logger.warnOnce('testwarn'); | ||
|
||
expect(warnMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('logOnce', () => { | ||
const logMock = jest.spyOn(global.console, 'log').mockImplementation(); | ||
|
||
beforeEach(() => logMock.mockClear()); | ||
afterAll(() => logMock.mockRestore()); | ||
|
||
test('logs only once per session', () => { | ||
logger.logOnce('testlog'); | ||
logger.logOnce('testlog'); | ||
logger.logOnce('testlog'); | ||
|
||
expect(logMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
const loggedMessages: Set<string> = new Set(); | ||
|
||
export const logger = { | ||
/** | ||
* A custom logger that ensures messages are logged only once. | ||
* Reduces noise and duplicated messages when logs are in a hot codepath. | ||
*/ | ||
warnOnce: (msg: string) => { | ||
if (loggedMessages.has(msg)) { | ||
return; | ||
} | ||
|
||
loggedMessages.add(msg); | ||
console.warn(msg); | ||
}, | ||
logOnce: (msg: string) => { | ||
if (loggedMessages.has(msg)) { | ||
return; | ||
} | ||
|
||
console.log(msg); | ||
loggedMessages.add(msg); | ||
}, | ||
}; |
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