codegym/laravel-slack-output 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codegym/laravel-slack-output

Composer 安装命令:

composer require codegym/laravel-slack-output

包简介

Sends a message to Slack when something goes wrong with your Laravel application.

README 文档

README

Sends messages to Slack with your Laravel application.

This package provides:

  • Post Command

    Send message to slack with a Laravel command.

  • Stats Command

    Send stats about your Laravel app with this customizable command.

  • Exceptions handler

    Output to Slack useful information about exceptions when they occurred.

  • Failed jobs handler

    Get alerted when a job failed.

  • Scheduled commands reporting

    Keep an eye on the result of your scheduled commands.

Requirements

  • Laravel 5.1 or greater
  • PHP 5.5.9 or greater

Installation

You can install the package using the Composer package manager. You can install it by running this command in your project root:

composer require codegym/laravel-slack-output

You need to include the service provider and the facade in your Laravel app.

Add the service provider to the providers array in config/app.php:

'providers' => [
  ...
  CodeGym\SlackOutput\ServiceProvider::class,
],

and then add the facade to your aliases array:

'aliases' => [
  ...
  'SlackOutput' => CodeGym\SlackOutput\Facade\SlackOutput::class,
],

Publish the configuration file with:

php artisan vendor:publish --provider="CodeGym\SlackOutput\ServiceProvider"

You need to add the webhook URL to the configuration file in order for the package to post to Slack. Create an incoming webhook on your Slack account. Copy the webhook url and open config/slack-output.php and set the webhook url to endpoint.

If null is set for any, the package will fall back on the default settings set by the webhook.

Usage

Post Command

The command slack:post posts message to Slack. It can take as arguments:

  • message: the message to send
  • to: the channel or person to post to
  • attach: the attachment payload

You can find information about the attach argument here: https://api.slack.com/docs/attachments

You can call it by the running the command:

php artisan slack:post "Hello, I'm a bot" #channel

You can also call it in your Laravel app:

Artisan::call('slack:post', [
  'to' => "#api-output",
  'attach' => $someAttachment,
  'message' => "Hello, I'm a bot"
]);

Note the Artisan::call, the command will be executed in background and will not block the current request.

Stats command

The command slack:stats send useful stats about your app to slack.

You need to configure this command by setting in config/slack-output.php the Eloquent classes and dates you prefer.

You can add constraints to the classes to limit the number of counted data.

'classes' => [
	  \App\Models\User::class => [
		  'is_active' => true //optional constraint
	  ]
],

The dates array is the form 'name of the date' => Carbon::instance(). Like:

'dates' => [
	'yesterday' => \Carbon\Carbon::yesterday(),
	'last week' => \Carbon\Carbon::today()->subWeek(1)
]

To schedule this command every day, simple add to app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
  $schedule->command('slack:stats')->daily()
}

Exceptions handler

To report useful exception to Slack, open app/Exceptions/Handler.php, and transform it like:

use CodeGym\SlackOutput\Facade\SlackOutput;

...

public function report(Exception $e)
{
  if ($this->shouldReport($e)) {
    SlackOutput::exception($e);
  }

  parent::report($e);
}

This will only reports exceptions that are not in the $dontReport array in the same file.

Failed jobs handler

To report failed jobs to Slack, open app/Providers/AppServiceProvider.php, and transform it like:

use CodeGym\SlackOutput\Facade\SlackOutput;

...

public function boot()
{
  Queue::failing(function (JobFailed $job) {
    SlackOutput::jobFailed($job);
  });
}

Scheduled commands reporting

To report the output of scheduled commands to Slack, open app/Console/Kernel.php, and transform it like:

use CodeGym\SlackOutput\Facade\SlackOutput;

...

protected function schedule(Schedule $schedule)
{
  SlackOutput::scheduledCommand(
    $schedule->command('db:backup-auto')->daily()
  );
}

Contributing

If you have problems, found a bug or have a feature suggestion, please add an issue on GitHub. Pull requests are also welcomed!

License

This package is open-sourced software licensed under the MIT license

codegym/laravel-slack-output 适用场景与选型建议

codegym/laravel-slack-output 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.2k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「exception」 「command」 「jobs」 「slack」 「laravel 5.4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codegym/laravel-slack-output 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codegym/laravel-slack-output 我们能提供哪些服务?
定制开发 / 二次开发

基于 codegym/laravel-slack-output 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-03