Skip to content

Commit 7b83155

Browse files
committed
feat: Export logging methods for node/server-side SDKs
1 parent 4d0678a commit 7b83155

File tree

13 files changed

+29
-3
lines changed

13 files changed

+29
-3
lines changed

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export {
127127
withScope,
128128
zodErrorsIntegration,
129129
profiler,
130+
_experiment_log,
130131
} from '@sentry/node';
131132

132133
export { init } from './server/sdk';

packages/aws-serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export {
113113
profiler,
114114
amqplibIntegration,
115115
vercelAIIntegration,
116+
_experiment_log,
116117
} from '@sentry/node';
117118

118119
export {

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export {
132132
profiler,
133133
amqplibIntegration,
134134
vercelAIIntegration,
135+
_experiment_log,
135136
} from '@sentry/node';
136137

137138
export {

packages/cloudflare/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export {
8585
spanToTraceHeader,
8686
spanToBaggageHeader,
8787
updateSpanName,
88+
_experiment_log,
8889
} from '@sentry/core';
8990

9091
export { withSentry } from './handler';

packages/core/src/exports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
Extras,
1313
FinishedCheckIn,
1414
MonitorConfig,
15-
ParameterizedString,
1615
Primitive,
1716
Session,
1817
SessionContext,
@@ -341,7 +340,7 @@ export function captureSession(end: boolean = false): void {
341340
/**
342341
* A namespace for experimental logging functions.
343342
*
344-
* @experimental Will be removed in future versions. Do not use.
343+
* @experimental Will be removed in future versions. Use with caution.
345344
*/
346345
export const _experiment_log = {
347346
/**

packages/core/src/log.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,24 @@ function valueToAttribute(key: string, value: unknown): LogAttribute {
8282
}
8383
}
8484

85+
let hasRegisteredFlushHook = false;
86+
8587
function addToLogBuffer(client: Client, log: Log, scope: Scope): void {
8688
function sendLogs(flushedLogs: Log[]): void {
8789
const envelope = createLogEnvelope(flushedLogs, client, scope);
8890
// eslint-disable-next-line @typescript-eslint/no-floating-promises
8991
void client.sendEnvelope(envelope);
9092
}
9193

94+
// Only register the hook once
95+
if (!hasRegisteredFlushHook) {
96+
client.on('flush', () => {
97+
sendLogs(GLOBAL_LOG_BUFFER);
98+
GLOBAL_LOG_BUFFER = [];
99+
});
100+
hasRegisteredFlushHook = true;
101+
}
102+
92103
if (GLOBAL_LOG_BUFFER.length >= LOG_BUFFER_MAX_LENGTH) {
93104
sendLogs(GLOBAL_LOG_BUFFER);
94105
GLOBAL_LOG_BUFFER = [];
@@ -99,12 +110,17 @@ function addToLogBuffer(client: Client, log: Log, scope: Scope): void {
99110
// this is the first time logs have been enabled, let's kick off an interval to flush them
100111
// we should only do this once.
101112
if (!isFlushingLogs) {
102-
setInterval(() => {
113+
const flushTimer = setInterval(() => {
103114
if (GLOBAL_LOG_BUFFER.length > 0) {
104115
sendLogs(GLOBAL_LOG_BUFFER);
105116
GLOBAL_LOG_BUFFER = [];
106117
}
107118
}, 5000);
119+
120+
// We need to unref the timer in node.js, otherwise the node process never exit.
121+
if (typeof flushTimer !== 'number' && flushTimer.unref) {
122+
flushTimer.unref();
123+
}
108124
}
109125
isFlushingLogs = true;
110126
}

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export {
8383
spanToTraceHeader,
8484
spanToBaggageHeader,
8585
updateSpanName,
86+
_experiment_log,
8687
} from '@sentry/core';
8788

8889
export { DenoClient } from './client';

packages/google-cloud-serverless/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export {
113113
amqplibIntegration,
114114
childProcessIntegration,
115115
vercelAIIntegration,
116+
_experiment_log,
116117
} from '@sentry/node';
117118

118119
export {

packages/node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export {
130130
updateSpanName,
131131
zodErrorsIntegration,
132132
profiler,
133+
_experiment_log,
133134
} from '@sentry/core';
134135

135136
export type {

packages/remix/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export {
112112
withMonitor,
113113
withScope,
114114
zodErrorsIntegration,
115+
_experiment_log,
115116
} from '@sentry/node';
116117

117118
// Keeping the `*` exports for backwards compatibility and types

0 commit comments

Comments
 (0)