As far as I understand, it is currently not possible to log all errors that Kirby catches to a file (or am I wrong?). This would be a great feature for production use.
If Kirby doesn’t want to add this in core, it could provide public access to the Whoops instance, so that one can easily add a handler and do whatever one wants with the caught errors.
In my case, I solved this by extending \Kirby\Cms\App:
use Kirby\Cms\App;
use Whoops\Handler\CallbackHandler;
use Whoops\Handler\Handler;
class MyApp extends App
{
public function enableErrorLogs()
{
// Create a new Whoops handler that simply logs the exception and continues running the next handler
$handler = new CallbackHandler(function ($exception, $inspector, $run) {
error_log(
sprintf(
'PHP error: %s (%s) | URI: %s | Trace: %s',
$exception->getMessage(),
$inspector->getExceptionName(),
isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'unknown',
$exception->getTraceAsString()
)
);
return Handler::DONE;
});
$this->whoops()->prependHandler($handler);
}
}
$kirby = new MyApp();
$kirby->enableErrorLogs();
echo $kirby->render();
If the whoops()
getter would be public, I could do this without extending the App class itself.
Related issues:
We have a work-in-progress pull request for this here: https://github.com/getkirby/kirby/pull/3952
Wow, awesome, thanks for taking action so fast!
It’s a coincidence as we were working on this before. :)