33require ( '../common' ) ;
44
55const assert = require ( 'node:assert' ) ;
6+ const common = require ( '../common' ) ;
67const { createLogger, Logger, LogConsumer, JSONConsumer, LEVELS , channels } = require ( 'node:logger' ) ;
78const fs = require ( 'node:fs' ) ;
89const os = require ( 'node:os' ) ;
@@ -111,7 +112,7 @@ const path = require('node:path');
111112 consumer . flushSync ( ) ;
112113 consumer . end ( ( ) => {
113114 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
114- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
115+ assert . notStrictEqual ( output , '' ) ;
115116 const parsed = JSON . parse ( output ) ;
116117 assert . strictEqual ( parsed . level , 'info' ) ;
117118 assert . strictEqual ( parsed . msg , 'test message' ) ;
@@ -137,12 +138,11 @@ const path = require('node:path');
137138 consumer . flushSync ( ) ;
138139 consumer . end ( ( ) => {
139140 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
140- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
141+ assert . notStrictEqual ( output , '' ) ;
141142 const parsed = JSON . parse ( output ) ;
142143 assert . strictEqual ( parsed . hostname , 'test-host' ) ;
143144 assert . strictEqual ( parsed . pid , 12345 ) ;
144145 assert . strictEqual ( parsed . msg , 'with fields' ) ;
145- // Cleanup
146146 fs . unlinkSync ( tmpfile ) ;
147147 } ) ;
148148}
@@ -163,7 +163,7 @@ const path = require('node:path');
163163 consumer . flushSync ( ) ;
164164 consumer . end ( ( ) => {
165165 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
166- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
166+ assert . notStrictEqual ( output , '' ) ;
167167 const parsed = JSON . parse ( output ) ;
168168 assert . strictEqual ( parsed . requestId , 'xyz-789' ) ;
169169 assert . strictEqual ( parsed . action , 'create' ) ;
@@ -240,7 +240,7 @@ const path = require('node:path');
240240 consumer . flushSync ( ) ;
241241 consumer . end ( ( ) => {
242242 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
243- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
243+ assert . notStrictEqual ( output , '' ) ;
244244 const parsed = JSON . parse ( output ) ;
245245 assert . strictEqual ( parsed . msg , 'simple message' ) ;
246246 assert . strictEqual ( parsed . level , 'info' ) ;
@@ -263,7 +263,7 @@ const path = require('node:path');
263263 consumer . flushSync ( ) ;
264264 consumer . end ( ( ) => {
265265 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
266- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
266+ assert . notStrictEqual ( output , '' ) ;
267267 const parsed = JSON . parse ( output ) ;
268268 assert . strictEqual ( parsed . msg , 'user login' ) ;
269269 assert . strictEqual ( parsed . userId , 123 ) ;
@@ -289,7 +289,7 @@ const path = require('node:path');
289289 consumer . flushSync ( ) ;
290290 consumer . end ( ( ) => {
291291 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
292- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
292+ assert . notStrictEqual ( output , '' ) ;
293293 const parsed = JSON . parse ( output ) ;
294294 // Error should be serialized with stack trace
295295 assert . strictEqual ( parsed . msg , 'operation failed' ) ;
@@ -317,7 +317,7 @@ const path = require('node:path');
317317 consumer . flushSync ( ) ;
318318 consumer . end ( ( ) => {
319319 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
320- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
320+ assert . notStrictEqual ( output , '' ) ;
321321 const parsed = JSON . parse ( output ) ;
322322 assert . strictEqual ( parsed . msg , 'boom' ) ; // message from error
323323 assert . strictEqual ( typeof parsed . err , 'object' ) ;
@@ -343,7 +343,7 @@ const path = require('node:path');
343343 consumer . flushSync ( ) ;
344344 consumer . end ( ( ) => {
345345 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
346- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
346+ assert . notStrictEqual ( output , '' ) ;
347347 const parsed = JSON . parse ( output ) ;
348348 // Merge order: consumer fields < bindings < log fields
349349 assert . strictEqual ( parsed . service , 'api' ) ;
@@ -371,7 +371,7 @@ const path = require('node:path');
371371 consumer . flushSync ( ) ;
372372 consumer . end ( ( ) => {
373373 const output = fs . readFileSync ( tmpfile , 'utf8' ) . trim ( ) ;
374- assert . notStrictEqual ( output , '' , 'Log file is empty!' ) ;
374+ assert . notStrictEqual ( output , '' ) ;
375375 const parsed = JSON . parse ( output ) ;
376376 // Log fields should override everything
377377 assert . strictEqual ( parsed . env , 'production' ) ;
@@ -408,28 +408,35 @@ const path = require('node:path');
408408
409409 consumer1 . flushSync ( ) ;
410410 consumer2 . flushSync ( ) ;
411- consumer1 . end ( ) ;
412- consumer2 . end ( ) ;
413-
414- const output1 = fs . readFileSync ( tmpfile1 , 'utf8' ) ;
415- const lines1 = output1 . trim ( ) . split ( '\n' ) ;
416- const output2 = fs . readFileSync ( tmpfile2 , 'utf8' ) ;
417- const lines2 = output2 . trim ( ) . split ( '\n' ) ;
418-
419- // Consumer1 dosya formatı satır başına log yazıyorsa 4 olmalı, değilse bu kontrolü güncelle.
420- assert . strictEqual ( lines1 . length , 4 ) ;
421- assert . strictEqual ( JSON . parse ( lines1 [ 0 ] ) . level , 'debug' ) ;
422- assert . strictEqual ( JSON . parse ( lines1 [ 1 ] ) . level , 'info' ) ;
423- assert . strictEqual ( JSON . parse ( lines1 [ 2 ] ) . level , 'warn' ) ;
424- assert . strictEqual ( JSON . parse ( lines1 [ 3 ] ) . level , 'error' ) ;
425-
426- // Consumer2 dosya formatı satır başına log yazıyorsa 2 olmalı, değilse bu kontrolü güncelle.
427- assert . strictEqual ( lines2 . length , 2 ) ;
428- assert . strictEqual ( JSON . parse ( lines2 [ 0 ] ) . level , 'warn' ) ;
429- assert . strictEqual ( JSON . parse ( lines2 [ 1 ] ) . level , 'error' ) ;
430-
431- fs . unlinkSync ( tmpfile1 ) ;
432- fs . unlinkSync ( tmpfile2 ) ;
411+
412+ let doneCount = 0 ;
413+ function done ( ) {
414+ doneCount ++ ;
415+ if ( doneCount === 2 ) {
416+ const output1 = fs . readFileSync ( tmpfile1 , 'utf8' ) ;
417+ const lines1 = output1 . trim ( ) . split ( '\n' ) . filter ( Boolean ) ;
418+ const output2 = fs . readFileSync ( tmpfile2 , 'utf8' ) ;
419+ const lines2 = output2 . trim ( ) . split ( '\n' ) . filter ( Boolean ) ;
420+
421+ // Consumer1 dosya formatı satır başına log yazıyorsa 4 olmalı, değilse bu kontrolü güncelle.
422+ assert . strictEqual ( lines1 . length , 4 ) ;
423+ assert . strictEqual ( JSON . parse ( lines1 [ 0 ] ) . level , 'debug' ) ;
424+ assert . strictEqual ( JSON . parse ( lines1 [ 1 ] ) . level , 'info' ) ;
425+ assert . strictEqual ( JSON . parse ( lines1 [ 2 ] ) . level , 'warn' ) ;
426+ assert . strictEqual ( JSON . parse ( lines1 [ 3 ] ) . level , 'error' ) ;
427+
428+ // Consumer2 dosya formatı satır başına log yazıyorsa 2 olmalı, değilse bu kontrolü güncelle.
429+ assert . strictEqual ( lines2 . length , 2 ) ;
430+ assert . strictEqual ( JSON . parse ( lines2 [ 0 ] ) . level , 'warn' ) ;
431+ assert . strictEqual ( JSON . parse ( lines2 [ 1 ] ) . level , 'error' ) ;
432+
433+ fs . unlinkSync ( tmpfile1 ) ;
434+ fs . unlinkSync ( tmpfile2 ) ;
435+ }
436+ }
437+
438+ consumer1 . end ( done ) ;
439+ consumer2 . end ( done ) ;
433440}
434441
435442// Test channels export
0 commit comments