@@ -25,6 +25,57 @@ let isEnabled = false;
25
25
let realLogger : DebugLoggerInterface | null = null ;
26
26
let lastOptions : Omit < InitOptions , 'enabled' > | null = null ;
27
27
28
+ type BufferedLogEntry = {
29
+ level : DebugLogLevel ;
30
+ message : string ;
31
+ context ?: Record < string , unknown > ;
32
+ source ?: string ;
33
+ ts : number ;
34
+ } ;
35
+
36
+ const MAX_BUFFERED_LOGS = 200 ;
37
+ const preInitBuffer : BufferedLogEntry [ ] = [ ] ;
38
+
39
+ function pushBuffered ( level : DebugLogLevel , message : string , context ?: Record < string , unknown > , source ?: string ) : void {
40
+ preInitBuffer . push ( { level, message, context, source, ts : Date . now ( ) } ) ;
41
+ if ( preInitBuffer . length > MAX_BUFFERED_LOGS ) {
42
+ preInitBuffer . shift ( ) ;
43
+ }
44
+ }
45
+
46
+ function flushBuffered ( ) : void {
47
+ if ( ! realLogger || preInitBuffer . length === 0 ) {
48
+ return ;
49
+ }
50
+ for ( const entry of preInitBuffer ) {
51
+ const mergedContext = {
52
+ ...( entry . context || { } ) ,
53
+ __preInit : true ,
54
+ __preInitTs : entry . ts ,
55
+ } as Record < string , unknown > ;
56
+ switch ( entry . level ) {
57
+ case 'error' :
58
+ realLogger . error ( entry . message , mergedContext , entry . source ) ;
59
+ break ;
60
+ case 'warn' :
61
+ realLogger . warn ( entry . message , mergedContext , entry . source ) ;
62
+ break ;
63
+ case 'info' :
64
+ realLogger . info ( entry . message , mergedContext , entry . source ) ;
65
+ break ;
66
+ case 'debug' :
67
+ realLogger . debug ( entry . message , mergedContext , entry . source ) ;
68
+ break ;
69
+ case 'trace' :
70
+ realLogger . trace ( entry . message , mergedContext , entry . source ) ;
71
+ break ;
72
+ default :
73
+ break ;
74
+ }
75
+ }
76
+ preInitBuffer . length = 0 ;
77
+ }
78
+
28
79
async function ensureInitialized ( ) : Promise < void > {
29
80
try {
30
81
if ( ! isEnabled || realLogger ) {
@@ -40,6 +91,7 @@ async function ensureInitialized(): Promise<void> {
40
91
41
92
if ( logger ) {
42
93
realLogger = logger ;
94
+ flushBuffered ( ) ;
43
95
}
44
96
} catch ( error ) {
45
97
const message = 'Debug logger initialization failed' ;
@@ -113,6 +165,7 @@ export function initDebugLogger(options: InitOptions = {}): void {
113
165
} ) ;
114
166
if ( logger ) {
115
167
realLogger = logger ;
168
+ flushBuffered ( ) ;
116
169
}
117
170
} catch ( error ) {
118
171
try {
@@ -145,30 +198,35 @@ export function initDebugLogger(options: InitOptions = {}): void {
145
198
const baseDebugLogger : DebugLoggerInterface = {
146
199
debug ( message : string , context ?: Record < string , unknown > , source ?: string ) : void {
147
200
if ( ! realLogger ) {
201
+ pushBuffered ( 'debug' , message , context , source ) ;
148
202
return ;
149
203
}
150
204
realLogger . debug ( message , context , source ) ;
151
205
} ,
152
206
error ( message : string , context ?: Record < string , unknown > , source ?: string ) : void {
153
207
if ( ! realLogger ) {
208
+ pushBuffered ( 'error' , message , context , source ) ;
154
209
return ;
155
210
}
156
211
realLogger . error ( message , context , source ) ;
157
212
} ,
158
213
info ( message : string , context ?: Record < string , unknown > , source ?: string ) : void {
159
214
if ( ! realLogger ) {
215
+ pushBuffered ( 'info' , message , context , source ) ;
160
216
return ;
161
217
}
162
218
realLogger . info ( message , context , source ) ;
163
219
} ,
164
220
trace ( message : string , context ?: Record < string , unknown > , source ?: string ) : void {
165
221
if ( ! realLogger ) {
222
+ pushBuffered ( 'trace' , message , context , source ) ;
166
223
return ;
167
224
}
168
225
realLogger . trace ( message , context , source ) ;
169
226
} ,
170
227
warn ( message : string , context ?: Record < string , unknown > , source ?: string ) : void {
171
228
if ( ! realLogger ) {
229
+ pushBuffered ( 'warn' , message , context , source ) ;
172
230
return ;
173
231
}
174
232
realLogger . warn ( message , context , source ) ;
0 commit comments