11import { Roarr , logLevels , type LogLevelName } from 'roarr' ;
2- import callsites from 'callsites' ;
32import { config } from '$lib/server/core/config' ;
43import type {
5- LoggerContext ,
6- LoggerContextWithError ,
7- LoggerLoggingMethodName ,
4+ ServerLoggerContext ,
5+ ServerLoggerContextWithError ,
6+ ServerLoggerLoggingMethodName ,
87} from './types' ;
98import { serializeError } from 'serialize-error' ;
109import { sentry } from '$lib/shared/sentry' ;
11- import { getTraceId } from '$lib/shared/sentry/utils' ;
1210import type { SeverityLevel } from '$lib/shared/sentry/types' ;
11+ import {
12+ enrichContextWithDebugInfo ,
13+ enrichLoggerContextWithSentryTraceId ,
14+ } from '$lib/shared/logging/utils' ;
1315
1416export const roarr = ( function ( ) {
15- const createLogger = ( methodName : LoggerLoggingMethodName ) => {
17+ const createLogger = ( methodName : ServerLoggerLoggingMethodName ) => {
1618 return (
1719 message : string ,
18- context : LoggerContextWithError = { } ,
20+ context : ServerLoggerContextWithError = { } ,
1921 stackLevel : number = 3 ,
2022 ) => {
2123 if ( ! shouldBeLogged ( methodName ) ) {
@@ -24,11 +26,15 @@ export const roarr = (function () {
2426
2527 let contextClone = serializeErrorInContext ( context ) ;
2628 contextClone = enrichContextWithLogType ( contextClone ) ;
27- contextClone = enrichContextWithSentryTraceId ( contextClone ) ;
29+ contextClone = enrichLoggerContextWithSentryTraceId ( contextClone ) ;
2830
2931 Roarr [ methodName ] (
3032 config . roarr . isDebugContextShown
31- ? enrichContextWithDebugInfo ( contextClone , stackLevel )
33+ ? enrichContextWithDebugInfo (
34+ contextClone ,
35+ config . folders . root ,
36+ stackLevel ,
37+ )
3238 : contextClone ,
3339 message ,
3440 ) ;
@@ -44,9 +50,9 @@ export const roarr = (function () {
4450
4551 const roarrLoggingMethodNamesNoOnce = Object . keys ( Roarr ) . filter ( ( property ) =>
4652 Object . keys ( logLevels ) . includes ( property ) ,
47- ) as LoggerLoggingMethodName [ ] ;
53+ ) as ServerLoggerLoggingMethodName [ ] ;
4854 const roarrLoggingMethodNamesOnce = roarrLoggingMethodNamesNoOnce . map (
49- ( methodName ) => `${ methodName } Once` as LoggerLoggingMethodName ,
55+ ( methodName ) => `${ methodName } Once` as ServerLoggerLoggingMethodName ,
5056 ) ;
5157 const roarrLoggingMethodNames = [
5258 ...roarrLoggingMethodNamesNoOnce ,
@@ -58,10 +64,10 @@ export const roarr = (function () {
5864 return acc ;
5965 } ,
6066 { } as Record <
61- LoggerLoggingMethodName ,
67+ ServerLoggerLoggingMethodName ,
6268 (
6369 message : string ,
64- context ?: LoggerContextWithError ,
70+ context ?: ServerLoggerContextWithError ,
6571 stackLevel ?: number ,
6672 ) => void
6773 > ,
@@ -70,7 +76,7 @@ export const roarr = (function () {
7076 return roarrLogger ;
7177} ) ( ) ;
7278
73- function shouldBeLogged ( methodName : LoggerLoggingMethodName ) : boolean {
79+ function shouldBeLogged ( methodName : ServerLoggerLoggingMethodName ) : boolean {
7480 const requestedLogLevelName = methodName . replace ( 'Once' , '' ) as LogLevelName ;
7581 const requestedLogLevel = logLevels [ requestedLogLevelName ] ;
7682 const minLogLevel = logLevels [ config . roarr . minLogLevel as LogLevelName ] ;
@@ -79,11 +85,11 @@ function shouldBeLogged(methodName: LoggerLoggingMethodName): boolean {
7985}
8086
8187function serializeErrorInContext (
82- context : LoggerContextWithError ,
83- ) : LoggerContext {
88+ context : ServerLoggerContextWithError ,
89+ ) : ServerLoggerContext {
8490 const errorContext = context . error ;
8591 if ( ! errorContext || ! ( errorContext instanceof Error ) ) {
86- return { ...context } as LoggerContext ;
92+ return { ...context } as ServerLoggerContext ;
8793 }
8894
8995 return {
@@ -92,65 +98,17 @@ function serializeErrorInContext(
9298 } ;
9399}
94100
95- function enrichContextWithSentryTraceId ( context : LoggerContext ) : LoggerContext {
96- if ( ! sentry ) {
97- return { ...context } ;
98- }
99-
100- const traceId = getTraceId ( ) ;
101- if ( ! traceId ) {
102- return { ...context } ;
103- }
104-
105- return {
106- ...context ,
107- sentryTraceId : traceId ,
108- } ;
109- }
110-
111- function enrichContextWithDebugInfo (
112- context : LoggerContext = { } ,
113- stackLevel : number = 3 ,
114- ) : LoggerContext {
115- return {
116- ...context ,
117- callName : getCallName ( stackLevel ) ,
118- fileName : getFileName ( stackLevel ) ,
119- } ;
120- }
121-
122- function enrichContextWithLogType ( context : LoggerContext ) : LoggerContext {
101+ function enrichContextWithLogType (
102+ context : ServerLoggerContext ,
103+ ) : ServerLoggerContext {
123104 return {
124105 logType : 'app' ,
125106 ...context ,
126107 } ;
127108}
128109
129- function getCallName ( stackLevel : number = 3 ) : string {
130- const typeName = callsites ( ) [ stackLevel ] ?. getTypeName ( ) ?? '' ;
131- const functionName =
132- callsites ( ) [ 3 ] ?. getFunctionName ( ) ??
133- callsites ( ) [ stackLevel ] ?. getMethodName ( ) ??
134- '' ;
135-
136- if ( typeName ) {
137- return `${ typeName } .${ functionName } ` ;
138- }
139-
140- return functionName ;
141- }
142-
143- function getFileName ( stackLevel : number = 3 ) : string {
144- const fileName =
145- callsites ( ) [ stackLevel ] ?. getFileName ( ) ??
146- callsites ( ) [ stackLevel ] ?. getEvalOrigin ( ) ??
147- '' ;
148-
149- return fileName . replace ( config . folders . root , '' ) ;
150- }
151-
152110function convertLogLevelNameRoarrToSentry (
153- methodName : LoggerLoggingMethodName ,
111+ methodName : ServerLoggerLoggingMethodName ,
154112) : SeverityLevel {
155113 const methodNameWithoutOnce = methodName . replace ( 'Once' , '' ) ;
156114
0 commit comments