Skip to content

Commit 72599d9

Browse files
committed
Allow to log errors using Roarr logger
1 parent 64e4910 commit 72599d9

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"posthog-js": "1.102.0",
6969
"posthog-node": "3.6.1",
7070
"roarr": "7.21.0",
71+
"serialize-error": "11.0.3",
7172
"sveltekit-flash-message": "2.3.0",
7273
"sveltekit-superforms": "1.12.0",
7374
"zod": "3.22.4"

src/lib/shared/roarr/client.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import { Roarr, logLevels, type LogLevelName } from 'roarr';
22
import callsites from 'callsites';
33
import { config } from '$lib/server/core/config';
4-
import type { JsonObject, LoggerLoggingMethodName } from './types';
4+
import type {
5+
LoggerContext,
6+
LoggerContextWithError,
7+
LoggerLoggingMethodName,
8+
} from './types';
9+
import { serializeError } from 'serialize-error';
510

611
export const roarr = (function () {
712
const createLogger = (methodName: LoggerLoggingMethodName) => {
8-
return (message: string, context: JsonObject = {}) => {
13+
return (message: string, context: LoggerContextWithError = {}) => {
914
if (!shouldBeLogged(methodName)) {
1015
return;
1116
}
1217

18+
const contextClone = { ...context } as LoggerContext;
19+
const errorContext = contextClone.error;
20+
if (errorContext && errorContext instanceof Error) {
21+
contextClone.error = serializeError(errorContext);
22+
}
23+
1324
Roarr[methodName](
1425
config.roarr.isDebugContextShown
15-
? enrichContextWithDebugInfo(context)
16-
: context,
26+
? enrichContextWithDebugInfo(contextClone)
27+
: contextClone,
1728
message,
1829
);
1930
};
@@ -36,7 +47,7 @@ export const roarr = (function () {
3647
},
3748
{} as Record<
3849
LoggerLoggingMethodName,
39-
(message: string, context?: JsonObject) => void
50+
(message: string, context?: LoggerContextWithError) => void
4051
>,
4152
);
4253

@@ -51,7 +62,9 @@ function shouldBeLogged(methodName: LoggerLoggingMethodName): boolean {
5162
return requestedLogLevel >= minLogLevel;
5263
}
5364

54-
function enrichContextWithDebugInfo(override: JsonObject = {}): JsonObject {
65+
function enrichContextWithDebugInfo(
66+
override: LoggerContext = {},
67+
): LoggerContext {
5568
return {
5669
callName: getCallName(),
5770
fileName: getFileName(),

src/lib/shared/roarr/types/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import type { LogLevelName } from 'roarr';
88
export type LoggerLoggingMethodName =
99
| LoggerLoggingMethodNameNoOnce
1010
| LoggerLoggingMethodNameOnce;
11+
1112
export type JsonObject = {
1213
[k: string]: JsonValue;
1314
};
14-
15-
type LoggerLoggingMethodNameNoOnce = LogLevelName;
16-
type LoggerLoggingMethodNameOnce = `${LoggerLoggingMethodNameNoOnce}Once`;
17-
18-
type JsonValue =
15+
export type JsonValue =
1916
| JsonObject
2017
| JsonValue[]
2118
| boolean
@@ -24,3 +21,17 @@ type JsonValue =
2421
| readonly JsonValue[]
2522
| null
2623
| undefined;
24+
25+
export interface LoggerContext extends JsonObject {
26+
error?: JsonValue;
27+
}
28+
29+
export interface LoggerContextWithError {
30+
error?: Error | JsonValue;
31+
// WARN: Other properties should not have an `Error` type, but I don't know
32+
// how to enforce it in combination with the type of `error` property.
33+
[k: string]: Error | JsonValue;
34+
}
35+
36+
type LoggerLoggingMethodNameNoOnce = LogLevelName;
37+
type LoggerLoggingMethodNameOnce = `${LoggerLoggingMethodNameNoOnce}Once`;

0 commit comments

Comments
 (0)