Send a weak notification every time the log is run :: error

Using Laravel 5, I want to receive a notification (if weak, but it does not matter) every time Log::error (or the like) is launched, by a direct call to this method or by default the ExceptionHandler calls the report method. I think I need to extend the default Laravel Log system, but I'm not sure. What is the (best) way for laravel to do this? (without changing all the Log::error calls in my code).

Firstly, it seemed to me that I only need to change the Log Facade, but it will not handle the ExceptionHandler (i.e., on 500 errors due to an unchecked exception). Another solution can add code directly to the ExceptionHandler , but it will not run if I report an error using Log::error() or in some other way ( app('logger')->error() , logger()->error() , etc.).

+8
source share
4 answers

Change: Violated with Laravel 5.6

The Log Facade is really just a wrapper for the base Monolog instance. The good news is that Monolog supports Slack. You just need to tell Monologue to use it.

With that said, everything can be configured in 3 lines of code.

 $monolog = \Log::getMonolog(); $slackHandler = new \Monolog\Handler\SlackHandler('your-token', '#your-channel', 'Monolog', true, null, \Monolog\Logger::ERROR); $monolog->pushHandler($slackHandler); 

Then, to launch it, you can either create your own service provider for it, or simply delete it in the AppServiceProvider boot AppServiceProvider .

You might want to take a look at the SlackHandler source code in case you need to use more options that the constructor uses.

Now whenever you \Log::error('some error'); , this error message will be sent to the configured Slack channel. Please note that this pop-up window means that it will be sent to the Slack channel for any error related to registration, error , critical , alert and emergency . Set the bubble parameter to false if you want it to only register error s.

+18
source

For Laravel 5.6 and higher:

Laravel supports a weak driver for registration starting from 5.6. Configure your free channel in config/logging.php as follows:

 'slack' => [ 'driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'), 'username' => 'Laravel Log', 'emoji' => ':boom:', 'level' => 'critical', ], 

Now all log messages that are of critical importance or higher will be automatically recorded in a free channel.

You can also enter the Slack channel specifically:

 Log::channel('slack')->info('Something happened!'); 

More information in the Laravel and Slack log of incoming webhooks .

+8
source

You can listen for the illuminate.log [String $level, String $message, Array $context] event illuminate.log [String $level, String $message, Array $context] . If $level is error , you will send a notification.

Define an event listener in EventServiceProvider

 protected $listen = [ 'illuminate.log' => [ 'App\Listeners\LogEventListener' ] ]; 

This means that Laravel fires LogEventListener when the illuminate.log event fires.

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 */ } } 
+6
source

Prefiero esta librería :)

https://github.com/jeremykenedy/slack-laravel

Es mas génerico and puedo dar mas formato todo el mensaje

https://slack.com/intl/es-pe/help/articles/202288908-format-your-messages

0
source

All Articles