-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlog.ts
186 lines (167 loc) · 5.22 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
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);
}