@@ -3,266 +3,221 @@ import { format } from 'node:util';
3
3
import type { LogSeverityLevel , Log } from '@sentry/core' ;
4
4
import { _INTERNAL_captureLog } from '@sentry/core' ;
5
5
6
+ type CaptureLogArgs =
7
+ | [ message : string , attributes ?: Log [ 'attributes' ] ]
8
+ | [ messageTemplate : string , messageParams : Array < unknown > , attributes ?: Log [ 'attributes' ] ] ;
9
+
6
10
/**
7
11
* Capture a log with the given level.
8
12
*
9
13
* @param level - The level of the log.
10
14
* @param message - The message to log.
11
15
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
12
16
*/
13
- function captureLog ( level : LogSeverityLevel , message : string , attributes ?: Log [ 'attributes' ] ) : void {
14
- _INTERNAL_captureLog ( { level, message, attributes } ) ;
17
+ function captureLog ( level : LogSeverityLevel , ...args : CaptureLogArgs ) : void {
18
+ const [ messageOrMessageTemplate , paramsOrAttributes , maybeAttributes ] = args ;
19
+ if ( Array . isArray ( paramsOrAttributes ) ) {
20
+ const attributes = { ...maybeAttributes } ;
21
+ attributes [ 'sentry.message.template' ] = messageOrMessageTemplate ;
22
+ paramsOrAttributes . forEach ( ( param , index ) => {
23
+ attributes [ `sentry.message.param.${ index } ` ] = param ;
24
+ } ) ;
25
+ const message = format ( messageOrMessageTemplate , ...paramsOrAttributes ) ;
26
+ _INTERNAL_captureLog ( { level, message, attributes } ) ;
27
+ } else {
28
+ _INTERNAL_captureLog ( { level, message : messageOrMessageTemplate , attributes : paramsOrAttributes } ) ;
29
+ }
15
30
}
16
31
17
32
/**
18
33
* @summary Capture a log with the `trace` level. Requires `_experiments.enableLogs` to be enabled.
19
34
*
20
- * @param message - The message to log.
21
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
35
+ * You can either pass a message and attributes or a message template, params and attributes.
22
36
*
23
37
* @example
24
38
*
25
39
* ```
26
- * Sentry.logger.trace('Hello world', { userId: 100 });
40
+ * Sentry.logger.trace('Starting database connection', {
41
+ * database: 'users',
42
+ * connectionId: 'conn_123'
43
+ * });
27
44
* ```
28
- */
29
- export function trace ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
30
- captureLog ( 'trace' , message , attributes ) ;
31
- }
32
-
33
- /**
34
- * @summary Capture a log with the `debug` level. Requires `_experiments.enableLogs` to be enabled.
35
- *
36
- * @param message - The message to log.
37
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
38
45
*
39
- * @example
46
+ * @example With template strings
40
47
*
41
48
* ```
42
- * Sentry.logger.debug('Hello world', { userId: 100 });
49
+ * Sentry.logger.trace('Database connection %s established for %s',
50
+ * ['successful', 'users'],
51
+ * { connectionId: 'conn_123' }
52
+ * );
43
53
* ```
44
54
*/
45
- export function debug ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
46
- captureLog ( 'debug ' , message , attributes ) ;
55
+ export function trace ( ... args : CaptureLogArgs ) : void {
56
+ captureLog ( 'trace ' , ... args ) ;
47
57
}
48
58
49
59
/**
50
- * @summary Capture a log with the `info ` level. Requires `_experiments.enableLogs` to be enabled.
60
+ * @summary Capture a log with the `debug ` level. Requires `_experiments.enableLogs` to be enabled.
51
61
*
52
- * @param message - The message to log.
53
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
62
+ * You can either pass a message and attributes or a message template, params and attributes.
54
63
*
55
64
* @example
56
65
*
57
66
* ```
58
- * Sentry.logger.info('Hello world', { userId: 100 });
67
+ * Sentry.logger.debug('Cache miss for user profile', {
68
+ * userId: 'user_123',
69
+ * cacheKey: 'profile:user_123'
70
+ * });
59
71
* ```
60
- */
61
- export function info ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
62
- captureLog ( 'info' , message , attributes ) ;
63
- }
64
-
65
- /**
66
- * @summary Capture a log with the `warn` level. Requires `_experiments.enableLogs` to be enabled.
67
72
*
68
- * @param message - The message to log.
69
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
70
- *
71
- * @example
73
+ * @example With template strings
72
74
*
73
75
* ```
74
- * Sentry.logger.warn('Hello world', { userId: 100 });
76
+ * Sentry.logger.debug('Cache %s for %s: %s',
77
+ * ['miss', 'user profile', 'key not found'],
78
+ * { userId: 'user_123' }
79
+ * );
75
80
* ```
76
81
*/
77
- export function warn ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
78
- captureLog ( 'warn ' , message , attributes ) ;
82
+ export function debug ( ... args : CaptureLogArgs ) : void {
83
+ captureLog ( 'debug ' , ... args ) ;
79
84
}
80
85
81
86
/**
82
- * @summary Capture a log with the `error ` level. Requires `_experiments.enableLogs` to be enabled.
87
+ * @summary Capture a log with the `info ` level. Requires `_experiments.enableLogs` to be enabled.
83
88
*
84
- * @param message - The message to log.
85
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
89
+ * You can either pass a message and attributes or a message template, params and attributes.
86
90
*
87
91
* @example
88
92
*
89
93
* ```
90
- * Sentry.logger.error('Hello world', { userId: 100 });
94
+ * Sentry.logger.info('User profile updated', {
95
+ * userId: 'user_123',
96
+ * updatedFields: ['email', 'preferences']
97
+ * });
91
98
* ```
92
- */
93
- export function error ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
94
- captureLog ( 'error' , message , attributes ) ;
95
- }
96
-
97
- /**
98
- * @summary Capture a log with the `fatal` level. Requires `_experiments.enableLogs` to be enabled.
99
99
*
100
- * @param message - The message to log.
101
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
102
- *
103
- * @example
100
+ * @example With template strings
104
101
*
105
102
* ```
106
- * Sentry.logger.fatal('Hello world', { userId: 100 });
103
+ * Sentry.logger.info('User %s updated their %s',
104
+ * ['John Doe', 'profile settings'],
105
+ * { userId: 'user_123' }
106
+ * );
107
107
* ```
108
108
*/
109
- export function fatal ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
110
- captureLog ( 'fatal ' , message , attributes ) ;
109
+ export function info ( ... args : CaptureLogArgs ) : void {
110
+ captureLog ( 'info ' , ... args ) ;
111
111
}
112
112
113
113
/**
114
- * @summary Capture a log with the `critical ` level. Requires `_experiments.enableLogs` to be enabled.
114
+ * @summary Capture a log with the `warn ` level. Requires `_experiments.enableLogs` to be enabled.
115
115
*
116
- * @param message - The message to log.
117
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
116
+ * You can either pass a message and attributes or a message template, params and attributes.
118
117
*
119
118
* @example
120
119
*
121
120
* ```
122
- * Sentry.logger.critical('Hello world', { userId: 100 });
121
+ * Sentry.logger.warn('Rate limit approaching', {
122
+ * endpoint: '/api/users',
123
+ * currentRate: '95/100',
124
+ * resetTime: '2024-03-20T10:00:00Z'
125
+ * });
123
126
* ```
124
- */
125
- export function critical ( message : string , attributes ?: Log [ 'attributes' ] ) : void {
126
- captureLog ( 'critical' , message , attributes ) ;
127
- }
128
-
129
- /**
130
- * Capture a formatted log with the given level.
131
- *
132
- * @param level - The level of the log.
133
- * @param message - The message template.
134
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
135
- */
136
- function captureLogFmt (
137
- level : LogSeverityLevel ,
138
- messageTemplate : string ,
139
- params : Array < unknown > ,
140
- logAttributes : Log [ 'attributes' ] = { } ,
141
- ) : void {
142
- const attributes = { ...logAttributes } ;
143
- attributes [ 'sentry.message.template' ] = messageTemplate ;
144
- params . forEach ( ( param , index ) => {
145
- attributes [ `sentry.message.param.${ index } ` ] = param ;
146
- } ) ;
147
- const message = format ( messageTemplate , ...params ) ;
148
- _INTERNAL_captureLog ( { level, message, attributes } ) ;
149
- }
150
-
151
- /**
152
- * @summary Capture a formatted log with the `trace` level. Requires `_experiments.enableLogs` to be enabled.
153
- *
154
- * @param message - The message to log.
155
- * @param params - The parameters to interpolate into the message.
156
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
157
127
*
158
- * @example
128
+ * @example With template strings
159
129
*
160
130
* ```
161
- * Sentry.logger.traceFmt('Hello world %s', ['foo'], { userId: 100 });
131
+ * Sentry.logger.warn('Rate limit %s for %s: %s',
132
+ * ['approaching', '/api/users', '95/100 requests'],
133
+ * { resetTime: '2024-03-20T10:00:00Z' }
134
+ * );
162
135
* ```
163
136
*/
164
- export function traceFmt ( message : string , params : Array < unknown > , attributes : Log [ 'attributes' ] = { } ) : void {
165
- captureLogFmt ( 'trace ', message , params , attributes ) ;
137
+ export function warn ( ... args : CaptureLogArgs ) : void {
138
+ captureLog ( 'warn ', ... args ) ;
166
139
}
167
140
168
141
/**
169
- * @summary Capture a formatted log with the `debug ` level. Requires `_experiments.enableLogs` to be enabled.
142
+ * @summary Capture a log with the `error ` level. Requires `_experiments.enableLogs` to be enabled.
170
143
*
171
- * @param message - The message to log.
172
- * @param params - The parameters to interpolate into the message.
173
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
144
+ * You can either pass a message and attributes or a message template, params and attributes.
174
145
*
175
146
* @example
176
147
*
177
148
* ```
178
- * Sentry.logger.debugFmt('Hello world %s', ['foo'], { userId: 100 });
149
+ * Sentry.logger.error('Failed to process payment', {
150
+ * orderId: 'order_123',
151
+ * errorCode: 'PAYMENT_FAILED',
152
+ * amount: 99.99
153
+ * });
179
154
* ```
180
- */
181
- export function debugFmt ( message : string , params : Array < unknown > , attributes : Log [ 'attributes' ] = { } ) : void {
182
- captureLogFmt ( 'debug' , message , params , attributes ) ;
183
- }
184
-
185
- /**
186
- * @summary Capture a formatted log with the `info` level. Requires `_experiments.enableLogs` to be enabled.
187
155
*
188
- * @param message - The message to log.
189
- * @param params - The parameters to interpolate into the message.
190
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
191
- *
192
- * @example
156
+ * @example With template strings
193
157
*
194
158
* ```
195
- * Sentry.logger.infoFmt('Hello world %s', ['foo'], { userId: 100 });
159
+ * Sentry.logger.error('Payment processing failed for order %s: %s',
160
+ * ['order_123', 'insufficient funds'],
161
+ * { amount: 99.99 }
162
+ * );
196
163
* ```
197
164
*/
198
- export function infoFmt ( message : string , params : Array < unknown > , attributes ?: Log [ 'attributes' ] ) : void {
199
- captureLogFmt ( 'info ', message , params , attributes ) ;
165
+ export function error ( ... args : CaptureLogArgs ) : void {
166
+ captureLog ( 'error ', ... args ) ;
200
167
}
201
168
202
169
/**
203
- * @summary Capture a formatted log with the `warn ` level. Requires `_experiments.enableLogs` to be enabled.
170
+ * @summary Capture a log with the `fatal ` level. Requires `_experiments.enableLogs` to be enabled.
204
171
*
205
- * @param message - The message to log.
206
- * @param params - The parameters to interpolate into the message.
207
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
172
+ * You can either pass a message and attributes or a message template, params and attributes.
208
173
*
209
174
* @example
210
175
*
211
176
* ```
212
- * Sentry.logger.warnFmt('Hello world %s', ['foo'], { userId: 100 });
177
+ * Sentry.logger.fatal('Database connection pool exhausted', {
178
+ * database: 'users',
179
+ * activeConnections: 100,
180
+ * maxConnections: 100
181
+ * });
213
182
* ```
214
- */
215
- export function warnFmt ( message : string , params : Array < unknown > , attributes ?: Log [ 'attributes' ] ) : void {
216
- captureLogFmt ( 'warn' , message , params , attributes ) ;
217
- }
218
-
219
- /**
220
- * @summary Capture a formatted log with the `error` level. Requires `_experiments.enableLogs` to be enabled.
221
183
*
222
- * @param message - The message to log.
223
- * @param params - The parameters to interpolate into the message.
224
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
225
- *
226
- * @example
184
+ * @example With template strings
227
185
*
228
186
* ```
229
- * Sentry.logger.errorFmt('Hello world %s', ['foo'], { userId: 100 });
187
+ * Sentry.logger.fatal('Database %s: %s connections active',
188
+ * ['connection pool exhausted', '100/100'],
189
+ * { database: 'users' }
190
+ * );
230
191
* ```
231
192
*/
232
- export function errorFmt ( message : string , params : Array < unknown > , attributes ?: Log [ 'attributes' ] ) : void {
233
- captureLogFmt ( 'error ', message , params , attributes ) ;
193
+ export function fatal ( ... args : CaptureLogArgs ) : void {
194
+ captureLog ( 'fatal ', ... args ) ;
234
195
}
235
196
236
197
/**
237
- * @summary Capture a formatted log with the `fatal ` level. Requires `_experiments.enableLogs` to be enabled.
198
+ * @summary Capture a log with the `critical ` level. Requires `_experiments.enableLogs` to be enabled.
238
199
*
239
- * @param message - The message to log.
240
- * @param params - The parameters to interpolate into the message.
241
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
200
+ * You can either pass a message and attributes or a message template, params and attributes.
242
201
*
243
202
* @example
244
203
*
245
204
* ```
246
- * Sentry.logger.fatalFmt('Hello world %s', ['foo'], { userId: 100 });
205
+ * Sentry.logger.critical('Service health check failed', {
206
+ * service: 'payment-gateway',
207
+ * status: 'DOWN',
208
+ * lastHealthy: '2024-03-20T09:55:00Z'
209
+ * });
247
210
* ```
248
- */
249
- export function fatalFmt ( message : string , params : Array < unknown > , attributes ?: Log [ 'attributes' ] ) : void {
250
- captureLogFmt ( 'fatal' , message , params , attributes ) ;
251
- }
252
-
253
- /**
254
- * @summary Capture a formatted log with the `critical` level. Requires `_experiments.enableLogs` to be enabled.
255
211
*
256
- * @param message - The message to log.
257
- * @param params - The parameters to interpolate into the message.
258
- * @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
259
- *
260
- * @example
212
+ * @example With template strings
261
213
*
262
214
* ```
263
- * Sentry.logger.criticalFmt('Hello world %s', ['foo'], { userId: 100 });
215
+ * Sentry.logger.critical('Service %s is %s',
216
+ * ['payment-gateway', 'DOWN'],
217
+ * { lastHealthy: '2024-03-20T09:55:00Z' }
218
+ * );
264
219
* ```
265
220
*/
266
- export function criticalFmt ( message : string , params : Array < unknown > , attributes ?: Log [ 'attributes' ] ) : void {
267
- captureLogFmt ( 'critical' , message , params , attributes ) ;
221
+ export function critical ( ... args : CaptureLogArgs ) : void {
222
+ captureLog ( 'critical' , ... args ) ;
268
223
}
0 commit comments