@@ -36,6 +36,8 @@ protected function tearDown(): void
3636
3737 // Reset the current time.
3838 Time::setTestNow ();
39+
40+ service ('context ' )->clearAll (); // Clear any context data that may have been set during tests.
3941 }
4042
4143 public function testThrowsExceptionWithBadHandlerSettings (): void
@@ -438,4 +440,67 @@ public function testDetermineFileNoStackTrace(): void
438440
439441 $ this ->assertSame ($ expected , $ logger ->determineFile ());
440442 }
443+
444+ public function testLogsGlobalContext (): void
445+ {
446+ $ config = new LoggerConfig ();
447+ $ config ->logGlobalContext = true ;
448+
449+ $ logger = new Logger ($ config );
450+
451+ Time::setTestNow ('2026-02-18 12:00:00 ' );
452+
453+ service ('context ' )->set ('foo ' , 'bar ' );
454+
455+ $ expected = 'DEBUG - ' . Time::now ()->format ('Y-m-d ' ) . ' --> Test message {"foo":"bar"} ' ;
456+
457+ $ logger ->log ('debug ' , 'Test message ' );
458+
459+ $ logs = TestHandler::getLogs ();
460+
461+ $ this ->assertCount (1 , $ logs );
462+ $ this ->assertSame ($ expected , $ logs [0 ]);
463+ }
464+
465+ public function testDoesNotLogGlobalContext (): void
466+ {
467+ $ config = new LoggerConfig ();
468+ $ config ->logGlobalContext = false ;
469+
470+ $ logger = new Logger ($ config );
471+
472+ Time::setTestNow ('2026-02-18 12:00:00 ' );
473+
474+ service ('context ' )->set ('foo ' , 'bar ' );
475+
476+ $ expected = 'DEBUG - ' . Time::now ()->format ('Y-m-d ' ) . ' --> Test message ' ;
477+
478+ $ logger ->log ('debug ' , 'Test message ' );
479+
480+ $ logs = TestHandler::getLogs ();
481+
482+ $ this ->assertCount (1 , $ logs );
483+ $ this ->assertSame ($ expected , $ logs [0 ]);
484+ }
485+
486+ public function testDoesNotLogHiddenGlobalContext (): void
487+ {
488+ $ config = new LoggerConfig ();
489+ $ config ->logGlobalContext = true ;
490+
491+ $ logger = new Logger ($ config );
492+
493+ Time::setTestNow ('2026-02-18 12:00:00 ' );
494+
495+ service ('context ' )->setHidden ('secret ' , 'hidden value ' );
496+
497+ $ expected = 'DEBUG - ' . Time::now ()->format ('Y-m-d ' ) . ' --> Test message ' ;
498+
499+ $ logger ->log ('debug ' , 'Test message ' );
500+
501+ $ logs = TestHandler::getLogs ();
502+
503+ $ this ->assertCount (1 , $ logs );
504+ $ this ->assertSame ($ expected , $ logs [0 ]);
505+ }
441506}
0 commit comments