因?yàn)轫?xiàng)目是TP6開發(fā)的、現(xiàn)在目前想做一個(gè)日志監(jiān)控功能。想問下webman-statistic 應(yīng)用監(jiān)控插件可以用在TP上嗎? 可以用于分布式的日志管理上嗎
<?php
declare(strict_types=1);
namespace app\middleware;
class Transfer
{
/**
* 處理請(qǐng)求
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
$startTime = microtime(true); // 開始時(shí)間
$project = 'tp6'; // 應(yīng)用名
$ip = '127.0.0.1'; // 請(qǐng)求IP
$transfer = 'test'; // 調(diào)用入口
$response = $next($request);
$finishTime = microtime(true); // 結(jié)束時(shí)間
$costTime = $finishTime - $startTime; // 運(yùn)行時(shí)長(zhǎng)
$code = mt_rand(2, 5) * 100; // 狀態(tài)碼
$success = $code < 400; // 是否成功
// 詳細(xì)信息,自定義設(shè)置
$details = [
'time' => date('Y-m-d H:i:s.', (int)$startTime) . substr((string)$startTime, 11), // 請(qǐng)求時(shí)間(包含毫秒時(shí)間)
'run_time' => $costTime, // 運(yùn)行時(shí)長(zhǎng)
// .....
];
// 執(zhí)行上報(bào)
try {
// 數(shù)據(jù)打包 多條“\n”隔開
$data = json_encode([
'time' => date('Y-m-d H:i:s.', (int)$startTime) . substr((string)$startTime, 11),
'project' => $project,
'ip' => $ip,
'transfer' => $transfer,
'costTime' => $costTime,
'success' => $success ? 1 : 0,
'code' => $code,
'details' => json_encode($details, 320),
], 320) . "\n";
$client = new \GuzzleHttp\Client(['verify' => false]);
$client->post(
// 上報(bào)地址
'http://127.0.0.1:8788/report/statistic/transfer',
[
'headers' => [
// 上報(bào)認(rèn)證,不設(shè)置默認(rèn)為當(dāng)前年份的md5值
'authorization' => md5(date('Y'))
],
'form_params' => [
// 上報(bào)數(shù)據(jù)
'transfer' => $data
],
]
);
} catch (\Throwable $th) {
//throw $th;
}
return $response;
}
}