laravel 每次触发 Log::error 时发送一个 slack 通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32354102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send a slack notification every time Log::error is triggered
提问by rap-2-h
Using Laravel 5, I want to be notified (on slack but it does not matter) every time Log::error
(or similar) is triggered, by a direct call to this method or by the default ExceptionHandler
calling report
method. I think I have to extends default Laravel's Log system but I'm not sure. What's the (best) "laravel way" to do this ?(without changing every Log::error
calls in my whole code).
使用 Laravel 5,我希望在每次Log::error
(或类似)被触发时(通过直接调用此方法或通过默认ExceptionHandler
调用report
方法)得到通知(在 slack 上但无所谓)。我想我必须扩展默认的 Laravel 日志系统,但我不确定。执行此操作的(最佳)“laravel 方式”是什么?(无需更改Log::error
我整个代码中的每个调用)。
First, I thought I only have to change Log Facade to one other but it will not handle the ExceptionHandler
(i.e. on 500 errors due to uncaught Exception). One other solution could be add some code directly in the ExceptionHandler
, but it will not be triggered if I report an error by Log::error()
or some other way (app('logger')->error()
, logger()->error()
, etc.).
首先,我认为我只需要将 Log Facade 更改为另一个,但它不会处理ExceptionHandler
(即由于未捕获的异常导致 500 个错误)。另一种解决方案可能是直接在添加一些代码ExceptionHandler
,但如果我报告错误,否则它将不会被触发Log::error()
或其他方式(app('logger')->error()
,logger()->error()
,等)。
回答by user1669496
Edit: Broken as of Laravel 5.6
编辑:从 Laravel 5.6 开始损坏
The Log
facade is really just a wrapper for an underlying instance of Monolog
. The good news is Monolog comes with support for Slack. You just need to tell Monolog to use it.
该Log
门面实际上只是一个基本的实例的包装Monolog
。好消息是 Monolog 支持 Slack。你只需要告诉 Monolog 使用它。
With that said, everything can be setup in 3 lines of code.
话虽如此,一切都可以在 3 行代码中设置。
$monolog = \Log::getMonolog();
$slackHandler = new \Monolog\Handler\SlackHandler('your-token', '#your-channel', 'Monolog', true, null, \Monolog\Logger::ERROR);
$monolog->pushHandler($slackHandler);
Then to get this running, you can either create your own service provider for it or just drop it in AppServiceProvider
's boot
method.
然后为了让它运行,你可以为它创建你自己的服务提供者,或者只是将它放入AppServiceProvider
的boot
方法中。
You may want to look at the source code for SlackHandler
just in case there are more options the constructor takes which you would need to use.
您可能需要查看源代码,SlackHandler
以防万一构造函数需要使用更多选项。
Now whenever you \Log::error('some error');
, that error's message will be sent to the Slack channel you have setup. Please note this "bubbles up" which means it will be sent to the Slack channel for any logging done error and up, error
, critical
, alert
, and emergency
. Set the bubble
parameter to false
if you only want it to log error
s.
现在,无论何时\Log::error('some error');
,该错误消息都会发送到您设置的 Slack 频道。请注意这个“冒泡”,这意味着它将被发送到 Slack 通道,以获取任何日志记录完成错误和 up error
、critical
、alert
、 和emergency
。如果您只希望它记录s bubble
,false
请将参数设置为error
。
回答by Sapnesh Naik
For Laravel 5.6 and above:
对于 Laravel 5.6 及更高版本:
Laravel suppports slack driver for logging beginning with 5.6. Configure your slack channel in config/logging.php
like so:
Laravel 从 5.6 开始支持日志记录的 slack 驱动程序。config/logging.php
像这样配置你的松弛通道:
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],
Now all the log messages which have an importance critical or above will automatically logged to slack channel.
现在,所有具有关键或以上重要性的日志消息都将自动记录到 slack 通道。
You can also log to slack channel specifically:
您还可以专门登录到 slack 频道:
Log::channel('slack')->info('Something happened!');
More info in Laravel loggingand Slack incoming webhooks.
回答by Maxim Lanin
You can listen to illuminate.log [String $level, String $message, Array $context]
event. If $level
equals error
, you will send notification.
您可以收听illuminate.log [String $level, String $message, Array $context]
事件。如果$level
等于error
,您将发送通知。
Define event listener in your EventServiceProvider
定义事件侦听器 EventServiceProvider
protected $listen = [
'illuminate.log' => [
'App\Listeners\LogEventListener'
]
];
This will tell Laravel to fire LogEventListener
when illuminate.log
event is fired.
这会告诉Laravel火灾LogEventListener
时,illuminate.log
事件被触发。
Then create this listener:
然后创建这个监听器:
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class LogEventListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param DeployDisabledEvent $event
* @return void
*/
public function handle($event)
{
if ($event->type == 'error') {
$this->notifyViaSlack($event->message, $event->context);
}
}
/**
* Send Slack notification.
*
* @param string $message
* @param string $context
* @return void
*/
protected function notifyViaSlack($message, $context)
{
/*
* Slack notification logic
*/
}
}