Download the PHP package goletter/hyperf-exception-notify without Composer

On this page you can find all versions of the php package goletter/hyperf-exception-notify. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package hyperf-exception-notify

hyperf-exception-notify

Hyperf 异常通知组件:线上出现异常时,自动把异常信息、请求参数、堆栈推送到 钉钉 / 企业微信 / 飞书 群机器人,或写入日志。

环境要求

一、安装

发布后生成配置文件 config/autoload/exception_notify.php。

二、三步接入(以钉钉为例)

1. 创建群机器人

钉钉群 → 群设置 → 机器人 → 添加「自定义」机器人,安全设置任选:

复制 Webhook 地址里 access_token= 后面的部分,填到 EXCEPTION_NOTIFY_DINGTALK_TOKEN。

2. 配置 .env

默认只在 APP_ENV 为 production / prod 时推送。本地调试见「七、本地测试」。

3. 注册异常处理器

config/autoload/exceptions.php,放在第一个:

Hyperf 按顺序执行异常处理器,前面的处理器一旦调用 stopPropagation(),后面的就不会执行。ExceptionNotifyHandler 只负责上报,不修改响应、不阻止后续处理器,放在第一个才能保证每个异常都经过它。

完成。命令行(php bin/hyperf.php xxx)执行失败也会自动上报,无需额外配置。

三、各平台配置

可以同时推多个平台:EXCEPTION_NOTIFY_CHANNELS=dingTalk,weWork,log。没填 token 的平台会被自动跳过。

平台 渠道名 环境变量 消息格式 长度上限
钉钉 dingTalk EXCEPTION_NOTIFY_DINGTALK_TOKEN / _SECRET / _KEYWORD Markdown 20000
企业微信 weWork EXCEPTION_NOTIFY_WEWORK_TOKEN Markdown 4096 字节
飞书 feiShu EXCEPTION_NOTIFY_FEISHU_TOKEN / _SECRET / _KEYWORD 纯文本 30720
日志 log EXCEPTION_NOTIFY_LOG_LEVEL 输出到控制台 无

四、通知内容示例

json { "goods_id": 1024, "password": "**" }

0 /opt/www/app/Controller/OrderController.php(42): App\Service\OrderService->create(Array)

9 {main}

