@@ -109,18 +109,16 @@ const path = require('node:path');
109109
110110 // Flush synchronously and read the output
111111 consumer . flushSync ( ) ;
112- consumer . end ( ) ;
113- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
114-
115- // Parse the JSON output
116- const parsed = JSON . parse ( output . trim ( ) ) ;
117- assert . strictEqual ( parsed . level , 'info' ) ;
118- assert . strictEqual ( parsed . msg , 'test message' ) ;
119- assert . strictEqual ( parsed . userId , 123 ) ;
120- assert . strictEqual ( typeof parsed . time , 'number' ) ;
121-
122- // Cleanup
123- fs . unlinkSync ( tmpfile ) ;
112+ consumer . end ( ( ) => {
113+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
114+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
115+ const parsed = JSON . parse ( output ) ;
116+ assert . strictEqual ( parsed . level , 'info' ) ;
117+ assert . strictEqual ( parsed . msg , 'test message' ) ;
118+ assert . strictEqual ( parsed . userId , 123 ) ;
119+ assert . strictEqual ( typeof parsed . time , 'number' ) ;
120+ fs . unlinkSync ( tmpfile ) ;
121+ } ) ;
124122}
125123
126124// Test JSONConsumer with additional fields
@@ -137,15 +135,16 @@ const path = require('node:path');
137135 logger . info ( { msg : 'with fields' } ) ;
138136
139137 consumer . flushSync ( ) ;
140- consumer . end ( ) ;
141- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
142- const parsed = JSON . parse ( output . trim ( ) ) ;
143- assert . strictEqual ( parsed . hostname , 'test-host' ) ;
144- assert . strictEqual ( parsed . pid , 12345 ) ;
145- assert . strictEqual ( parsed . msg , 'with fields' ) ;
146-
147- // Cleanup
148- fs . unlinkSync ( tmpfile ) ;
138+ consumer . end ( ( ) => {
139+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
140+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
141+ const parsed = JSON . parse ( output ) ;
142+ assert . strictEqual ( parsed . hostname , 'test-host' ) ;
143+ assert . strictEqual ( parsed . pid , 12345 ) ;
144+ assert . strictEqual ( parsed . msg , 'with fields' ) ;
145+ // Cleanup
146+ fs . unlinkSync ( tmpfile ) ;
147+ } ) ;
149148}
150149
151150// Test child logger bindings in output
@@ -162,15 +161,15 @@ const path = require('node:path');
162161 childLogger . info ( { msg : 'child log' , action : 'create' } ) ;
163162
164163 consumer . flushSync ( ) ;
165- consumer . end ( ) ;
166- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
167- const parsed = JSON . parse ( output . trim ( ) ) ;
168- assert . strictEqual ( parsed . requestId , 'xyz-789' ) ;
169- assert . strictEqual ( parsed . action , 'create ' ) ;
170- assert . strictEqual ( parsed . msg , 'child log ' ) ;
171-
172- // Cleanup
173- fs . unlinkSync ( tmpfile ) ;
164+ consumer . end ( ( ) => {
165+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
166+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
167+ const parsed = JSON . parse ( output ) ;
168+ assert . strictEqual ( parsed . requestId , 'xyz-789 ' ) ;
169+ assert . strictEqual ( parsed . action , 'create ' ) ;
170+ assert . strictEqual ( parsed . msg , 'child log' ) ;
171+ fs . unlinkSync ( tmpfile ) ;
172+ } ) ;
174173}
175174
176175// Test invalid log level
@@ -239,14 +238,14 @@ const path = require('node:path');
239238 logger . info ( 'simple message' ) ;
240239
241240 consumer . flushSync ( ) ;
242- consumer . end ( ) ;
243- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
244- const parsed = JSON . parse ( output . trim ( ) ) ;
245-
246- assert . strictEqual ( parsed . msg , 'simple message' ) ;
247- assert . strictEqual ( parsed . level , 'info' ) ;
248-
249- fs . unlinkSync ( tmpfile ) ;
241+ consumer . end ( ( ) => {
242+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
243+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
244+ const parsed = JSON . parse ( output ) ;
245+ assert . strictEqual ( parsed . msg , 'simple message' ) ;
246+ assert . strictEqual ( parsed . level , 'info' ) ;
247+ fs . unlinkSync ( tmpfile ) ;
248+ } ) ;
250249}
251250
252251// Test string message with fields
@@ -262,15 +261,15 @@ const path = require('node:path');
262261 logger . info ( 'user login' , { userId : 123 , ip : '127.0.0.1' } ) ;
263262
264263 consumer . flushSync ( ) ;
265- consumer . end ( ) ;
266- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
267- const parsed = JSON . parse ( output . trim ( ) ) ;
268-
269- assert . strictEqual ( parsed . msg , 'user login' ) ;
270- assert . strictEqual ( parsed . userId , 123 ) ;
271- assert . strictEqual ( parsed . ip , '127.0.0.1' ) ;
272-
273- fs . unlinkSync ( tmpfile ) ;
264+ consumer . end ( ( ) => {
265+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
266+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
267+ const parsed = JSON . parse ( output ) ;
268+ assert . strictEqual ( parsed . msg , 'user login' ) ;
269+ assert . strictEqual ( parsed . userId , 123 ) ;
270+ assert . strictEqual ( parsed . ip , '127.0.0.1' ) ;
271+ fs . unlinkSync ( tmpfile ) ;
272+ } ) ;
274273}
275274
276275// Test Error object serialization
@@ -288,18 +287,18 @@ const path = require('node:path');
288287 logger . error ( { msg : 'operation failed' , err } ) ;
289288
290289 consumer . flushSync ( ) ;
291- consumer . end ( ) ;
292- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
293- const parsed = JSON . parse ( output . trim ( ) ) ;
294-
295- // Error should be serialized with stack trace
296- assert . strictEqual ( parsed . msg , 'operation failed' ) ;
297- assert . strictEqual ( typeof parsed . err , 'object' ) ;
298- assert . strictEqual ( parsed . err . message , 'test error' ) ;
299- assert . strictEqual ( parsed . err . code , 'TEST_ERROR' ) ;
300- assert ( parsed . err . stack ) ;
301-
302- fs . unlinkSync ( tmpfile ) ;
290+ consumer . end ( ( ) => {
291+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
292+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
293+ const parsed = JSON . parse ( output ) ;
294+ // Error should be serialized with stack trace
295+ assert . strictEqual ( parsed . msg , 'operation failed' ) ;
296+ assert . strictEqual ( typeof parsed . err , 'object' ) ;
297+ assert . strictEqual ( parsed . err . message , 'test error' ) ;
298+ assert . strictEqual ( parsed . err . code , 'TEST_ERROR' ) ;
299+ assert ( parsed . err . stack ) ;
300+ fs . unlinkSync ( tmpfile ) ;
301+ } ) ;
303302}
304303
305304// Test Error as first argument
@@ -316,15 +315,15 @@ const path = require('node:path');
316315 logger . error ( err ) ; // Error as first arg
317316
318317 consumer . flushSync ( ) ;
319- consumer . end ( ) ;
320- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
321- const parsed = JSON . parse ( output . trim ( ) ) ;
322-
323- assert . strictEqual ( parsed . msg , 'boom' ) ; // message from error
324- assert . strictEqual ( typeof parsed . err , 'object' ) ;
325- assert ( parsed . err . stack ) ;
326-
327- fs . unlinkSync ( tmpfile ) ;
318+ consumer . end ( ( ) => {
319+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
320+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
321+ const parsed = JSON . parse ( output ) ;
322+ assert . strictEqual ( parsed . msg , 'boom' ) ; // message from error
323+ assert . strictEqual ( typeof parsed . err , 'object' ) ;
324+ assert ( parsed . err . stack ) ;
325+ fs . unlinkSync ( tmpfile ) ;
326+ } ) ;
328327}
329328
330329// Test child logger with parent fields merge
@@ -342,17 +341,17 @@ const path = require('node:path');
342341 childLogger . info ( 'request processed' , { duration : 150 } ) ; // log fields
343342
344343 consumer . flushSync ( ) ;
345- consumer . end ( ) ;
346- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
347- const parsed = JSON . parse ( output . trim ( ) ) ;
348-
349- // Merge order: consumer fields < bindings < log fields
350- assert . strictEqual ( parsed . service , 'api' ) ;
351- assert . strictEqual ( parsed . requestId , '123' ) ;
352- assert . strictEqual ( parsed . duration , 150 ) ;
353- assert . strictEqual ( parsed . msg , 'request processed' ) ;
354-
355- fs . unlinkSync ( tmpfile ) ;
344+ consumer . end ( ( ) => {
345+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
346+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
347+ const parsed = JSON . parse ( output ) ;
348+ // Merge order: consumer fields < bindings < log fields
349+ assert . strictEqual ( parsed . service , 'api' ) ;
350+ assert . strictEqual ( parsed . requestId , '123' ) ;
351+ assert . strictEqual ( parsed . duration , 150 ) ;
352+ assert . strictEqual ( parsed . msg , 'request processed' ) ;
353+ fs . unlinkSync ( tmpfile ) ;
354+ } ) ;
356355}
357356
358357// Test field override priority
@@ -370,15 +369,15 @@ const path = require('node:path');
370369 childLogger . info ( 'test' , { env : 'production' } ) ;
371370
372371 consumer . flushSync ( ) ;
373- consumer . end ( ) ;
374- const output = fs . readFileSync ( tmpfile , 'utf8' ) ;
375- const parsed = JSON . parse ( output . trim ( ) ) ;
376-
377- // Log fields should override everything
378- assert . strictEqual ( parsed . env , 'production' ) ;
379- assert . strictEqual ( parsed . version , '1.0' ) ;
380-
381- fs . unlinkSync ( tmpfile ) ;
372+ consumer . end ( ( ) => {
373+ const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
374+ assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
375+ const parsed = JSON . parse ( output ) ;
376+ // Log fields should override everything
377+ assert . strictEqual ( parsed . env , 'production' ) ;
378+ assert . strictEqual ( parsed . version , '1.0' ) ;
379+ fs . unlinkSync ( tmpfile ) ;
380+ } ) ;
382381}
383382
384383// Test multiple consumers (Qard's use case)
@@ -408,24 +407,23 @@ const path = require('node:path');
408407 logger . error ( { msg : 'error message' } ) ;
409408
410409 consumer1 . flushSync ( ) ;
411- consumer1 . end ( ) ;
412410 consumer2 . flushSync ( ) ;
411+ consumer1 . end ( ) ;
413412 consumer2 . end ( ) ;
414413
415414 const output1 = fs . readFileSync ( tmpfile1 , 'utf8' ) ;
416415 const lines1 = output1 . trim ( ) . split ( '\n' ) ;
417-
418416 const output2 = fs . readFileSync ( tmpfile2 , 'utf8' ) ;
419417 const lines2 = output2 . trim ( ) . split ( '\n' ) ;
420418
421- // Consumer1 should have all 4 logs (debug+)
419+ // Consumer1 dosya formatı satır başına log yazıyorsa 4 olmalı, değilse bu kontrolü güncelle.
422420 assert . strictEqual ( lines1 . length , 4 ) ;
423421 assert . strictEqual ( JSON . parse ( lines1 [ 0 ] ) . level , 'debug' ) ;
424422 assert . strictEqual ( JSON . parse ( lines1 [ 1 ] ) . level , 'info' ) ;
425423 assert . strictEqual ( JSON . parse ( lines1 [ 2 ] ) . level , 'warn' ) ;
426424 assert . strictEqual ( JSON . parse ( lines1 [ 3 ] ) . level , 'error' ) ;
427425
428- // Consumer2 should have only 2 logs (warn+)
426+ // Consumer2 dosya formatı satır başına log yazıyorsa 2 olmalı, değilse bu kontrolü güncelle.
429427 assert . strictEqual ( lines2 . length , 2 ) ;
430428 assert . strictEqual ( JSON . parse ( lines2 [ 0 ] ) . level , 'warn' ) ;
431429 assert . strictEqual ( JSON . parse ( lines2 [ 1 ] ) . level , 'error' ) ;
0 commit comments