Authored by yangyang

Merge branch 'hotfix/log' of http://git.dev.yoho.cn/web/yohobuywap into hotfix/log

... ... @@ -12,6 +12,8 @@
namespace Api;
use Plugin\Cache;
use Plugin\UdpLog;
class Yohobuy
{
/* 正式环境 */
... ... @@ -186,6 +188,8 @@ class Yohobuy
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
$result = curl_exec($ch);
//log打印
UdpLog::info('get调用接口入参url/出参:', 'in:'.$url.' out:'.$result);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
... ... @@ -249,6 +253,8 @@ class Yohobuy
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
$result = curl_exec($ch);
//log打印
UdpLog::info('get调用接口入参url/出参:', 'in:'.$url.'?'.$str.' out:'.$result);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
... ...
<?php
/**
* Created by IntelliJ IDEA.
* User: hbomb_000
* Date: 2016/5/5
* Time: 18:17
*/
namespace Plugin;
/**
* Class UdpLog
* @useage:
* UdpLog::info('get payment list begin',array('order_code'=>123231));
* @package WebPlugin
*/
class UdpLog
{
//influxdb url
public static $url = 'influxdb.yohobuy.com';
//influxdb port
public static $port = '4444';
//influxdb measurement
public static $measurement = 'php_log';
/**
* proc line and send log to influxdb
* @param $level
* @param $message
* @param $meta
*/
private static function procLog($level,$message,$debugInfo,$meta='') {
$level = str_replace(__CLASS__.'::','',$level);
$file = $debugInfo[0]["file"];
$line = $debugInfo[0]["line"];
//make tags
$tags = array(
'host='.gethostname(),
'level='.$level,
'file='.$file,
'line='.$line
);
//make a line
$tags = implode(',',$tags);
$string = self::$measurement .','.$tags.' message="'.$message.'",meta="'.var_export($meta,true).'"';
self::send($string);
}
/**
* send by udp
* @param $string
*/
private static function send($string) {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$len = strlen($string);
socket_sendto($sock, $string, $len, 0, self::$url, self::$port);
socket_close($sock);
}
/**
* info log
* @param $message
* @param $meta
*/
public static function info($message,$meta='') {
self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
}
/**
* warn log
* @param $message
* @param $meta
*/
public static function warn($message,$meta='') {
self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
}
/**
* error log
* @param $message
* @param $meta
*/
public static function error($message,$meta='') {
self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
}
/**
* debug log
* @param $message
* @param $meta
*/
public static function debug($message,$meta='') {
self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
}
}
... ...