-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlog.ts
292 lines (272 loc) · 7.87 KB
/
log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import type { LogSeverityLevel, Log, Client, ParameterizedString } 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: ParameterizedString,
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, route: '/dashboard' }.
*
* @example
*
* ```
* Sentry.logger.trace('User clicked submit button', {
* buttonId: 'submit-form',
* formId: 'user-profile',
* timestamp: Date.now()
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.trace(Sentry.logger.fmt`User ${user} navigated to ${page}`, {
* userId: '123',
* sessionId: 'abc-xyz'
* });
* ```
*/
export function trace(message: ParameterizedString, 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., { component: 'Header', state: 'loading' }.
*
* @example
*
* ```
* Sentry.logger.debug('Component mounted', {
* component: 'UserProfile',
* props: { userId: 123 },
* renderTime: 150
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.debug(Sentry.logger.fmt`API request to ${endpoint} failed`, {
* statusCode: 404,
* requestId: 'req-123',
* duration: 250
* });
* ```
*/
export function debug(message: ParameterizedString, 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., { feature: 'checkout', status: 'completed' }.
*
* @example
*
* ```
* Sentry.logger.info('User completed checkout', {
* orderId: 'order-123',
* amount: 99.99,
* paymentMethod: 'credit_card'
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.info(Sentry.logger.fmt`User ${user} updated profile picture`, {
* userId: 'user-123',
* imageSize: '2.5MB',
* timestamp: Date.now()
* });
* ```
*/
export function info(message: ParameterizedString, 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., { browser: 'Chrome', version: '91.0' }.
*
* @example
*
* ```
* Sentry.logger.warn('Browser compatibility issue detected', {
* browser: 'Safari',
* version: '14.0',
* feature: 'WebRTC',
* fallback: 'enabled'
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.warn(Sentry.logger.fmt`API endpoint ${endpoint} is deprecated`, {
* recommendedEndpoint: '/api/v2/users',
* sunsetDate: '2024-12-31',
* clientVersion: '1.2.3'
* });
* ```
*/
export function warn(message: ParameterizedString, 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., { error: 'NetworkError', url: '/api/data' }.
*
* @example
*
* ```
* Sentry.logger.error('Failed to load user data', {
* error: 'NetworkError',
* url: '/api/users/123',
* statusCode: 500,
* retryCount: 3
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.error(Sentry.logger.fmt`Payment processing failed for order ${orderId}`, {
* error: 'InsufficientFunds',
* amount: 100.00,
* currency: 'USD',
* userId: 'user-456'
* });
* ```
*/
export function error(message: ParameterizedString, 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., { appState: 'corrupted', sessionId: 'abc-123' }.
*
* @example
*
* ```
* Sentry.logger.fatal('Application state corrupted', {
* lastKnownState: 'authenticated',
* sessionId: 'session-123',
* timestamp: Date.now(),
* recoveryAttempted: true
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.fatal(Sentry.logger.fmt`Critical system failure in ${service}`, {
* service: 'payment-processor',
* errorCode: 'CRITICAL_FAILURE',
* affectedUsers: 150,
* timestamp: Date.now()
* });
* ```
*/
export function fatal(message: ParameterizedString, 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., { security: 'breach', severity: 'high' }.
*
* @example
*
* ```
* Sentry.logger.critical('Security breach detected', {
* type: 'unauthorized_access',
* user: '132123',
* endpoint: '/api/admin',
* timestamp: Date.now()
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.critical(Sentry.logger.fmt`Multiple failed login attempts from user ${user}`, {
* attempts: 10,
* timeWindow: '5m',
* blocked: true,
* timestamp: Date.now()
* });
* ```
*/
export function critical(message: ParameterizedString, attributes?: Log['attributes']): void {
captureLog('critical', message, attributes);
}
export { fmt } from '@sentry/core';