Skip to content

Commit b598526

Browse files
committed
ref: re-org functions and exports
1 parent b3cc364 commit b598526

File tree

3 files changed

+81
-40
lines changed

3 files changed

+81
-40
lines changed

packages/core/src/exports.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } from './currentScopes';
22
import { DEBUG_BUILD } from './debug-build';
3-
import { captureLog } from './log';
3+
import { captureLog, sendLog } from './log';
44
import type { CaptureContext } from './scope';
55
import { closeSession, makeSession, updateSession } from './session';
66
import type {
@@ -11,6 +11,7 @@ import type {
1111
Extra,
1212
Extras,
1313
FinishedCheckIn,
14+
LogSeverityLevel,
1415
MonitorConfig,
1516
Primitive,
1617
Session,
@@ -336,29 +337,60 @@ export function captureSession(end: boolean = false): void {
336337
_sendSessionUpdate();
337338
}
338339

340+
type OmitFirstArg<F> = F extends (x: LogSeverityLevel, ...args: infer P) => infer R ? (...args: P) => R : never;
341+
339342
/**
340343
* A namespace for experimental logging functions.
344+
*
345+
* @experimental Will be removed in future versions. Do not use.
341346
*/
342347
export const _experiment_log = {
348+
/**
349+
* A utility to record a log with level 'TRACE' and send it to sentry.
350+
*
351+
* Logs represent a message and some parameters which provide context for a trace or error.
352+
* Ex: Sentry._experiment_log.trace`user ${username} just bought ${item}!`
353+
*/
354+
trace: sendLog.bind(null, 'trace') as OmitFirstArg<typeof sendLog>,
355+
/**
356+
* A utility to record a log with level 'DEBUG' and send it to sentry.
357+
*
358+
* Logs represent a message and some parameters which provide context for a trace or error.
359+
* Ex: Sentry._experiment_log.debug`user ${username} just bought ${item}!`
360+
*/
361+
debug: sendLog.bind(null, 'debug') as OmitFirstArg<typeof sendLog>,
343362
/**
344363
* A utility to record a log with level 'INFO' and send it to sentry.
345364
*
346365
* Logs represent a message and some parameters which provide context for a trace or error.
347366
* Ex: Sentry._experiment_log.info`user ${username} just bought ${item}!`
348367
*/
349-
info: captureLog.bind(null, 'info'),
368+
info: sendLog.bind(null, 'info') as OmitFirstArg<typeof sendLog>,
350369
/**
351370
* A utility to record a log with level 'ERROR' and send it to sentry.
352371
*
353372
* Logs represent a message and some parameters which provide context for a trace or error.
354373
* Ex: Sentry._experiment_log.error`user ${username} just bought ${item}!`
355374
*/
356-
error: captureLog.bind(null, 'error'),
375+
error: sendLog.bind(null, 'error') as OmitFirstArg<typeof sendLog>,
357376
/**
358377
* A utility to record a log with level 'WARN' and send it to sentry.
359378
*
360379
* Logs represent a message and some parameters which provide context for a trace or error.
361380
* Ex: Sentry._experiment_log.warn`user ${username} just bought ${item}!`
362381
*/
363-
warn: captureLog.bind(null, 'warn'),
382+
warn: sendLog.bind(null, 'warn') as OmitFirstArg<typeof sendLog>,
383+
/**
384+
* A utility to record a log with level 'FATAL' and send it to sentry.
385+
*
386+
* Logs represent a message and some parameters which provide context for a trace or error.
387+
* Ex: Sentry._experiment_log.warn`user ${username} just bought ${item}!`
388+
*/
389+
fatal: sendLog.bind(null, 'fatal') as OmitFirstArg<typeof sendLog>,
390+
/**
391+
* A flexible utility to record a log with a custom level and send it to sentry.
392+
*
393+
* You can optionally pass in custom attributes and a custom severity number to be attached to the log.
394+
*/
395+
captureLog,
364396
};

packages/core/src/log.ts

+43-34
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ let GLOBAL_LOG_BUFFER: Log[] = [];
1414
let isFlushingLogs = false;
1515

1616
const SEVERITY_TEXT_TO_SEVERITY_NUMBER: Partial<Record<LogSeverityLevel, number>> = {
17-
debug: 10,
18-
info: 20,
19-
warning: 30,
20-
error: 40,
21-
critical: 50,
17+
trace: 1,
18+
debug: 5,
19+
info: 9,
20+
warn: 13,
21+
error: 17,
22+
fatal: 21,
2223
};
2324

2425
/**
@@ -106,12 +107,36 @@ function addToLogBuffer(client: Client, log: Log, scope: Scope): void {
106107
}
107108

108109
/**
109-
* A utility function to be able to create methods like Sentry.info`...`
110+
* A utility function to be able to create methods like Sentry.info`...` that use tagged template functions.
110111
*
111112
* The first parameter is bound with, e.g., const info = captureLog.bind(null, 'info')
112113
* The other parameters are in the format to be passed a tagged template, Sentry.info`hello ${world}`
113114
*/
114-
export function captureLog(level: LogSeverityLevel, messages: string[] | string, ...values: unknown[]): void {
115+
export function sendLog(level: LogSeverityLevel, messageArr: TemplateStringsArray, ...values: unknown[]): void {
116+
const message = messageArr.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '');
117+
118+
const attributes = values.reduce<Record<string, unknown>>(
119+
(acc, value, index) => {
120+
acc[`param${index}`] = value;
121+
return acc;
122+
},
123+
{
124+
'sentry.template': messageArr.map((s, i) => s + (i < messageArr.length - 1 ? `$param${i}` : '')).join(''),
125+
},
126+
);
127+
128+
captureLog(level, message, attributes);
129+
}
130+
131+
/**
132+
* Sends a log to Sentry.
133+
*/
134+
export function captureLog(
135+
level: LogSeverityLevel,
136+
message: string,
137+
customAttributes: Record<string, unknown> = {},
138+
severityNumber?: number,
139+
): void {
115140
const client = getClient();
116141

117142
if (!client) {
@@ -124,53 +149,37 @@ export function captureLog(level: LogSeverityLevel, messages: string[] | string,
124149
return;
125150
}
126151

127-
const message = Array.isArray(messages)
128-
? messages.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '')
129-
: messages;
130-
const attributes = values.map<LogAttribute>((value, index) => valueToAttribute(`param${index}`, value));
131-
if (Array.isArray(messages)) {
132-
attributes.push({
133-
key: 'sentry.template',
134-
value: {
135-
stringValue: messages.map((s, i) => s + (i < messages.length - 1 ? `$param${i}` : '')).join(''),
136-
},
137-
});
138-
}
139-
140152
const { release, environment } = client.getOptions();
141153

154+
const logAttributes = {
155+
...customAttributes,
156+
};
157+
142158
if (release) {
143-
attributes.push({
144-
key: 'sentry.release',
145-
value: {
146-
stringValue: release,
147-
},
148-
});
159+
logAttributes['sentry.release'] = release;
149160
}
150161

151162
if (environment) {
152-
attributes.push({
153-
key: 'sentry.environment',
154-
value: {
155-
stringValue: environment,
156-
},
157-
});
163+
logAttributes['sentry.environment'] = environment;
158164
}
159165

160166
const scope = getCurrentScope();
161167

168+
const attributes = Object.entries(logAttributes).map<LogAttribute>(([key, value]) => valueToAttribute(key, value));
169+
162170
const log: Log = {
163171
severityText: level,
164172
body: {
165173
stringValue: message,
166174
},
167-
attributes: attributes,
175+
attributes,
168176
timeUnixNano: `${new Date().getTime().toString()}000000`,
169177
traceId: scope.getPropagationContext().traceId,
178+
severityNumber,
170179
};
171180

172181
const maybeSeverityNumber = SEVERITY_TEXT_TO_SEVERITY_NUMBER[level];
173-
if (maybeSeverityNumber !== undefined) {
182+
if (maybeSeverityNumber !== undefined && log.severityNumber === undefined) {
174183
log.severityNumber = maybeSeverityNumber;
175184
}
176185

packages/core/src/types-hoist/log.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type LogSeverityLevel = 'trace' | 'debug' | 'info' | 'warning' | 'error' | 'fatal' | 'critical';
1+
export type LogSeverityLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'critical';
22

33
export type LogAttributeValueType =
44
| {
@@ -22,7 +22,7 @@ export type LogAttribute = {
2222
export interface Log {
2323
/**
2424
* Allowed values are, from highest to lowest:
25-
* `critical`, `fatal`, `error`, `warning`, `info`, `debug`, `trace`.
25+
* `critical`, `fatal`, `error`, `warn`, `info`, `debug`, `trace`.
2626
*
2727
* The log level changes how logs are filtered and displayed.
2828
* Critical level logs are emphasized more than trace level logs.

0 commit comments

Comments
 (0)