stasadev/laravel-slack-notifier
Composer 安装命令:
composer require stasadev/laravel-slack-notifier
包简介
Send exceptions and dump variables to Slack
关键字:
README 文档
README
Laravel Slack Notifier
Send exceptions and dump variables to Slack.
use Stasadev\SlackNotifier\Facades\SlackNotifier; SlackNotifier::send(new \RuntimeException('Test exception')); SlackNotifier::send('Test message');
Installation
Install the package via composer:
composer require stasadev/laravel-slack-notifier
All env variables used by this package (only LOG_SLACK_WEBHOOK_URL is required):
APP_NAME=Laravel LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/ABC LOG_SLACK_CHANNEL= LOG_SLACK_EMOJI=:boom: LOG_SLACK_CACHE_SECONDS=0
How to get a webhook URL in the Slack API docs.
To temporarily disable all logging, simply comment out LOG_SLACK_WEBHOOK_URL or set it to an empty string or null.
Optionally publish the config file with:
php artisan vendor:publish --tag="slack-notifier"
Usage
To send a message to Slack, simply call SlackNotifier::send().
Report Exception
// In Laravel 11.x and later // bootstrap/app.php return Application::configure(basePath: dirname(__DIR__)) ->withExceptions(function (Exceptions $exceptions) { $exceptions->reportable(function (Throwable $e) { \Stasadev\SlackNotifier\Facades\SlackNotifier::send($e); }); })->create(); // In Laravel 8.x, 9.x, 10.x // app/Exceptions/Handler.php public function register(): void { $this->reportable(function (Throwable $e) { \Stasadev\SlackNotifier\Facades\SlackNotifier::send($e); }); } // In Laravel 7.x // app/Exceptions/Handler.php public function report(Throwable $exception) { if ($this->shouldReport($exception)) { \Stasadev\SlackNotifier\Facades\SlackNotifier::send($exception); } parent::report($exception); } // In Laravel 5.7.x, 5.8.x, 6.x // app/Exceptions/Handler.php public function report(Exception $exception) { if ($this->shouldReport($exception)) { \Stasadev\SlackNotifier\Facades\SlackNotifier::send($exception); } parent::report($exception); }
Dump Variable
use Stasadev\SlackNotifier\Facades\SlackNotifier; $variable = 'message'; // $variable = ['test' => 'array']; // $variable = new stdClass(); SlackNotifier::send($variable);
Using multiple webhooks
Use an alternative webhook, by specify extra ones in the config file.
// config/slack-notifier.php 'webhook_urls' => [ 'default' => 'https://hooks.slack.com/services/ABC', 'testing' => 'https://hooks.slack.com/services/DEF', ],
The webhook to be used can be chosen using the to function.
use Stasadev\SlackNotifier\Facades\SlackNotifier; SlackNotifier::to('testing')->send('Test message');
Using a custom webhooks
The to function also supports custom webhook URLs.
use Stasadev\SlackNotifier\Facades\SlackNotifier; SlackNotifier::to('https://custom-url.com')->send('Test message');
Sending message to another channel
You can send a message to a channel (use LOG_SLACK_CHANNEL) other than the default one for the webhook, by passing it to the channel function.
use Stasadev\SlackNotifier\Facades\SlackNotifier; SlackNotifier::channel('reminders')->send('Test message');
Slack bot customizing
Use username (use APP_NAME) and emoji (use LOG_SLACK_EMOJI) to make your messages unique, or override them right before sending.
use Stasadev\SlackNotifier\Facades\SlackNotifier; SlackNotifier::username('My Laravel Bot')->emoji(':tada:')->send('Test message');
Formatting
Extend the default Stasadev\SlackNotifier\SlackNotifierFormatter::class to format the messages however you like. Then simply replace the formatter key in the configuration file.
// config/slack-notifier.php 'formatter' => App\Formatters\CustomSlackNotifierFormatter::class,
Additional context in the message
Include additional context in a Slack message (use dont_flash to exclude sensitive info from context). It will be added as an attachment.
Exception stack trace filtering
Stack traces for exceptions in Laravel usually contain many lines, including framework files. Usually, you are only interested in tracking exception details in the application files.
You can filter it out with the dont_trace config option.
Caching the same exceptions
Sometimes a large group of exceptions is thrown, and you don't want to log each of them because they are the same.
Use LOG_SLACK_CACHE_SECONDS (uses Laravel cache under the hood) to suppress output for X seconds, or pass it to the cacheSeconds function.
use Stasadev\SlackNotifier\Facades\SlackNotifier; SlackNotifier::cacheSeconds(60)->send(new \RuntimeException('Test exception'));
Testing
composer test
Credits
Inspired by spatie/laravel-slack-alerts.
License
The MIT License (MIT). Please see License File for more information.
stasadev/laravel-slack-notifier 适用场景与选型建议
stasadev/laravel-slack-notifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「notifications」 「laravel」 「slack」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stasadev/laravel-slack-notifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stasadev/laravel-slack-notifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stasadev/laravel-slack-notifier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova package that adds a notification feed in your Nova app.
A Highly Opinionated Symfony3/4 Deployer Recipe
This bundle provides integration with the Slack API library, allowing you to interact with the Slack API from within your Symfony projects
Captures outgoing SMS notifications
Wrapper for Slack Web API
Alfabank REST API integration
统计信息
- 总下载量: 95
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-02