Make a bearychat laravel project error log notification robot

Source: Internet
Author: User
Tags webhook
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

    • Create a bearychat group to accept notifications

      • Create a new discussion group, such as "Laravel error log Report"
      • Add a robot to the discussion group, click Incoming Add a "Laravel log Robot", send the target to select the previous step of the "Laravel error log Report", so that the Webhook URL address is obtained
      • Add the appropriate personnel to this discussion group
    • Integrated Chearychathandler

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?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.