Skip to content

Commit c5eb7fe

Browse files
committed
linting, etc
1 parent a2c10e7 commit c5eb7fe

File tree

6 files changed

+62
-47
lines changed

6 files changed

+62
-47
lines changed

packages/core/src/exports.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } from './currentScopes';
22
import { DEBUG_BUILD } from './debug-build';
3+
import { captureLog } from './ourlogs';
34
import type { CaptureContext } from './scope';
45
import { closeSession, makeSession, updateSession } from './session';
56
import type {
@@ -24,7 +25,6 @@ import { timestampInSeconds } from './utils-hoist/time';
2425
import { GLOBAL_OBJ } from './utils-hoist/worldwide';
2526
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
2627
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
27-
import { captureLog } from './ourlogs';
2828

2929
/**
3030
* Captures an exception event and sends it to Sentry.
@@ -336,7 +336,6 @@ export function captureSession(end: boolean = false): void {
336336
_sendSessionUpdate();
337337
}
338338

339-
340339
/**
341340
* A utility to record a log with level 'INFO' and send it to sentry.
342341
*

packages/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export {
3131
addEventProcessor,
3232
_experimentalLogError,
3333
_experimentalLogInfo,
34-
_experimentalLogWarning
34+
_experimentalLogWarning,
3535
} from './exports';
3636
export {
3737
getCurrentScope,

packages/core/src/ourlogs.ts

+42-30
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ function addLog(log: Log): void {
2929
return;
3030
}
3131

32-
// if (!client.getOptions()._experiments?.logSupport) {
33-
// return;
34-
// }
32+
if (!client.getOptions()._experiments?.logSupport) {
33+
return;
34+
}
3535

3636
const globalScope = getGlobalScope();
3737
const dsn = client.getDsn();
@@ -41,74 +41,86 @@ function addLog(log: Log): void {
4141
trace_id: globalScope.getPropagationContext().traceId,
4242
public_key: dsn?.publicKey,
4343
},
44-
...(dsn ? {dsn: dsnToString(dsn)} : {}),
45-
}
46-
if(!log.traceId) {
44+
...(dsn ? { dsn: dsnToString(dsn) } : {}),
45+
};
46+
if (!log.traceId) {
4747
log.traceId = globalScope.getPropagationContext().traceId || '00000000-0000-0000-0000-000000000000';
4848
}
49-
if(!log.timeUnixNano) {
50-
log.timeUnixNano = `${(new Date()).getTime().toString()}000000`;
49+
if (!log.timeUnixNano) {
50+
log.timeUnixNano = `${new Date().getTime().toString()}000000`;
5151
}
5252

5353
const envelope = createEnvelope<LogEnvelope>(headers, [createLogEnvelopeItem(log)]);
5454

55-
client.sendEnvelope(envelope).then(null, ex => console.error(ex));
55+
// sendEnvelope should not throw
56+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
57+
client.sendEnvelope(envelope);
5658
}
5759

5860
function valueToAttribute(key: string, value: unknown): LogAttribute {
5961
if (typeof value === 'number') {
60-
if(Number.isInteger(value)) {
62+
if (Number.isInteger(value)) {
6163
return {
6264
key,
6365
value: {
64-
intValue: value
65-
}
66-
}
66+
intValue: value,
67+
},
68+
};
6769
}
6870
return {
6971
key,
7072
value: {
71-
doubleValue: value
72-
}
73-
}
73+
doubleValue: value,
74+
},
75+
};
7476
} else if (typeof value === 'boolean') {
7577
return {
7678
key,
7779
value: {
78-
boolValue: value
79-
}
80-
}
80+
boolValue: value,
81+
},
82+
};
8183
} else if (typeof value === 'string') {
8284
return {
8385
key,
8486
value: {
85-
stringValue: value
86-
}
87-
}
87+
stringValue: value,
88+
},
89+
};
8890
} else {
8991
return {
9092
key,
9193
value: {
92-
stringValue: JSON.stringify(value)
93-
}
94-
}
94+
stringValue: JSON.stringify(value),
95+
},
96+
};
9597
}
9698
}
9799

98100
/**
99101
* A utility function to be able to create methods like Sentry.info`...`
100102
*
101103
* The first parameter is bound with, e.g., const info = captureLog.bind(null, 'info')
102-
* The other parameters are in the format to be passed a template, Sentry.info`hello ${world}`
104+
* The other parameters are in the format to be passed a tagged template, Sentry.info`hello ${world}`
103105
*/
104106
export function captureLog(level: LogSeverityLevel, messages: string[] | string, ...values: unknown[]): void {
105-
const message = Array.isArray(messages) ? messages.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '') : messages;
106-
107+
const message = Array.isArray(messages)
108+
? messages.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '')
109+
: messages;
110+
const attributes = values.map<LogAttribute>((value, index) => valueToAttribute(`param${index}`, value));
111+
if (Array.isArray(messages)) {
112+
attributes.push({
113+
key: 'sentry.template',
114+
value: {
115+
stringValue: messages.map((s, i) => s + (i < messages.length - 1 ? `$param${i}` : '')).join(''),
116+
},
117+
});
118+
}
107119
addLog({
108120
severityText: level,
109121
body: {
110122
stringValue: message,
111123
},
112-
attributes: values.map<LogAttribute>((value, index) => valueToAttribute(`param${index}`, value)),
113-
})
124+
attributes: attributes,
125+
});
114126
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { LegacyCSPReport } from './csp';
55
import type { DsnComponents } from './dsn';
66
import type { Event } from './event';
77
import type { FeedbackEvent, UserFeedback } from './feedback';
8+
import type { Log } from './ourlogs';
89
import type { Profile, ProfileChunk } from './profiling';
910
import type { ReplayEvent, ReplayRecordingData } from './replay';
1011
import type { SdkInfo } from './sdkinfo';
1112
import type { SerializedSession, SessionAggregates } from './session';
1213
import type { SpanJSON } from './span';
13-
import { Log } from './ourlogs';
1414

1515
// Based on: https://develop.sentry.dev/sdk/envelopes/
1616

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export type {
117117
Log,
118118
LogAttribute,
119119
LogSeverityLevel,
120-
LogAttributeValueType
120+
LogAttributeValueType,
121121
} from './ourlogs';
122122
export type { SpanStatus } from './spanStatus';
123123
export type { TimedEvent } from './timedEvent';

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ import type { SeverityLevel } from './severity';
22

33
export type LogSeverityLevel = SeverityLevel | 'critical' | 'trace';
44

5-
export type LogAttributeValueType = {
6-
stringValue: string
7-
} | {
8-
intValue: number
9-
} | {
10-
boolValue: boolean
11-
} | {
12-
doubleValue: number
13-
}
5+
export type LogAttributeValueType =
6+
| {
7+
stringValue: string;
8+
}
9+
| {
10+
intValue: number;
11+
}
12+
| {
13+
boolValue: boolean;
14+
}
15+
| {
16+
doubleValue: number;
17+
};
1418

1519
export type LogAttribute = {
16-
key: string,
17-
value: LogAttributeValueType
20+
key: string;
21+
value: LogAttributeValueType;
1822
};
1923

2024
export interface Log {
@@ -48,7 +52,7 @@ export interface Log {
4852
* The message to be logged - for example, 'hello world' would become a log like '[INFO] hello world'
4953
*/
5054
body: {
51-
stringValue: string,
55+
stringValue: string;
5256
};
5357

5458
/**

0 commit comments

Comments
 (0)