File tree Expand file tree Collapse file tree 3 files changed +39
-1
lines changed Expand file tree Collapse file tree 3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 3
3
.gitattributes export-ignore
4
4
.gitignore export-ignore
5
5
.php_cs export-ignore
6
+ Makefile export-ignore
6
7
phpstan.neon export-ignore
7
8
phpunit.xml export-ignore
Original file line number Diff line number Diff line change @@ -55,6 +55,11 @@ final class ErrorHandler
55
55
*/
56
56
private $ emailCallback ;
57
57
58
+ /**
59
+ * @var callable
60
+ */
61
+ private $ errorLogCallback = '\\error_log ' ;
62
+
58
63
/**
59
64
* @var array<int, bool>
60
65
*/
@@ -99,6 +104,16 @@ public function __construct(callable $emailCallback)
99
104
$ this ->emailCallback = $ emailCallback ;
100
105
}
101
106
107
+ public function setErrorLogCallback (callable $ callback ): void
108
+ {
109
+ $ this ->errorLogCallback = $ callback ;
110
+ }
111
+
112
+ public function getErrorLogCallback (): callable
113
+ {
114
+ return $ this ->errorLogCallback ;
115
+ }
116
+
102
117
public function setAutoExit (bool $ autoExit ): void
103
118
{
104
119
$ this ->autoExit = $ autoExit ;
@@ -365,6 +380,8 @@ public function logException(Throwable $exception): void
365
380
return ;
366
381
}
367
382
383
+ $ errorLogCallback = $ this ->errorLogCallback ;
384
+
368
385
$ i = 0 ;
369
386
do {
370
387
$ output = \sprintf ('%s%s: %s in %s:%s%s%s ' ,
@@ -377,7 +394,7 @@ public function logException(Throwable $exception): void
377
394
$ this ->purgeTrace ($ exception ->getTraceAsString ())
378
395
);
379
396
380
- \error_log ($ output );
397
+ $ errorLogCallback ($ output );
381
398
382
399
++$ i ;
383
400
} while ($ exception = $ exception ->getPrevious ());
Original file line number Diff line number Diff line change @@ -306,4 +306,24 @@ public function test404SpecificExceptionForHeaders(): void
306
306
307
307
self ::assertStringContainsString ('404: Not Found ' , $ this ->errorHandler ->renderHtmlException (new RuntimeException ()));
308
308
}
309
+
310
+ public function testCanSetCustomErrorLogCallback (): void
311
+ {
312
+ $ this ->errorHandler ->setLogErrors (true );
313
+ self ::assertSame ('\\error_log ' , $ this ->errorHandler ->getErrorLogCallback ());
314
+
315
+ $ data = [];
316
+ $ customCallback = static function (string $ text ) use (& $ data ): void {
317
+ $ data [] = $ text ;
318
+ };
319
+
320
+ $ this ->errorHandler ->setErrorLogCallback ($ customCallback );
321
+
322
+ self ::assertSame ($ customCallback , $ this ->errorHandler ->getErrorLogCallback ());
323
+
324
+ $ this ->errorHandler ->logException ($ this ->exception );
325
+
326
+ self ::assertSame (0 , \filesize ($ this ->errorLog ));
327
+ self ::assertStringContainsString ($ this ->exception ->getMessage (), \var_export ($ data , true ));
328
+ }
309
329
}
You can’t perform that action at this time.
0 commit comments