-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add basic logging support for browser-telemetry. (#736)
Adds the ability to specify a logger and to also use the base SDK logger when possible. The base SDK of a 3.x version does not expose the logger, but 4.x will. There is a minimal replication of the warning level of the logging interface to allow compatibility with both the 3.x SDK and the 4.x SDK. Prefixing is done at message time instead of as part of the logger. Potentially it could be ideal to do it in the logger, but this approach makes it clear that the interpolation supported by most browser loggers will not come into play. (Where if the logger does this prefixing it is either complex or you lose the sprintf style formatting support.) Some earlier log messages has been added, but a logger instance was not yet available.
- Loading branch information
1 parent
5c327a1
commit 2ef1486
Showing
12 changed files
with
385 additions
and
37 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
52 changes: 52 additions & 0 deletions
52
packages/telemetry/browser-telemetry/__tests__/logging.test.ts
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,52 @@ | ||
import { MinLogger } from '../src/api'; | ||
import { fallbackLogger, prefixLog, safeMinLogger } from '../src/logging'; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('prefixes the message with the telemetry prefix', () => { | ||
const message = 'test message'; | ||
const prefixed = prefixLog(message); | ||
expect(prefixed).toBe('LaunchDarkly - Browser Telemetry: test message'); | ||
}); | ||
|
||
it('uses fallback logger when no logger provided', () => { | ||
const spy = jest.spyOn(fallbackLogger, 'warn'); | ||
const logger = safeMinLogger(undefined); | ||
|
||
logger.warn('test message'); | ||
|
||
expect(spy).toHaveBeenCalledWith('test message'); | ||
spy.mockRestore(); | ||
}); | ||
|
||
it('uses provided logger when it works correctly', () => { | ||
const mockWarn = jest.fn(); | ||
const testLogger: MinLogger = { | ||
warn: mockWarn, | ||
}; | ||
|
||
const logger = safeMinLogger(testLogger); | ||
logger.warn('test message'); | ||
|
||
expect(mockWarn).toHaveBeenCalledWith('test message'); | ||
}); | ||
|
||
it('falls back to fallback logger when provided logger throws', () => { | ||
const spy = jest.spyOn(fallbackLogger, 'warn'); | ||
const testLogger: MinLogger = { | ||
warn: () => { | ||
throw new Error('logger error'); | ||
}, | ||
}; | ||
|
||
const logger = safeMinLogger(testLogger); | ||
logger.warn('test message'); | ||
|
||
expect(spy).toHaveBeenCalledWith('test message'); | ||
expect(spy).toHaveBeenCalledWith( | ||
'LaunchDarkly - Browser Telemetry: The provided logger threw an exception, using fallback logger.', | ||
); | ||
spy.mockRestore(); | ||
}); |
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
Oops, something went wrong.