-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcapture.ts
More file actions
52 lines (47 loc) · 2.05 KB
/
capture.ts
File metadata and controls
52 lines (47 loc) · 2.05 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
import { format } from 'node:util';
import type { Log, LogSeverityLevel, ParameterizedString, Scope } from '@sentry/core';
import { _INTERNAL_captureLog } from '@sentry/core';
/**
* Additional metadata to capture the log with.
*/
interface CaptureLogMetadata {
scope?: Scope;
}
type CaptureLogArgWithTemplate = [
messageTemplate: string,
messageParams: Array<unknown>,
attributes?: Log['attributes'],
metadata?: CaptureLogMetadata,
];
type CaptureLogArgWithoutTemplate = [
message: ParameterizedString,
attributes?: Log['attributes'],
metadata?: CaptureLogMetadata,
];
export type CaptureLogArgs = CaptureLogArgWithTemplate | CaptureLogArgWithoutTemplate;
/**
* 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.
*/
export function captureLog(level: LogSeverityLevel, ...args: CaptureLogArgs): void {
const [messageOrMessageTemplate, paramsOrAttributes, maybeAttributesOrMetadata, maybeMetadata] = args;
if (Array.isArray(paramsOrAttributes)) {
// type-casting here because from the type definitions we know that `maybeAttributesOrMetadata` is an attributes object (or undefined)
const attributes = { ...(maybeAttributesOrMetadata as Log['attributes'] | undefined) };
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 }, maybeMetadata?.scope);
} else {
_INTERNAL_captureLog(
{ level, message: messageOrMessageTemplate, attributes: paramsOrAttributes },
// type-casting here because from the type definitions we know that `maybeAttributesOrMetadata` is a metadata object (or undefined)
(maybeAttributesOrMetadata as CaptureLogMetadata | undefined)?.scope ?? maybeMetadata?.scope,
);
}
}