`

五、配置说明

所有配置都在 config/autoload/exception_notify.php,大部分可以用环境变量覆盖。

开关与范围

配置项 环境变量 默认值 说明
enabled EXCEPTION_NOTIFY_ENABLED true 总开关
enabled_cli EXCEPTION_NOTIFY_ENABLED_CLI true 命令行异常是否上报
env EXCEPTION_NOTIFY_ENV production,prod 哪些环境推送,逗号分隔,支持通配符,* 表示所有环境
dont_report - [] 不上报的异常类(含子类)
report_channels EXCEPTION_NOTIFY_CHANNELS log 推送到哪些渠道,逗号分隔

常见的不需要上报的异常:

推送方式

配置项 环境变量 默认值 说明
async EXCEPTION_NOTIFY_ASYNC true 是否通过异步队列推送;log 渠道始终同步
queue EXCEPTION_NOTIFY_QUEUE default 使用的队列池,对应 config/autoload/async_queue.php
format EXCEPTION_NOTIFY_FORMAT markdown markdown 为可读摘要;json 为全部采集数据
title EXCEPTION_NOTIFY_REPORT_TITLE [环境] 应用名 exception 消息标题

限流

配置项 环境变量 默认值 说明
rate_limiter.max_attempts EXCEPTION_NOTIFY_LIMIT 生产 1,其它 50 时间窗口内同一异常最多推送次数
rate_limiter.decay_seconds EXCEPTION_NOTIFY_DECAY 300 时间窗口(秒)

「同一异常」指异常类、文件、行号、消息完全相同。消息里带变量(如订单号)时,每条都会被视为不同异常。

敏感字段打码

对 Post、Query、Header 生效,不区分大小写,支持 * 通配符,嵌套数组也会处理。可按业务追加,例如 'id_card'、'*mobile*'。

六、手动上报

帮助函数

依赖注入

手动上报同样受 enabled、env、dont_report、限流约束。

上报队列任务、定时任务的异常

默认只自动上报 HTTP 请求和命令行的异常。异步队列任务、定时任务执行失败时,可以加一个监听器:

七、本地测试

.env 临时改为:

加一个测试路由:

访问后群里应收到消息。测试完记得把配置改回来。

收不到消息时按顺序检查:

  1. APP_ENV 是否在 EXCEPTION_NOTIFY_ENV 中
  2. EXCEPTION_NOTIFY_CHANNELS 是否包含该平台,token 是否已填写
  3. 异常是否在 dont_report 中,或者是否被其它异常处理器提前 stopPropagation()(处理器要放第一个)
  4. 是否被限流:同一异常在 decay_seconds 内已推送过
  5. 异步模式下队列消费进程是否在运行
  6. 机器人安全设置:关键词是否一致、加签密钥是否正确、IP 白名单是否包含服务器出口 IP
  7. 查看控制台日志中的 Exception notify failed 错误信息

八、进阶

事件

每次推送前后会分发事件,可用于统计或审计:

事件 时机 属性
Goletter\HyperfExceptionNotify\Events\ReportingEvent 推送前 channel、report(最终消息内容)
Goletter\HyperfExceptionNotify\Events\ReportedEvent 推送后 channel、result(平台返回结果)

采集器

配置项 collector 决定采集哪些信息:

采集器 内容 默认启用
ApplicationCollector 应用名、版本、环境 是
ExceptionBasicCollector 异常类、消息、代码、文件行号 是
ExceptionTraceCollector 堆栈(过滤 vendor) 是
RequestBasicCollector URL、方法、IP、路由、耗时 是
RequestPostCollector Post 参数(打码) 是
RequestQueryCollector Query 参数(打码) 是
RequestHeaderCollector 请求头(打码) 否
ExceptionContextCollector 出错位置前后的源码 否
RequestMiddlewareCollector 路由中间件 否
RequestFileCollector 上传文件信息 否
RequestCookieCollector Cookie(不打码) 否
RequestSessionCollector Session(不打码) 否
RequestServerCollector Server 参数 否
ChoreCollector 时间、内存峰值 否
PhpInfoCollector PHP 版本、SAPI 否

注意:markdown 格式只展示 Application / Exception Basic / Request Basic / Post / Query / Trace 等内容。其它采集器(包括自定义采集器)的数据只在 format=json 时出现。

自定义采集器:

需要拿到异常对象时,实现 ExceptionAwareContract 并使用 ExceptionAwareTrait,通过 $this->exception 访问。

消息处理管道(sanitizers)

每个渠道的 sanitizers 会在发送前依次处理消息内容,参数写在冒号后面,多个参数用逗号分隔:

可用的处理器:LengthLimitSanitizer、AppendContentSanitizer、PrependContentSanitizer、StrReplaceSanitizer、TrimSanitizer、ToMarkdownSanitizer、ToHtmlSanitizer、UrlEncodeSanitizer、FixPrettyJsonSanitizer、VarOutputSanitizer。

截断要放在追加关键词之前,否则关键词可能被截掉,导致机器人拒收。

九、升级注意(相对旧版)

  1. 不再默认推送全部渠道,只推 EXCEPTION_NOTIFY_CHANNELS / report_channels 中的渠道
  2. 默认环境改为 production,prod,本地默认不推送
  3. 修复 WeWork 驱动方法名、渠道名、配置 key(exception_notify)、pipes 改名为 sanitizers
  4. 推送默认走异步队列,请确保队列消费进程在运行
  5. composer.json 中 ConfigProvider 命名空间已修正为 Goletter\HyperfExceptionNotify\ConfigProvider
  6. 新增 mask_fields 配置;钉钉 sanitizers 顺序改为「先截断、再追加关键词」
  7. ExceptionNotifyHandler 建议放在异常处理器列表的第一个

重新发布配置后,请对照合并旧的 exception_notify.php。


All versions of hyperf-exception-notify with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
goletter/hyperf-utils Version ^1.0
guanguans/notify Version ^1.25
hyperf/async-queue Version ^3.1
hyperf/cache Version ^3.1
hyperf/config Version ^3.1
hyperf/di Version ^3.1
hyperf/exception-handler Version ^3.1
hyperf/framework Version ^3.1
hyperf/http-server Version ^3.1
hyperf/logger Version ^3.1
hyperf/redis Version ^3.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package goletter/hyperf-exception-notify contains the following files

Loading the files please wait ...