This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

What makes a good back-end programmer? Never do by yourself what a machine can do, haha.

Scene: the repetition

  • Client: there is an error in the backend interface. I failed to parse the data.
  • Server: Ok, I’ll check log. I need you to print out the request parameters.
  • Client: How do I print?
  • Server:…. I’ll just look up the log myself

Does this happen all the time in development? Is it difficult? Is there a good way to make debug smarter?

Analysis of the

  • No matter which language you do server-side development in, there is always exception handling and logging.
  • Find a three-party platform, call back when an exception is caught or a new print log is available, and push the error log to us.
  • After some research, a nailing robot is a good job

Just do it, brush the document, write the implementation.

The back-end implementation uses Laravel in PHP as an example, but other languages can also use the same idea.

Modifying Log Configurations

<? php use Monolog\Handler\NullHandler; use Monolog\Handler\StreamHandler; use Monolog\Handler\SyslogUdpHandler; return [ 'default' => env('LOG_CHANNEL', 'stack'), 'channels' => [ 'stack' => [ 'driver' => 'stack', Logs /laravel.log (' daily ', 'laravel.log', 'laravel.log'); Dingding 'channel 'channels => env(' APP_ENV ') == 'test'? ['daily', 'dingding'] : ['daily'], 'ignore_exceptions' => false,], // monolog' dingding' => [' driver' => 'monolog', 'level' = > 'error' and 'handler' = > \ App \ handler \ DingdingLogHandler: : class, / / a custom handler, 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', 'days' => 14, ], [,];Copy the code

The unimportant code above uses three vertical ones. Omit display.

The custom Handler

<?php
namespace App\Handler;

use App\Library\CurlRequest;
use App\Library\Utility;
use Monolog\Logger;
use Monolog\Handler;

class DingdingLogHandler extends Handler\AbstractProcessingHandler
{
    private $apiKey;
    private $channel;

    public function __construct(
        $level = Logger::DEBUG,
        bool $bubble = true
    ) {
        parent::__construct($level, $bubble);


    }

    protected function write(array $record): void
    {
        $this->send($record['formatted']);
    }


    protected function send(string $message): void
    {
        $microSecond = Utility::getMicroSecond();
        $key = "xxxx";
        $hashString = hash_hmac("sha256", $microSecond ."\n" . $key, $key, true);
        $sign = urlencode(base64_encode($hashString));

        CurlRequest::post("https://oapi.dingtalk.com/robot/send?access_token=xxxxx&timestamp=".$microSecond."&sign=".$sign,
            [
                "msgtype" => "text",
                "at" => [
                    "atMobiles" => [
                        "xxxx",
                        "xxxx"
                    ]
                ],
                "text" => [
                    "content" => $message
                ]
            ]);
    }
}

Copy the code

Effect of the deployment going online

No more log climbing!

Test sister again to find me to say the client report error data analysis error, I also can immediately hard answer: “should be the client analysis problem, the server has not received the error. “

Put a witty expression here

Reference documentation

  • Laravel This document is a log in Chinese
  • Nailing related reference documents Nailing robot documentation

You are welcome to comment and like. When will the Nuggets get a three-button feature