How can I configure Monolog to output PHP errors within the response, as would have been done without Monolog?
What I want to do is when, for example, a PHP E_ERROR occurs within a PHP page, that error message will be output to the response, and also passed to any other Handlers set for Monolog.
AFAIK, I might use StreamHandler and have it output to stdout, but don't know how to do this or if it will work as expected?
There are two variations I'd like the option of:
How could I achieve these? I don't even know how I can get Monolog to register itself as a handler for exceptions and errors. Would I need to write my own functions to pass to register_error_handler()
, register_exception_handler()
and register_shutdown_function()
?
Short version:
use Monolog\ErrorHandler;
$logger = new Logger('Logger Name');
ErrorHandler::register($logger);
Longer, more customizable version:
use Monolog\ErrorHandler;
$logger = new Logger('Logger Name');
$handler = new ErrorHandler($logger);
$handler->registerErrorHandler([], false);
$handler->registerExceptionHandler();
$handler->registerFatalHandler();