1111use IteratorAggregate ;
1212use JsonSerializable ;
1313use Psr \Log \LoggerInterface ;
14+ use Psr \Log \NullLogger ;
1415use Throwable ;
1516use Traversable ;
1617
@@ -33,22 +34,20 @@ final class Profiler implements JsonSerializable, IteratorAggregate, Countable
3334 /** @var non-empty-string */
3435 private readonly string $ identifier ;
3536 private readonly Closure $ callback ;
36- private readonly ?LoggerInterface $ logger ;
3737 /** @var list<Span> */
3838 private array $ spans ;
3939
4040 /**
4141 * @param ?non-empty-string $identifier
4242 */
43- public function __construct (callable $ callback , ?string $ identifier = null , ? LoggerInterface $ logger = null )
43+ public function __construct (callable $ callback , ?string $ identifier = null , private LoggerInterface $ logger = new NullLogger () )
4444 {
4545 $ identifier ??= self ::generateLabel ();
4646 $ identifier = trim ($ identifier );
4747 '' !== $ identifier || throw new InvalidArgument ('The identifier must be a non-empty string. ' );
4848
4949 $ this ->identifier = $ identifier ;
5050 $ this ->callback = $ callback instanceof Closure ? $ callback : $ callback (...);
51- $ this ->logger = $ logger ;
5251 $ this ->reset ();
5352 }
5453
@@ -92,20 +91,20 @@ public function run(mixed ...$args): mixed
9291 public function profile (string $ label , mixed ...$ args ): mixed
9392 {
9493 try {
95- $ this ->logger ? ->info('Profiler [ ' .$ this ->identifier .'] starting profiling for label: ' .$ label .'. ' , ['identifier ' => $ this ->identifier , 'label ' => $ label ]);
94+ $ this ->logger ->info ('Profiler [ ' .$ this ->identifier .'] starting profiling for label: ' .$ label .'. ' , ['identifier ' => $ this ->identifier , 'label ' => $ label ]);
9695 $ start = Snapshot::now ('start ' );
9796 $ returnValue = ($ this ->callback )(...$ args );
9897 $ end = Snapshot::now ('end ' );
9998 $ span = new Span ($ label , $ start , $ end );
100- $ this ->logger ? ->info('Profiler [ ' .$ this ->identifier .'] ending profiling for label: ' .$ label .'. ' , [...['identifier ' => $ this ->identifier ], ...$ span ->toArray ()]);
99+ $ this ->logger ->info ('Profiler [ ' .$ this ->identifier .'] ending profiling for label: ' .$ label .'. ' , [...['identifier ' => $ this ->identifier ], ...$ span ->toArray ()]);
101100
102101 $ profiled = new Result ($ returnValue , $ span );
103102 $ this ->spans [] = $ profiled ->span ;
104103
105104 return $ profiled ->returnValue ;
106105
107106 } catch (Throwable $ exception ) {
108- $ this ->logger ? ->error('Profiler [ ' .$ this ->identifier .'] profiling aborted for label: ' .$ label .' due to an error in the executed code. ' , ['identifier ' => $ this ->identifier , 'label ' => $ label , 'exception ' => $ exception ]);
107+ $ this ->logger ->error ('Profiler [ ' .$ this ->identifier .'] profiling aborted for label: ' .$ label .' due to an error in the executed code. ' , ['identifier ' => $ this ->identifier , 'label ' => $ label , 'exception ' => $ exception ]);
109108
110109 throw $ exception ;
111110 }
0 commit comments