Last active
May 18, 2018 16:06
-
-
Save DaaGER/e3bb73988cfcd5ad1733a2b0467d81ab to your computer and use it in GitHub Desktop.
Sending Laravel5.5 errors to Telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'telegram'=>false, | |
'telegram_token'=>'', | |
'telegram_chat_id'=>'', | |
]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Monolog\Handler\TelegramHandler; | |
use Illuminate\Support\ServiceProvider; | |
use Monolog\Logger; | |
class TelegramExceptionsProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap the application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$logger = \Log::getMonolog(); | |
$logger->pushProcessor(function($record) { | |
$data = []; | |
if (\Auth::check()) { | |
$user=\Auth::user(); | |
$data['user']= "User #{$user->id} {$user->name} {$user->email}"; | |
} else { | |
$data['user']="Guest"; | |
} | |
if(isset($_SERVER['REMOTE_ADDR'])) $data['ip']= 'IP ' . $_SERVER['REMOTE_ADDR']; | |
if(isset($_SERVER['REQUEST_METHOD'])){ | |
$data['request_method']=$_SERVER['REQUEST_METHOD']; | |
} | |
if(isset($_SERVER['REQUEST_URI'])){ | |
$data['request_uri']=$_SERVER['REQUEST_METHOD']; | |
} | |
if(isset($_SERVER['HTTP_REFERER'])){ | |
$data['referer'] = "Referer: " . $_SERVER['HTTP_REFERER']; | |
} | |
$record['message'] = implode("\r\n",$data) . "\r\n---------------------\r\n".$record['message']; | |
return $record; | |
}); | |
if (\App::environment('local') OR config('logger.telegram')) { | |
$handler = new TelegramHandler( | |
config('logger.telegram_token'), | |
config('logger.telegram_chat_id'), | |
Logger::WARNING | |
); | |
$logger->pushHandler($handler); | |
} | |
} | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: daager | |
* Date: 15/05/2018 | |
* Time: 19:14 | |
*/ | |
namespace App\Monolog\Handler; | |
use Monolog\Handler\AbstractProcessingHandler; | |
use Monolog\Handler\Curl; | |
use Monolog\Logger; | |
class TelegramHandler extends AbstractProcessingHandler | |
{ | |
public function __construct($token, $chat_id, $level = Logger::CRITICAL, $bubble = true) | |
{ | |
parent::__construct($level, $bubble); | |
$this->token = $token; | |
$this->chat_id = $chat_id;; | |
} | |
protected function write(array $record) | |
{ | |
$url = | |
'https://api.telegram.org/bot'.$this->token.'/sendmessage?'.http_build_query([ | |
'chat_id' =>$this->chat_id, | |
'text' =>$record['message'] | |
]); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); | |
Curl\Util::execute($ch); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment