├── youtube-thumb.png ├── slack-error-notifier.png ├── .gitignore ├── CHANGELOG.md ├── config └── slack_error_notifier.php ├── composer.json ├── src ├── RequestDataProcessor.php └── SlackErrorNotifierServiceProvider.php ├── LICENSE └── README.md /youtube-thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/freshbitsweb/slack-error-notifier/HEAD/youtube-thumb.png -------------------------------------------------------------------------------- /slack-error-notifier.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/freshbitsweb/slack-error-notifier/HEAD/slack-error-notifier.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | /vendor/ 3 | 4 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 5 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 6 | # composer.lock 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [1.0.2] - 2018-11-30 8 | ### Added 9 | - Allow minimum error level to be set #1 (@benyanke) 10 | 11 | ## [1.0.3] - 2018-12-17 12 | ### Added 13 | - Append current environment name to the log context data #8 (@benyanke) -------------------------------------------------------------------------------- /config/slack_error_notifier.php: -------------------------------------------------------------------------------- 1 | env('SLACK_LOG_LEVEL', null), 5 | 6 | 'webhook_url' => env('SLACK_WEBHOOK_URL', ''), 7 | 8 | 'add_memory_usage' => true, 9 | 10 | 'add_request_headers' => true, 11 | 12 | 'add_session_data' => true, 13 | 14 | 'add_input_data' => true, 15 | 16 | // You can specify the inputs from the user that should not be sent to Slack 17 | 'ignore_request_fields' => ['password', 'confirm_password'] 18 | ]; 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "freshbitsweb/slack-error-notifier", 3 | "description": "Send a message to a Slack channel when error occurs in your Laravel Application", 4 | "require": { 5 | "php": ">=7.0", 6 | "illuminate/support": "5.5.*", 7 | "monolog/monolog": "~1.12" 8 | }, 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Gaurav Makhecha", 13 | "email": "info@freshbits.in" 14 | } 15 | ], 16 | "minimum-stability": "dev", 17 | "autoload": { 18 | "psr-4": { 19 | "Freshbitsweb\\SlackErrorNotifier\\": "src/" 20 | } 21 | }, 22 | "extra": { 23 | "laravel": { 24 | "providers": [ 25 | "Freshbitsweb\\SlackErrorNotifier\\SlackErrorNotifierServiceProvider" 26 | ] 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/RequestDataProcessor.php: -------------------------------------------------------------------------------- 1 | except(config('slack_error_notifier.ignore_request_fields')); 16 | } 17 | 18 | if (config('slack_error_notifier.add_request_headers')) { 19 | $record['extra']['headers'] = request()->header(); 20 | } 21 | 22 | if (config('slack_error_notifier.add_session_data')) { 23 | $record['extra']['session'] = session()->all(); 24 | } 25 | 26 | return $record; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Freshbits Web Solutions 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Slack Error Notifier](./slack-error-notifier.png "Slack Error Notifier") 2 | 3 | # Slack Error Notifier (Alternative to *Bugsnag/Sentry*) (Laravel 5.5) 4 | If you can't use *Bugsnag* or *Sentry* for monitoring your **production Laravel application**, here is a really simple solution to get notified when anything goes wrong. 5 | 6 | This is a *plug-and-play* Laravel package to send a message to a Slack channel via an [Incoming WebHook](https://api.slack.com/incoming-webhooks) when error/exception occurs in your application. 7 | 8 | 9 | **Note**: Logging feature had major updates in Laravel 5.6. you may use [Laravel Log Enhancer](https://github.com/freshbitsweb/laravel-log-enhancer) package for it. 10 | 11 | 12 | ## Requirements 13 | 14 | * PHP 7.0+ 15 | * Laravel 5.5 16 | 17 | ## Installation 18 | 19 | 1) Install the package by running this command in your terminal/cmd: 20 | ``` 21 | composer require freshbitsweb/slack-error-notifier 22 | ``` 23 | 24 | 2) Import config file by running this command in your terminal/cmd: 25 | ``` 26 | php artisan vendor:publish --tag=slack-error-notifier-config 27 | ``` 28 | 29 | 3) Create an [Incoming WebHook](https://my.slack.com/services/new/incoming-webhook/) in your Slack account and put *Webhook URL* in your .env file for **SLACK_WEBHOOK_URL** variable 30 | ``` 31 | SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/xxxxxxxxxxxxxxxxxxxxxxxx 32 | ``` 33 | 34 | 4) Optinal: Change configuration settings: 35 | * (bool) add_memory_usage => Set to *true* if you wish to send memory usage in the slack message 36 | 37 | * (bool) add_request_headers => Set to *true* if you wish to send request headers in the slack message 38 | 39 | * (bool) add_session_data => Set to *true* if you wish to send session data in the slack message 40 | 41 | * (bool) add_input_data => Set to *true* if you wish to send input data in the slack message 42 | 43 | * (array) ignore_request_fields => If input data is being sent, you can specify the inputs from the user that should not be sent to Slack for example, password,cc number, etc. 44 | 45 | * (env) SLACK_LOG_LEVEL => Specify minimum error level (#1) to notify slack. 46 | 47 | And it's done. Yeah, that simple. Here's a video demo for a quick look: 48 | 49 | [![Youtube Video](./youtube-thumb.png "Youtube Video")](https://www.youtube.com/watch?v=jeljU856bzE) 50 | 51 | #### Note 52 | This package uses *Monolog* library and sends a notification message based on the [**log_level**](https://laravel.com/docs/5.5/errors#log-severity-levels) configuration setting. So, if you set it to *alert*, only alert and emergency level errors will be considered. 53 | 54 | ## Authors 55 | 56 | * [**Gaurav Makhecha**](https://github.com/gauravmak) - *Initial work* 57 | 58 | See also the list of [contributors](https://github.com/freshbitsweb/slack-error-notifier/graphs/contributors) who participated in this project. 59 | 60 | ## License 61 | 62 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details 63 | 64 | ## Special Thanks to 65 | 66 | * Laravel Community 67 | -------------------------------------------------------------------------------- /src/SlackErrorNotifierServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 23 | // Publish config file 24 | $this->publishes([ 25 | __DIR__.'/../config/slack_error_notifier.php' => config_path('slack_error_notifier.php'), 26 | ], 'slack-error-notifier-config'); 27 | } 28 | 29 | $this->pushSlackHandlerToLogger(); 30 | } 31 | 32 | /** 33 | * Service container bindings 34 | * 35 | * @return void 36 | */ 37 | public function register() 38 | { 39 | // Users can specify only the options they actually want to override 40 | $this->mergeConfigFrom( 41 | __DIR__.'/../config/slack_error_notifier.php', 42 | 'slack_error_notifier' 43 | ); 44 | } 45 | 46 | /** 47 | * Pushes Slack webhook handler to current logger 48 | * 49 | * @return void 50 | */ 51 | protected function pushSlackHandlerToLogger() 52 | { 53 | // Only if webhook URL is available 54 | if ($webhookUrl = config('slack_error_notifier.webhook_url')) { 55 | $logWriter = $this->app->make(LoggerInterface::class); 56 | $logger = $logWriter->getMonolog(); 57 | 58 | // Add slack handler to the monologger 59 | $slackHandler = new SlackWebhookHandler($webhookUrl, null, null, true, null, false, true, $this->getlogLevel($logger)); 60 | $slackHandler = $this->pushProcessors($slackHandler); 61 | $logger->pushHandler($slackHandler); 62 | } 63 | } 64 | 65 | /** 66 | * Fetches and returns the log level for the new handler 67 | * 68 | * @param \Monolog\Logger Logger object 69 | * @return int 70 | */ 71 | protected function getLogLevel($logger) 72 | { 73 | $logLevel = config('slack_error_notifier.log_level') ?: config('app.log_level', 'error'); 74 | 75 | $logLevel = strtoupper($logLevel); 76 | 77 | return constant(get_class($logger) . '::' . $logLevel); 78 | } 79 | 80 | /** 81 | * Pushes number of processors to the handler to add extra parameters to the log message 82 | * 83 | * @param \Monolog\Handler\SlackWebhookHandler 84 | * @return \Monolog\Handler\SlackWebhookHandler 85 | */ 86 | protected function pushProcessors($handler) 87 | { 88 | if (config('slack_error_notifier.add_memory_usage')) { 89 | $handler->pushProcessor(new MemoryUsageProcessor); 90 | } 91 | 92 | $handler->pushProcessor(new RequestDataProcessor); 93 | $handler->pushProcessor(new WebProcessor); 94 | 95 | return $handler; 96 | } 97 | } 98 | --------------------------------------------------------------------------------