啥样的后端程序员是好程序员?能机器做的事自己绝对不做。
以上这种场景在开发中是不是时有发生?是不是很难顶?有啥好办法让debug更智能一点吗?
说干就干,刷文档,写实现。
后端实现以PHP的Laravel为例,其他语言也可以借鉴思路。
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
//测试环境除了使用daily保存每天日志到logs/laravel.log,还使用’dingding‘channel
'channels' => env("APP_ENV") == 'test' ? ['daily', 'dingding'] : ['daily'],
'ignore_exceptions' => false,
],
//配置钉钉 驱动选择 monolog
'dingding' => [
'driver' => 'monolog',
'level' => 'error',
'handler' => \App\Handler\DingdingLogHandler::class, //自定义handler
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
.
.
.
],
];
上面不重要的代码使用3个竖向排列的.省略显示。
<?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×tamp=".$microSecond."&sign=".$sign,
[
"msgtype" => "text",
"at" => [
"atMobiles" => [
"xxxx",
"xxxx"
]
],
"text" => [
"content" => $message
]
]);
}
}
再也不用爬日志啦!
测试妹子再找我说可能接口挂了,我也可以马上硬气的=回答:”应该是客户端解析问题,服务端没收到报错。“
此处放一个机智的表情