Monolog: how to catch all errors and exceptions

孤人 提交于 2021-02-08 15:27:11

问题


I'm missing something really obvious.

How can I make monolog record all php errors, php user errors, and exceptions?

Before using monolog, I wrote my own functions which I passed to set_error_handler(), register_shutdown_function() and set_exception_handler(). Is there a way of doing this using Monolog's API, or do I have to the following?

  1. Write an error handler and exception handler which I pass to PHP's functions above
  2. In those handlers, call the appropriate Monolog functions such as Logger::addError(...) using a switch statement or similar

Surely there must be a Monolog API that does the above in a single call?


回答1:


Old question but since it was not yet answered - ErrorHandler will do exactly what you're after.

From The documentation:

ErrorHandler: The Monolog\ErrorHandler class allows you to easily register a Logger instance as an exception handler, error handler or fatal error handler.




回答2:


From the ErrorHandler class docs:

use Monolog\ErrorHandler;
$logger = new Logger('Logger Name');

ErrorHandler::register($logger);


来源:https://stackoverflow.com/questions/32693922/monolog-how-to-catch-all-errors-and-exceptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!