zkwbbr/whoops-helper
Composer 安装命令:
composer require zkwbbr/whoops-helper
包简介
Log error once and notify admin (via email or push notification)
README 文档
README
On production mode, log an error message only once. Optionally, notify admins via email or Pushover. On development mode, show Whoops Pretty Page.
Install
Install via composer as zkwbbr/whoops-helper
Sample Usage
Using FileSystem logger
Put the ff. code on top of your script.
<?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();
Note: Using email and Pushover notifications are optional. You can just remove them from the sample code above if you don't want to use them.
Using PDO logger
You can use any PDO database (e.g., MySQL, PostgreSQL, SQLite)
Create a table with the ff. columns:
createdOnDATETIMEhashVARCHAR (10)messageTEXT (length depends on how big your log message is)
Make hash column UNIQUE if you want
Replace the $logger adapter (line 52 from the above sample) with the ff.
$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);
zkwbbr/whoops-helper 适用场景与选型建议
zkwbbr/whoops-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「email」 「error handling」 「error」 「pushover」 「push notification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zkwbbr/whoops-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zkwbbr/whoops-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zkwbbr/whoops-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Error Handler module that captures and displays all throwable errors in a given format, making debugging easier and more efficient
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Error and Exception override and observers.
Error handler based on Booboo with HTML and JSON support
Error Share App, for handle error page
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-26