Skip to content

Commit 9b62a11

Browse files
committed
Create an util log function to log an error with Roarr and send it to Sentry
1 parent b1919d6 commit 9b62a11

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/lib/shared/roarr/client.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import type { SeverityLevel } from '../sentry/types';
1313

1414
export const roarr = (function () {
1515
const createLogger = (methodName: LoggerLoggingMethodName) => {
16-
return (message: string, context: LoggerContextWithError = {}) => {
16+
return (
17+
message: string,
18+
context: LoggerContextWithError = {},
19+
stackLevel: number = 3,
20+
) => {
1721
if (!shouldBeLogged(methodName)) {
1822
return;
1923
}
@@ -23,7 +27,7 @@ export const roarr = (function () {
2327

2428
Roarr[methodName](
2529
config.roarr.isDebugContextShown
26-
? enrichContextWithDebugInfo(contextClone)
30+
? enrichContextWithDebugInfo(contextClone, stackLevel)
2731
: contextClone,
2832
message,
2933
);
@@ -54,7 +58,11 @@ export const roarr = (function () {
5458
},
5559
{} as Record<
5660
LoggerLoggingMethodName,
57-
(message: string, context?: LoggerContextWithError) => void
61+
(
62+
message: string,
63+
context?: LoggerContextWithError,
64+
stackLevel?: number,
65+
) => void
5866
>,
5967
);
6068

@@ -101,18 +109,21 @@ function enrichContextWithSentryTraceId(context: LoggerContext): LoggerContext {
101109

102110
function enrichContextWithDebugInfo(
103111
context: LoggerContext = {},
112+
stackLevel: number = 3,
104113
): LoggerContext {
105114
return {
106115
...context,
107-
callName: getCallName(),
108-
fileName: getFileName(),
116+
callName: getCallName(stackLevel),
117+
fileName: getFileName(stackLevel),
109118
};
110119
}
111120

112-
function getCallName(): string {
113-
const typeName = callsites()[3]?.getTypeName() ?? '';
121+
function getCallName(stackLevel: number = 3): string {
122+
const typeName = callsites()[stackLevel]?.getTypeName() ?? '';
114123
const functionName =
115-
callsites()[3]?.getFunctionName() ?? callsites()[3]?.getMethodName() ?? '';
124+
callsites()[3]?.getFunctionName() ??
125+
callsites()[stackLevel]?.getMethodName() ??
126+
'';
116127

117128
if (typeName) {
118129
return `${typeName}.${functionName}`;
@@ -121,9 +132,11 @@ function getCallName(): string {
121132
return functionName;
122133
}
123134

124-
function getFileName(): string {
135+
function getFileName(stackLevel: number = 3): string {
125136
const fileName =
126-
callsites()[3]?.getFileName() ?? callsites()[3]?.getEvalOrigin() ?? '';
137+
callsites()[stackLevel]?.getFileName() ??
138+
callsites()[stackLevel]?.getEvalOrigin() ??
139+
'';
127140

128141
return fileName.replace(config.folders.root, '');
129142
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { sentry } from '$lib/shared/sentry';
2+
import type { SeverityLevel } from '@sentry/sveltekit';
3+
import { roarr } from '../client';
4+
5+
export function logError(error: Error, level: SeverityLevel = 'error'): void {
6+
roarr.error(error.message, { error }, 4);
7+
sentry?.captureException(error, { level });
8+
}

0 commit comments

Comments
 (0)