2121class ProcessHandler extends StreamHandler
2222{
2323 private Level $ reportIncrementLevel = Level::Error;
24+ private bool $ filenameSet = false ;
2425
2526 public function __construct (
2627 private readonly string $ directory ,
2728 private readonly ProcessExecutionManager $ processExecutionManager ,
2829 int |string |Level $ level = Level::Debug,
2930 ) {
30- parent ::__construct ($ this ->directory , $ level );
31+ // Initialize with php://memory as placeholder - actual file will be set via setFilename()
32+ parent ::__construct ('php://memory ' , $ level );
3133 }
3234
3335 /**
@@ -40,27 +42,33 @@ public function setReportIncrementLevel(string $level): void
4042
4143 public function hasFilename (): bool
4244 {
43- return $ this ->directory !== $ this -> url ;
45+ return $ this ->filenameSet ;
4446 }
4547
4648 public function setFilename (string $ filename ): void
4749 {
50+ $ this ->close ();
4851 $ this ->url = \sprintf ('%s/%s ' , $ this ->directory , $ filename );
52+ $ this ->filenameSet = true ;
4953 }
5054
5155 public function close (): void
5256 {
53- $ this ->url = $ this ->directory ;
5457 parent ::close ();
58+ $ this ->filenameSet = false ;
5559 }
5660
5761 public function getFilename (): ?string
5862 {
59- return $ this ->url ;
63+ return $ this ->filenameSet ? $ this -> url : null ;
6064 }
6165
62- public function write (LogRecord $ record ): void
66+ protected function write (LogRecord $ record ): void
6367 {
68+ if (!$ this ->filenameSet ) {
69+ // Skip writing if no filename has been set yet
70+ return ;
71+ }
6472 parent ::write ($ record );
6573 if ($ record ->level ->value >= $ this ->reportIncrementLevel ->value ) {
6674 $ this ->processExecutionManager ->increment ($ record ->level ->name );
0 commit comments