Laravel integrated monolog to do log processing, Monolog is very powerful, but also easy to expand, in fact, it already supports a lot of Handler, you can send logs to mail or other log collection server or system, because our team use Bearychat As a team communication tool, so the real-time error log of the Laravel project is pushed to a discussion group in Bearychat, easy to find anomalies and solve problems quickly, in fact, very simple not many lines of code to be done.
- First create the Bearychathandler
Data[' channel '] = $channel; $this->webhook = $webhook; Parent::__construct ($level, $bubble); }/** * {@inheritDoc} */protected function write (array $record) {$postData = [' Text ' = = $record [' DateTime ']->format (' y-m-d h:i:s '). '-' . $record ["Level"]. '-' . $record ["Level_name"], ' markdown ' = false, ' notification ' = ' Laravel Error Log ', ' Attachments ' = [' title ' = ' + Current ' (Preg_split ("/([. \n\r]+)/I", $record [' Messa GE ']), ' text ' = = $record [' message '], ' color ' = ' #ffa500 '] ] ]; $postString = Json_encode (Array_merge ($this->data, $postData)); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $this->webhook); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_posTfields, $postString); curl_setopt ($ch, Curlopt_httpheader, Array ("Content-type:application/json")); Curl\util::execute ($ch); }}
As long as you inherit Abstractprocessinghandler, rewrite the Write method, write the Webhook address to Bearychat post data, $record is the details of the Monolog logging
In the Official Handbook, you have complete control of monolog like this.
Custom Monolog Configuration
If you would a and have a complete control over what Monolog is configured for your application, the Applicatio N ' s configuremonologusing method. You should place a call to this method in your bootstrap/app.php file right before the $app variable is returned by the FI Le
$app->configuremonologusing (function ($monolog) {$monolog->pushhandler (...);});
return $app;
But we have to keep the default record to log file in addition to Chearychat, so we do not, we just need to Exception Handler place to add a our chearychathandler on it.
To modify the app/exceptions/handler.php, add the following code to the report method:
Pushhandler (New Bearychathandler (' Https://yourhookurl ', ' laravel Log Robot ')); Return Parent::report ($e); } ......}
Well, write a mistake in the code to see if Bearychat received the error log.
Reprint Please specify: Reproduced from Ryan is a rookie | LNMP Technology Stack Notes
If you think this article is very useful to you, why not give it a reward?