stasadev/laravel-slack-notifier

将异常和变量输出到 Slack

1.1.0 2024-04-20 17:09 UTC

This package is auto-updated.

Last update: 2024-09-20 19:19:54 UTC


README

Stand With Ukraine

Laravel Slack Notifier

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

将异常和变量输出到 Slack。

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::send(new \RuntimeException('Test exception'));
SlackNotifier::send('Test message');

安装

通过 composer 安装此包

composer require stasadev/laravel-slack-notifier

此包使用的所有环境变量(只需要 LOG_SLACK_WEBHOOK_URL

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

如何获取 webhook URL,请参阅 Slack API 文档中的 此处

要暂时禁用所有日志记录,只需取消注释 LOG_SLACK_WEBHOOK_URL 或将其设置为空字符串或 null

可选:通过以下方式发布 配置文件

php artisan vendor:publish --tag="slack-notifier"

用法

要向 Slack 发送消息,只需调用 SlackNotifier::send()

报告异常

// 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);
}

输出变量

use Stasadev\SlackNotifier\Facades\SlackNotifier;

$variable = 'message';
// $variable = ['test' => 'array'];
// $variable = new stdClass();

SlackNotifier::send($variable);

使用多个 webhook

通过在配置文件中指定额外的 webhook 来使用替代 webhook。

// config/slack-notifier.php

'webhook_urls' => [
    'default' => 'https://hooks.slack.com/services/ABC',
    'testing' => 'https://hooks.slack.com/services/DEF',
],

可以使用 to 函数选择要使用的 webhook。

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::to('testing')->send('Test message');

使用自定义 webhook

to 函数也支持自定义 webhook URL。

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::to('https://custom-url.com')->send('Test message');

向其他频道发送消息

您可以通过将频道名(使用 LOG_SLACK_CHANNEL)传递给 channel 函数,向 webhook 的默认频道以外的频道发送消息。

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::channel('reminders')->send('Test message');

Slack 机器人定制

使用 username(使用 APP_NAME)和 emoji(使用 LOG_SLACK_EMOJI)来使您的消息独特,或在发送前直接覆盖它们。

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::username('My Laravel Bot')->emoji(':tada:')->send('Test message');

格式化

扩展默认的 Stasadev\SlackNotifier\SlackNotifierFormatter::class 来按您喜欢的格式化消息。然后只需替换配置文件中的 formatter 键。

// config/slack-notifier.php

'formatter' => App\Formatters\CustomSlackNotifierFormatter::class,

消息中的附加上下文

在 Slack 消息中包含额外的 context(使用 dont_flash 排除 context 中的敏感信息)。它将作为附件添加。

异常堆栈跟踪过滤

Laravel 中的异常堆栈跟踪通常包含许多行,包括框架文件。通常,您只对跟踪应用程序文件中的异常细节感兴趣。您可以使用 dont_trace 配置选项过滤掉它。

缓存相同的异常

有时会抛出一组大量的异常,您不希望记录每个异常,因为它们是相同的。

使用 LOG_SLACK_CACHE_SECONDS(使用 Laravel 缓存)来抑制 X 秒内的输出,或将其传递给 cacheSeconds 函数。

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::cacheSeconds(60)->send(new \RuntimeException('Test exception'));

测试

composer test

致谢

灵感来源于 spatie/laravel-slack-alerts

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件