11import { Roarr , logLevels , type LogLevelName } from 'roarr' ;
22import callsites from 'callsites' ;
33import { 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
611export 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 ( ) ,
0 commit comments