-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathlog.ts
More file actions
197 lines (187 loc) · 5.11 KB
/
log.ts
File metadata and controls
197 lines (187 loc) · 5.11 KB
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
import { format } from 'node:util';
import type { LogSeverityLevel, Log, ParameterizedString } from '@sentry/core';
import { _INTERNAL_captureLog } from '@sentry/core';
type CaptureLogArgs =
| [message: ParameterizedString, attributes?: Log['attributes']]
| [messageTemplate: string, messageParams: Array<unknown>, attributes?: Log['attributes']];
/**
* 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.
*/
function captureLog(level: LogSeverityLevel, ...args: CaptureLogArgs): void {
const [messageOrMessageTemplate, paramsOrAttributes, maybeAttributes] = args;
if (Array.isArray(paramsOrAttributes)) {
const attributes = { ...maybeAttributes };
attributes['sentry.message.template'] = messageOrMessageTemplate;
paramsOrAttributes.forEach((param, index) => {
attributes[`sentry.message.parameter.${index}`] = param;
});
const message = format(messageOrMessageTemplate, ...paramsOrAttributes);
_INTERNAL_captureLog({ level, message, attributes });
} else {
_INTERNAL_captureLog({ level, message: messageOrMessageTemplate, attributes: paramsOrAttributes });
}
}
/**
* @summary Capture a log with the `trace` level. Requires `_experiments.enableLogs` to be enabled.
*
* You can either pass a message and attributes or a message template, params and attributes.
*
* @example
*
* ```
* Sentry.logger.trace('Starting database connection', {
* database: 'users',
* connectionId: 'conn_123'
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.trace('Database connection %s established for %s',
* ['successful', 'users'],
* { connectionId: 'conn_123' }
* );
* ```
*/
export function trace(...args: CaptureLogArgs): void {
captureLog('trace', ...args);
}
/**
* @summary Capture a log with the `debug` level. Requires `_experiments.enableLogs` to be enabled.
*
* You can either pass a message and attributes or a message template, params and attributes.
*
* @example
*
* ```
* Sentry.logger.debug('Cache miss for user profile', {
* userId: 'user_123',
* cacheKey: 'profile:user_123'
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.debug('Cache %s for %s: %s',
* ['miss', 'user profile', 'key not found'],
* { userId: 'user_123' }
* );
* ```
*/
export function debug(...args: CaptureLogArgs): void {
captureLog('debug', ...args);
}
/**
* @summary Capture a log with the `info` level. Requires `_experiments.enableLogs` to be enabled.
*
* You can either pass a message and attributes or a message template, params and attributes.
*
* @example
*
* ```
* Sentry.logger.info('User profile updated', {
* userId: 'user_123',
* updatedFields: ['email', 'preferences']
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.info('User %s updated their %s',
* ['John Doe', 'profile settings'],
* { userId: 'user_123' }
* );
* ```
*/
export function info(...args: CaptureLogArgs): void {
captureLog('info', ...args);
}
/**
* @summary Capture a log with the `warn` level. Requires `_experiments.enableLogs` to be enabled.
*
* You can either pass a message and attributes or a message template, params and attributes.
*
* @example
*
* ```
* Sentry.logger.warn('Rate limit approaching', {
* endpoint: '/api/users',
* currentRate: '95/100',
* resetTime: '2024-03-20T10:00:00Z'
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.warn('Rate limit %s for %s: %s',
* ['approaching', '/api/users', '95/100 requests'],
* { resetTime: '2024-03-20T10:00:00Z' }
* );
* ```
*/
export function warn(...args: CaptureLogArgs): void {
captureLog('warn', ...args);
}
/**
* @summary Capture a log with the `error` level. Requires `_experiments.enableLogs` to be enabled.
*
* You can either pass a message and attributes or a message template, params and attributes.
*
* @example
*
* ```
* Sentry.logger.error('Failed to process payment', {
* orderId: 'order_123',
* errorCode: 'PAYMENT_FAILED',
* amount: 99.99
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.error('Payment processing failed for order %s: %s',
* ['order_123', 'insufficient funds'],
* { amount: 99.99 }
* );
* ```
*/
export function error(...args: CaptureLogArgs): void {
captureLog('error', ...args);
}
/**
* @summary Capture a log with the `fatal` level. Requires `_experiments.enableLogs` to be enabled.
*
* You can either pass a message and attributes or a message template, params and attributes.
*
* @example
*
* ```
* Sentry.logger.fatal('Database connection pool exhausted', {
* database: 'users',
* activeConnections: 100,
* maxConnections: 100
* });
* ```
*
* @example With template strings
*
* ```
* Sentry.logger.fatal('Database %s: %s connections active',
* ['connection pool exhausted', '100/100'],
* { database: 'users' }
* );
* ```
*/
export function fatal(...args: CaptureLogArgs): void {
captureLog('fatal', ...args);
}
export { fmt } from '@sentry/core';