1
+ import { getGlobalSingleton } from '../carrier' ;
1
2
import type { Client } from '../client' ;
2
3
import { _getTraceInfoFromScope } from '../client' ;
3
4
import { getClient , getCurrentScope , getGlobalScope , getIsolationScope } from '../currentScopes' ;
@@ -9,15 +10,11 @@ import { isParameterizedString } from '../utils/is';
9
10
import { debug } from '../utils/logger' ;
10
11
import { _getSpanForScope } from '../utils/spanOnScope' ;
11
12
import { timestampInSeconds } from '../utils/time' ;
12
- import { GLOBAL_OBJ } from '../utils/worldwide' ;
13
13
import { SEVERITY_TEXT_TO_SEVERITY_NUMBER } from './constants' ;
14
14
import { createLogEnvelope } from './envelope' ;
15
15
16
16
const MAX_LOG_BUFFER_SIZE = 100 ;
17
17
18
- // The reference to the Client <> LogBuffer map is stored to ensure it's always the same
19
- GLOBAL_OBJ . _sentryClientToLogBufferMap = new WeakMap < Client , Array < SerializedLog > > ( ) ;
20
-
21
18
/**
22
19
* Converts a log attribute to a serialized log attribute.
23
20
*
@@ -92,11 +89,13 @@ function setLogAttribute(
92
89
* the stable Sentry SDK API and can be changed or removed without warning.
93
90
*/
94
91
export function _INTERNAL_captureSerializedLog ( client : Client , serializedLog : SerializedLog ) : void {
92
+ const bufferMap = _getBufferMap ( ) ;
93
+
95
94
const logBuffer = _INTERNAL_getLogBuffer ( client ) ;
96
95
if ( logBuffer === undefined ) {
97
- GLOBAL_OBJ . _sentryClientToLogBufferMap ? .set ( client , [ serializedLog ] ) ;
96
+ bufferMap . set ( client , [ serializedLog ] ) ;
98
97
} else {
99
- GLOBAL_OBJ . _sentryClientToLogBufferMap ? .set ( client , [ ...logBuffer , serializedLog ] ) ;
98
+ bufferMap . set ( client , [ ...logBuffer , serializedLog ] ) ;
100
99
if ( logBuffer . length >= MAX_LOG_BUFFER_SIZE ) {
101
100
_INTERNAL_flushLogsBuffer ( client , logBuffer ) ;
102
101
}
@@ -217,7 +216,7 @@ export function _INTERNAL_flushLogsBuffer(client: Client, maybeLogBuffer?: Array
217
216
const envelope = createLogEnvelope ( logBuffer , clientOptions . _metadata , clientOptions . tunnel , client . getDsn ( ) ) ;
218
217
219
218
// Clear the log buffer after envelopes have been constructed.
220
- GLOBAL_OBJ . _sentryClientToLogBufferMap ? .set ( client , [ ] ) ;
219
+ _getBufferMap ( ) . set ( client , [ ] ) ;
221
220
222
221
client . emit ( 'flushLogs' ) ;
223
222
@@ -235,7 +234,7 @@ export function _INTERNAL_flushLogsBuffer(client: Client, maybeLogBuffer?: Array
235
234
* @returns The log buffer for the given client.
236
235
*/
237
236
export function _INTERNAL_getLogBuffer ( client : Client ) : Array < SerializedLog > | undefined {
238
- return GLOBAL_OBJ . _sentryClientToLogBufferMap ? .get ( client ) ;
237
+ return _getBufferMap ( ) . get ( client ) ;
239
238
}
240
239
241
240
/**
@@ -251,3 +250,8 @@ function getMergedScopeData(currentScope: Scope): ScopeData {
251
250
mergeScopeData ( scopeData , currentScope . getScopeData ( ) ) ;
252
251
return scopeData ;
253
252
}
253
+
254
+ function _getBufferMap ( ) : WeakMap < Client , Array < SerializedLog > > {
255
+ // The reference to the Client <> LogBuffer map is stored on the carrier to ensure it's always the same
256
+ return getGlobalSingleton ( 'clientToLogBufferMap' , ( ) => new WeakMap < Client , Array < SerializedLog > > ( ) ) ;
257
+ }
0 commit comments