zkwbbr/whoops-helper

记录一次错误并通知管理员(通过电子邮件或推送通知)

v4.0.2 2021-02-07 22:05 UTC

This package is auto-updated.

Last update: 2024-09-08 05:52:12 UTC


README

在生产模式下,仅记录一次错误信息。可选地,通过电子邮件或Pushover通知管理员。在开发模式下,显示Whoops Pretty Page。

安装

通过composer以zkwbbr/whoops-helper的方式安装

示例用法

使用FileSystem记录器

将以下代码放在您的脚本顶部。

<?php

\error_reporting(E_ALL);

use Zkwbbr\WhoopsHelper;

// the following constants are arbitrary and not required
define('APP_DEV_MODE', true); // set to false in production
define('APP_ADMIN_EMAIL', 'admin@example.com');
define('APP_LOG_DIR', '/path/to/logs/');
define('APP_LOG_TIME_ZONE', 'UTC');
define('APP_URL', 'example.com');
define('APP_SMTP_HOST', 'example.com');
define('APP_SMTP_USER', 'user');
define('APP_SMTP_PASS', 'pass');
define('APP_SMTP_PORT', '25');
define('APP_SMTP_ENCR', 'TLS');
define('APP_PUSHOVER_APP_KEY', 'example');
define('APP_PUSHOVER_USER_KEY', 'example');

$whoops = new \Whoops\Run;

if (APP_DEV_MODE) {

    \ini_set('display_errors', '1');

    $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);

} else {

    ini_set('display_errors', '0');

    $whoops->pushHandler(function ($ex) {

        $logger = new WhoopsHelper\Logger\Filesystem\FileSytemLogger(APP_LOG_DIR, APP_LOG_TIME_ZONE);

        $handler = new WhoopsHelper\Handler($ex, $logger, APP_LOG_TIME_ZONE);

        // optionally remove sensitive info from $_SERVER var in the log
        $sampleSensitiveInfo = ['PHP_AUTH_PW'];
        $handler->setItemsToRemoveFromServerVar($sampleSensitiveInfo);

        $handler->process();

        // ----------------------------------------------
        // optionally send email on first instance of an error
        // ----------------------------------------------

        $smtpServers = [
            0 => (new \MetaRush\EmailFallback\Server)
                ->setHost(APP_SMTP_HOST)
                ->setUser(APP_SMTP_USER)
                ->setPass(APP_SMTP_PASS)
                ->setPort(APP_SMTP_PORT)
                ->setEncr(APP_SMTP_ENCR)
        ];

        $mailBuilder = (new \MetaRush\EmailFallback\Builder)
            ->setServers($smtpServers)
            ->setAdminEmails([APP_ADMIN_EMAIL])
            ->setNotificationFromEmail('noreply@example.com')
            ->setFromEmail('noreply@example.com')
            ->setAppName(APP_URL)
            ->setTos([APP_ADMIN_EMAIL]);

        $action = new WhoopsHelper\Actions\Email\Action(APP_URL, $mailBuilder);

        $handler->invokeActionOnEvent(
            WhoopsHelper\Handler::LOGGED_EVENT,
            $action
        );

        // ----------------------------------------------
        // optionally send Pushover notification on first instance of an error
        // ----------------------------------------------

        $action = new WhoopsHelper\Actions\Pushover\Action(
            APP_URL, APP_PUSHOVER_APP_KEY, APP_PUSHOVER_USER_KEY
        );

        $handler->invokeActionOnEvent(
            WhoopsHelper\Handler::LOGGED_EVENT,
            $action
        );

        // ----------------------------------------------
        // send response to user/client
        // ----------------------------------------------

        \header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
        \header('Status: 500 Internal Server Error');

        echo 'Sorry an error occurred, our admins have been notified';
    });
}

$whoops->register();

注意:使用电子邮件和Pushover通知是可选的。如果您不想使用它们,可以直接从上面的示例代码中删除。

使用PDO记录器

您可以使用任何PDO数据库(例如,MySQL、PostgreSQL、SQLite)

创建以下列的表

  • createdOn DATETIME
  • hash VARCHAR (10)
  • message TEXT(长度取决于您的日志信息大小)

如果需要,使hash列唯一

将上面的示例(第52行)中的$logger适配器替换为以下内容。

$dataMapper = (new \MetaRush\DataMapper\DataMapper)
    ->setDsn('mysql:host=locolhost;dbname=you_db_name')
    ->setDbUser('your_db_user')
    ->setDbPass('your_db_pass')
    ->build();

$logger = new WhoopsHelper\Logger\Pdo\PdoLogger($dataMapper, 'your_db_table', APP_LOG_TIME_ZONE);