hexa2k9/exception-handler

此包已被废弃且不再维护。未建议替代包。

PHP异常处理器,用于将异常发送到Slack频道

v2.0.3 2014-08-29 08:06 UTC

This package is auto-updated.

Last update: 2021-07-10 08:24:37 UTC


README

PHP异常处理器,用于将异常发送到Slack频道

安装

  • 确保您的PHP安装已加载curl
  • 在Slack上创建一个频道
  • 为您的Slack频道创建一个入站Webhook并复制集成令牌。
  • Packagist包添加到您的composer.json
  • 运行composer update

配置

在使用之前,您需要配置此类

ExceptionHandler::configure(
    array(
        'username'       => 'company',                  // Your Slack Subdomain (e.g. company.slack.com)
        'token'          => '<your token>',             // The Slack Integration Token
        'data_path'      => '/tmp',                     // The Path to store full Exception Traces in
        'webhookChannel' => '#exceptions',              // Your Slack Channel
        'webhookUser'    => 'exception',                // The Username who will post Messages
        'webhookIcon'    => ':ghost:',                  // The Icon for the Username (can be :ghost: or an URL)
        'hostname'       => php_uname('n'),             // The Hostname your Application is running on
        'version'        => '1.0.0',                    // Your Application Version
        'env'            => 'production'                // The Applications Environment (e.g. production or development)
    )
);

最后设置异常处理器

set_exception_handler(array('\ExceptionHandler', 'handleException'));

您将在频道中开始收到如下消息

chrisbookair.local/2.0.81@development: 文件 /Users/christian/Code/PhpstormProjects/api-v2/app/Classes/Util/GeneralUtility.php 第581行发生未捕获的异常 (代码: 8 - 跟踪: handleException.1395606690.trace.532f44a20d0cc.txt): Memcache::connect(): 服务器 127.0.0.1 (tcp 11211) 连接失败,拒绝连接 (61)

ExceptionHandler将终止当前应用程序的运行并返回一个json_encode()的消息

{
  "status": 500,
  "message": "Okay, Houston, we've had a problem here. -- Don't panic. The Team has been notified."
}

其他用法

默认情况下,此ExceptionHandler将处理未捕获的异常。如果您想发送处理异常的Slack消息,可以使用\ExceptionHandler::handleException($exception, true);来接收通知。在这种情况下,ExceptionHandler不会die()

您甚至可以使用ExceptionHandler仅发送通知。这有点奇怪:\ExceptionHandler::handleException(new \Exception('I\'m some text to send to Slack.'), true);