@@ -505,4 +505,155 @@ public function testDoesNotLogHiddenGlobalContext(): void
505505 $ this ->assertCount (1 , $ logs );
506506 $ this ->assertSame ($ expected , $ logs [0 ]);
507507 }
508+
509+ public function testContextNotPassedToHandlersByDefault (): void
510+ {
511+ $ config = new LoggerConfig ();
512+ $ logger = new Logger ($ config );
513+
514+ $ logger ->log ('debug ' , 'Test message ' , ['foo ' => 'bar ' , 'baz ' => 'qux ' ]);
515+
516+ $ contexts = TestHandler::getContexts ();
517+
518+ $ this ->assertSame ([[]], $ contexts );
519+ }
520+
521+ public function testLogContextPassesNonInterpolatedKeysToHandlers (): void
522+ {
523+ $ config = new LoggerConfig ();
524+ $ config ->logContext = true ;
525+
526+ $ logger = new Logger ($ config );
527+
528+ $ logger ->log ('debug ' , 'Hello {name} ' , ['name ' => 'World ' , 'user_id ' => 42 ]);
529+
530+ $ contexts = TestHandler::getContexts ();
531+
532+ $ this ->assertArrayNotHasKey ('name ' , $ contexts [0 ]);
533+ $ this ->assertSame (42 , $ contexts [0 ]['user_id ' ]);
534+ }
535+
536+ public function testLogContextStripsInterpolatedKeysByDefault (): void
537+ {
538+ $ config = new LoggerConfig ();
539+ $ config ->logContext = true ;
540+
541+ $ logger = new Logger ($ config );
542+
543+ $ logger ->log ('debug ' , 'Hello {name} ' , ['name ' => 'World ' ]);
544+
545+ $ contexts = TestHandler::getContexts ();
546+
547+ $ this ->assertSame ([[]], $ contexts );
548+ }
549+
550+ public function testLogContextKeepsInterpolatedKeysWhenEnabled (): void
551+ {
552+ $ config = new LoggerConfig ();
553+ $ config ->logContext = true ;
554+ $ config ->logContextUsedKeys = true ;
555+
556+ $ logger = new Logger ($ config );
557+
558+ $ logger ->log ('debug ' , 'Hello {name} ' , ['name ' => 'World ' ]);
559+
560+ $ contexts = TestHandler::getContexts ();
561+
562+ $ this ->assertArrayHasKey ('name ' , $ contexts [0 ]);
563+ $ this ->assertSame ('World ' , $ contexts [0 ]['name ' ]);
564+ }
565+
566+ public function testLogContextNormalizesThrowable (): void
567+ {
568+ $ config = new LoggerConfig ();
569+ $ config ->logContext = true ;
570+
571+ $ logger = new Logger ($ config );
572+
573+ try {
574+ throw new RuntimeException ('Something went wrong ' , 42 );
575+ } catch (RuntimeException $ e ) {
576+ $ logger ->log ('error ' , 'An error occurred ' , ['exception ' => $ e ]);
577+ }
578+
579+ $ contexts = TestHandler::getContexts ();
580+
581+ $ this ->assertArrayHasKey ('exception ' , $ contexts [0 ]);
582+
583+ $ normalized = $ contexts [0 ]['exception ' ];
584+
585+ $ this ->assertSame (RuntimeException::class, $ normalized ['class ' ]);
586+ $ this ->assertSame ('Something went wrong ' , $ normalized ['message ' ]);
587+ $ this ->assertSame (42 , $ normalized ['code ' ]);
588+ $ this ->assertArrayHasKey ('file ' , $ normalized );
589+ $ this ->assertArrayHasKey ('line ' , $ normalized );
590+ $ this ->assertArrayNotHasKey ('trace ' , $ normalized );
591+ }
592+
593+ public function testLogContextNormalizesThrowableWithTrace (): void
594+ {
595+ $ config = new LoggerConfig ();
596+ $ config ->logContext = true ;
597+ $ config ->logContextTrace = true ;
598+
599+ $ logger = new Logger ($ config );
600+
601+ try {
602+ throw new RuntimeException ('Something went wrong ' );
603+ } catch (RuntimeException $ e ) {
604+ $ logger ->log ('error ' , 'An error occurred ' , ['exception ' => $ e ]);
605+ }
606+
607+ $ contexts = TestHandler::getContexts ();
608+
609+ $ this ->assertArrayHasKey ('exception ' , $ contexts [0 ]);
610+ $ this ->assertArrayHasKey ('trace ' , $ contexts [0 ]['exception ' ]);
611+ $ this ->assertIsString ($ contexts [0 ]['exception ' ]['trace ' ]);
612+ }
613+
614+ public function testLogContextNormalizesInterpolatedThrowableWhenUsedKeysEnabled (): void
615+ {
616+ $ config = new LoggerConfig ();
617+ $ config ->logContext = true ;
618+ $ config ->logContextUsedKeys = true ;
619+
620+ $ logger = new Logger ($ config );
621+
622+ try {
623+ throw new RuntimeException ('Something went wrong ' );
624+ } catch (RuntimeException $ e ) {
625+ $ logger ->log ('error ' , '[ERROR] {exception} ' , ['exception ' => $ e ]);
626+ }
627+
628+ $ contexts = TestHandler::getContexts ();
629+
630+ $ this ->assertArrayHasKey ('exception ' , $ contexts [0 ]);
631+
632+ $ normalized = $ contexts [0 ]['exception ' ];
633+
634+ $ this ->assertIsArray ($ normalized );
635+ $ this ->assertSame (RuntimeException::class, $ normalized ['class ' ]);
636+ $ this ->assertSame ('Something went wrong ' , $ normalized ['message ' ]);
637+ }
638+
639+ public function testLogContextDisabledStillAllowsGlobalContext (): void
640+ {
641+ $ config = new LoggerConfig ();
642+ $ config ->logContext = false ;
643+ $ config ->logGlobalContext = true ;
644+
645+ $ logger = new Logger ($ config );
646+
647+ Time::setTestNow ('2026-02-18 12:00:00 ' );
648+
649+ service ('context ' )->set ('request_id ' , 'abc123 ' );
650+
651+ $ logger ->log ('debug ' , 'Test message ' , ['extra ' => 'data ' ]);
652+
653+ $ contexts = TestHandler::getContexts ();
654+
655+ $ this ->assertArrayNotHasKey ('extra ' , $ contexts [0 ]);
656+ $ this ->assertArrayHasKey ('_ci_context ' , $ contexts [0 ]);
657+ $ this ->assertSame (['request_id ' => 'abc123 ' ], $ contexts [0 ]['_ci_context ' ]);
658+ }
508659}
0 commit comments