Skip to content

Commit 9bfbaa0

Browse files
authored
Merge pull request #8 from maiscrm/add-access-out-log
fix: add access out log
2 parents fdfba87 + 9be76e6 commit 9bfbaa0

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

jd/JdClient.php

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
namespace Jd;
33

44
use \Exception;
5+
use Yii;
6+
use yii\helpers\FileHelper;
7+
use yii\helpers\Json;
58

69
class JdClient
710
{
@@ -63,20 +66,21 @@ public function curl($url, $postFields = null)
6366
if ($postMultipart) {
6467
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
6568
} else {
66-
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
69+
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
6770
}
6871
}
69-
$reponse = curl_exec($ch);
72+
$response = curl_exec($ch);
73+
$this->recordAccessOutLog($ch, 'POST', $url, $response, $postFields, ['logResponse' => true]);
7074
if (curl_errno($ch)) {
71-
throw new Exception(curl_error($ch),0);
75+
throw new Exception(curl_error($ch), 0);
7276
} else {
7377
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7478
if (200 !== $httpStatusCode) {
75-
throw new Exception($reponse,$httpStatusCode);
79+
throw new Exception($response, $httpStatusCode);
7680
}
7781
}
7882
curl_close($ch);
79-
return $reponse;
83+
return $response;
8084
}
8185

8286
public function execute($request, $access_token = null)
@@ -133,7 +137,6 @@ public function execute($request, $access_token = null)
133137

134138
//返回的HTTP文本不是标准JSON或者XML,记下错误日志
135139
if (false === $respWellFormed) {
136-
$this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp);
137140
$result->code = 0;
138141
$result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
139142
return $result;
@@ -169,4 +172,65 @@ public function exec($paramsArray)
169172
}
170173
return $this->execute($req, $session);
171174
}
175+
176+
private function recordAccessOutLog($ch, $method, $url, $content, $params, $options = [])
177+
{
178+
// 如果是跑test case的直接跳过
179+
if (YII_ENV == 'test') {
180+
return true;
181+
}
182+
183+
$info = curl_getinfo($ch);
184+
$message = [
185+
'type' => 'accessOut',
186+
'remoteAddr' => $info['primary_ip'],
187+
'reqId' => defined('REQUEST_ID') ? REQUEST_ID : '00000000-0000-0000-0000-000000000000',
188+
'scheme' => parse_url($url, PHP_URL_SCHEME),
189+
'host' => parse_url($url, PHP_URL_HOST),
190+
'port' => $info['primary_port'],
191+
'method' => $method,
192+
'url' => substr($url, strpos($url, parse_url($url, PHP_URL_PATH))),
193+
'responseStatus' => $info['http_code'],
194+
'responseTime' => floor($info['total_time'] * 1000), // 单位:毫秒
195+
'others' => [
196+
'contentType' => $info['content_type'],
197+
'namelookupTime' => floor($info['namelookup_time'] * 1000),
198+
'connectTime' => floor($info['connect_time'] * 1000),
199+
'pretransferTime' => floor($info['pretransfer_time'] * 1000),
200+
'starttransferTime' => floor($info['starttransfer_time'] * 1000),
201+
],
202+
];
203+
204+
if (!empty($params) && in_array($method, ['POST', 'PUT'])) {
205+
$message['body'] = $params;
206+
}
207+
208+
if (isset($options['logResponse']) && $options['logResponse']) {
209+
$message['others']['responseBody'] = $content;
210+
}
211+
212+
$encodedMessage = Json::encode($message) . "\n";
213+
214+
if (defined('DISABLE_ACCESS_OUT_LOG')) {
215+
$logFile = Yii::$app->getRuntimePath() . '/logs/access-out.log';
216+
$logPath = dirname($logFile);
217+
if (!is_dir($logPath)) {
218+
FileHelper::createDirectory($logPath, 0775, true);
219+
}
220+
$text = '[' . date('Y-m-d H:i:s', time()) . ']' . $encodedMessage;
221+
$file = fopen($logFile, 'a');
222+
fwrite($file, $text);
223+
fclose($file);
224+
225+
return true;
226+
}
227+
228+
if (PHP_SAPI === 'cli') {
229+
echo $encodedMessage;
230+
} else {
231+
file_put_contents('/var/run/phplog', $encodedMessage);
232+
}
233+
234+
return true;
235+
}
172236
}

0 commit comments

Comments
 (0)