Skip to content

Commit a2c10e7

Browse files
committed
improve a bit
1 parent db263fe commit a2c10e7

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

packages/core/src/ourlogs.ts

+46-43
Original file line numberDiff line numberDiff line change
@@ -55,57 +55,60 @@ function addLog(log: Log): void {
5555
client.sendEnvelope(envelope).then(null, ex => console.error(ex));
5656
}
5757

58+
function valueToAttribute(key: string, value: unknown): LogAttribute {
59+
if (typeof value === 'number') {
60+
if(Number.isInteger(value)) {
61+
return {
62+
key,
63+
value: {
64+
intValue: value
65+
}
66+
}
67+
}
68+
return {
69+
key,
70+
value: {
71+
doubleValue: value
72+
}
73+
}
74+
} else if (typeof value === 'boolean') {
75+
return {
76+
key,
77+
value: {
78+
boolValue: value
79+
}
80+
}
81+
} else if (typeof value === 'string') {
82+
return {
83+
key,
84+
value: {
85+
stringValue: value
86+
}
87+
}
88+
} else {
89+
return {
90+
key,
91+
value: {
92+
stringValue: JSON.stringify(value)
93+
}
94+
}
95+
}
96+
}
97+
5898
/**
59-
* A utility function to be able to create methods like Sentry.info(...)
99+
* A utility function to be able to create methods like Sentry.info`...`
60100
*
61101
* The first parameter is bound with, e.g., const info = captureLog.bind(null, 'info')
62102
* The other parameters are in the format to be passed a template, Sentry.info`hello ${world}`
63103
*/
64-
export function captureLog(level: LogSeverityLevel, strings: string[], ...values: unknown[]): void {
104+
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+
65107
addLog({
66108
severityText: level,
67109
body: {
68-
stringValue: strings.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '' ),
110+
stringValue: message,
69111
},
70-
attributes: values.map<LogAttribute>((value, index) => {
71-
const key = `param${index}`;
72-
if (typeof value === 'number') {
73-
if(Number.isInteger(value)) {
74-
return {
75-
key,
76-
value: {
77-
intValue: value
78-
}
79-
}
80-
}
81-
return {
82-
key,
83-
value: {
84-
doubleValue: value
85-
}
86-
}
87-
} else if (typeof value === 'boolean') {
88-
return {
89-
key,
90-
value: {
91-
boolValue: value
92-
}
93-
}
94-
} else if (typeof value === 'string') {
95-
return {
96-
key,
97-
value: {
98-
stringValue: value
99-
}
100-
}
101-
} else {
102-
return {
103-
key,
104-
value: {
105-
stringValue: JSON.stringify(value)
106-
}
107-
}
108-
}
109-
}, {})
112+
attributes: values.map<LogAttribute>((value, index) => valueToAttribute(`param${index}`, value)),
110113
})
111114
}

0 commit comments

Comments
 (0)