Skip to content

feat(core): Allow re-use of captureLog #16352

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/core/src/logs/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,25 @@ export function logAttributeToSerializedLogAttribute(value: unknown): Serialized
}
}

function defaultCaptureSerializedLog(client: Client, serializedLog: SerializedLog): void {
const logBuffer = _INTERNAL_getLogBuffer(client);
if (logBuffer === undefined) {
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [serializedLog]);
} else {
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [...logBuffer, serializedLog]);
if (logBuffer.length >= MAX_LOG_BUFFER_SIZE) {
_INTERNAL_flushLogsBuffer(client, logBuffer);
}
}
}

/**
* Captures a log event and sends it to Sentry.
*
* @param log - The log event to capture.
* @param scope - A scope. Uses the current scope if not provided.
* @param client - A client. Uses the current client if not provided.
* @param captureSerializedLog - A function to capture the serialized log.
*
* @experimental This method will experience breaking changes. This is not yet part of
* the stable Sentry SDK API and can be changed or removed without warning.
Expand All @@ -75,6 +88,7 @@ export function _INTERNAL_captureLog(
beforeLog: Log,
client: Client | undefined = getClient(),
scope = getCurrentScope(),
captureSerializedLog: (client: Client, log: SerializedLog) => void = defaultCaptureSerializedLog,
): void {
if (!client) {
DEBUG_BUILD && logger.warn('No client available to capture log.');
Expand Down Expand Up @@ -151,15 +165,7 @@ export function _INTERNAL_captureLog(
),
};

const logBuffer = _INTERNAL_getLogBuffer(client);
if (logBuffer === undefined) {
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [serializedLog]);
} else {
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [...logBuffer, serializedLog]);
if (logBuffer.length >= MAX_LOG_BUFFER_SIZE) {
_INTERNAL_flushLogsBuffer(client, logBuffer);
}
}
captureSerializedLog(client, serializedLog);

client.emit('afterCaptureLog', log);
}
Expand Down
Loading