Skip to content

Commit 3447bb6

Browse files
committed
unify capture log methods
1 parent ff62866 commit 3447bb6

File tree

2 files changed

+124
-282
lines changed

2 files changed

+124
-282
lines changed

packages/node/src/log.ts

+111-156
Original file line numberDiff line numberDiff line change
@@ -3,266 +3,221 @@ import { format } from 'node:util';
33
import type { LogSeverityLevel, Log } from '@sentry/core';
44
import { _INTERNAL_captureLog } from '@sentry/core';
55

6+
type CaptureLogArgs =
7+
| [message: string, attributes?: Log['attributes']]
8+
| [messageTemplate: string, messageParams: Array<unknown>, attributes?: Log['attributes']];
9+
610
/**
711
* Capture a log with the given level.
812
*
913
* @param level - The level of the log.
1014
* @param message - The message to log.
1115
* @param attributes - Arbitrary structured data that stores information about the log - e.g., userId: 100.
1216
*/
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+
}
1530
}
1631

1732
/**
1833
* @summary Capture a log with the `trace` level. Requires `_experiments.enableLogs` to be enabled.
1934
*
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.
2236
*
2337
* @example
2438
*
2539
* ```
26-
* Sentry.logger.trace('Hello world', { userId: 100 });
40+
* Sentry.logger.trace('Starting database connection', {
41+
* database: 'users',
42+
* connectionId: 'conn_123'
43+
* });
2744
* ```
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.
3845
*
39-
* @example
46+
* @example With template strings
4047
*
4148
* ```
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+
* );
4353
* ```
4454
*/
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);
4757
}
4858

4959
/**
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.
5161
*
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.
5463
*
5564
* @example
5665
*
5766
* ```
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+
* });
5971
* ```
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.
6772
*
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
7274
*
7375
* ```
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+
* );
7580
* ```
7681
*/
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);
7984
}
8085

8186
/**
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.
8388
*
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.
8690
*
8791
* @example
8892
*
8993
* ```
90-
* Sentry.logger.error('Hello world', { userId: 100 });
94+
* Sentry.logger.info('User profile updated', {
95+
* userId: 'user_123',
96+
* updatedFields: ['email', 'preferences']
97+
* });
9198
* ```
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.
9999
*
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
104101
*
105102
* ```
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+
* );
107107
* ```
108108
*/
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);
111111
}
112112

113113
/**
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.
115115
*
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.
118117
*
119118
* @example
120119
*
121120
* ```
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+
* });
123126
* ```
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.
157127
*
158-
* @example
128+
* @example With template strings
159129
*
160130
* ```
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+
* );
162135
* ```
163136
*/
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);
166139
}
167140

168141
/**
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.
170143
*
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.
174145
*
175146
* @example
176147
*
177148
* ```
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+
* });
179154
* ```
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.
187155
*
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
193157
*
194158
* ```
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+
* );
196163
* ```
197164
*/
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);
200167
}
201168

202169
/**
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.
204171
*
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.
208173
*
209174
* @example
210175
*
211176
* ```
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+
* });
213182
* ```
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.
221183
*
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
227185
*
228186
* ```
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+
* );
230191
* ```
231192
*/
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);
234195
}
235196

236197
/**
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.
238199
*
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.
242201
*
243202
* @example
244203
*
245204
* ```
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+
* });
247210
* ```
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.
255211
*
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
261213
*
262214
* ```
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+
* );
264219
* ```
265220
*/
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);
268223
}

0 commit comments

Comments
 (0)