-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(browser): Add logger.X methods to browser SDK #15763
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import type { LogSeverityLevel, Log, Client } from '@sentry/core'; | ||
import { getClient, _INTERNAL_captureLog, _INTERNAL_flushLogsBuffer } from '@sentry/core'; | ||
|
||
import { WINDOW } from './helpers'; | ||
|
||
/** | ||
* TODO: Make this configurable | ||
*/ | ||
const DEFAULT_FLUSH_INTERVAL = 5000; | ||
|
||
let timeout: ReturnType<typeof setTimeout> | undefined; | ||
|
||
/** | ||
* This is a global timeout that is used to flush the logs buffer. | ||
* It is used to ensure that logs are flushed even if the client is not flushed. | ||
*/ | ||
function startFlushTimeout(client: Client): void { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
|
||
timeout = setTimeout(() => { | ||
_INTERNAL_flushLogsBuffer(client); | ||
}, DEFAULT_FLUSH_INTERVAL); | ||
} | ||
|
||
let isClientListenerAdded = false; | ||
/** | ||
* This is a function that is used to add a flush listener to the client. | ||
* It is used to ensure that the logger buffer is flushed when the client is flushed. | ||
*/ | ||
function addFlushingListeners(client: Client): void { | ||
if (isClientListenerAdded || !client.getOptions()._experiments?.enableLogs) { | ||
return; | ||
} | ||
|
||
isClientListenerAdded = true; | ||
|
||
if (WINDOW.document) { | ||
WINDOW.document.addEventListener('visibilitychange', () => { | ||
if (WINDOW.document.visibilityState === 'hidden') { | ||
_INTERNAL_flushLogsBuffer(client); | ||
} | ||
}); | ||
} | ||
|
||
client.on('flush', () => { | ||
_INTERNAL_flushLogsBuffer(client); | ||
}); | ||
} | ||
|
||
/** | ||
* Capture a log with the given level. | ||
* | ||
* @param level - The level of the log. | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* @param severityNumber - The severity number of the log. | ||
*/ | ||
function captureLog( | ||
level: LogSeverityLevel, | ||
message: string, | ||
attributes?: Log['attributes'], | ||
severityNumber?: Log['severityNumber'], | ||
): void { | ||
const client = getClient(); | ||
if (client) { | ||
addFlushingListeners(client); | ||
|
||
startFlushTimeout(client); | ||
} | ||
|
||
_INTERNAL_captureLog({ level, message, attributes, severityNumber }, client, undefined); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `trace` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.trace('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function trace(message: string, attributes?: Log['attributes']): void { | ||
captureLog('trace', message, attributes); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `debug` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.debug('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function debug(message: string, attributes?: Log['attributes']): void { | ||
captureLog('debug', message, attributes); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `info` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.info('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function info(message: string, attributes?: Log['attributes']): void { | ||
captureLog('info', message, attributes); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `warn` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.warn('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function warn(message: string, attributes?: Log['attributes']): void { | ||
captureLog('warn', message, attributes); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `error` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.error('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function error(message: string, attributes?: Log['attributes']): void { | ||
captureLog('error', message, attributes); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `fatal` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.fatal('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function fatal(message: string, attributes?: Log['attributes']): void { | ||
captureLog('fatal', message, attributes); | ||
} | ||
|
||
/** | ||
* @summary Capture a log with the `critical` level. Requires `_experiments.enableLogs` to be enabled. | ||
* | ||
* @param message - The message to log. | ||
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* Sentry.logger.critical('Hello world', { userId: 100 }); | ||
* ``` | ||
*/ | ||
export function critical(message: string, attributes?: Log['attributes']): void { | ||
captureLog('critical', message, attributes); | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we not also just flush in the client callback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which client callback are you referring too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.