Authored by yangyang

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

@@ -12,6 +12,8 @@ @@ -12,6 +12,8 @@
12 namespace Api; 12 namespace Api;
13 13
14 use Plugin\Cache; 14 use Plugin\Cache;
  15 +use Plugin\UdpLog;
  16 +
15 class Yohobuy 17 class Yohobuy
16 { 18 {
17 /* 正式环境 */ 19 /* 正式环境 */
@@ -186,6 +188,8 @@ class Yohobuy @@ -186,6 +188,8 @@ class Yohobuy
186 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 188 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
187 } 189 }
188 $result = curl_exec($ch); 190 $result = curl_exec($ch);
  191 + //log打印
  192 + UdpLog::info('get调用接口入参url/出参:', 'in:'.$url.' out:'.$result);
189 if (!$returnJson && !empty($result)) { 193 if (!$returnJson && !empty($result)) {
190 $result = json_decode($result, true); 194 $result = json_decode($result, true);
191 } 195 }
@@ -249,6 +253,8 @@ class Yohobuy @@ -249,6 +253,8 @@ class Yohobuy
249 curl_setopt($ch, CURLOPT_POSTFIELDS, $str); 253 curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
250 } 254 }
251 $result = curl_exec($ch); 255 $result = curl_exec($ch);
  256 + //log打印
  257 + UdpLog::info('get调用接口入参url/出参:', 'in:'.$url.'?'.$str.' out:'.$result);
252 if (!$returnJson && !empty($result)) { 258 if (!$returnJson && !empty($result)) {
253 $result = json_decode($result, true); 259 $result = json_decode($result, true);
254 } 260 }
  1 +<?php
  2 +/**
  3 + * Created by IntelliJ IDEA.
  4 + * User: hbomb_000
  5 + * Date: 2016/5/5
  6 + * Time: 18:17
  7 + */
  8 +
  9 +namespace Plugin;
  10 +
  11 +
  12 +/**
  13 + * Class UdpLog
  14 + * @useage:
  15 + * UdpLog::info('get payment list begin',array('order_code'=>123231));
  16 + * @package WebPlugin
  17 + */
  18 +class UdpLog
  19 +{
  20 + //influxdb url
  21 + public static $url = 'influxdb.yohobuy.com';
  22 + //influxdb port
  23 + public static $port = '4444';
  24 + //influxdb measurement
  25 + public static $measurement = 'php_log';
  26 +
  27 + /**
  28 + * proc line and send log to influxdb
  29 + * @param $level
  30 + * @param $message
  31 + * @param $meta
  32 + */
  33 + private static function procLog($level,$message,$debugInfo,$meta='') {
  34 + $level = str_replace(__CLASS__.'::','',$level);
  35 + $file = $debugInfo[0]["file"];
  36 + $line = $debugInfo[0]["line"];
  37 +
  38 + //make tags
  39 + $tags = array(
  40 + 'host='.gethostname(),
  41 + 'level='.$level,
  42 + 'file='.$file,
  43 + 'line='.$line
  44 + );
  45 +
  46 + //make a line
  47 + $tags = implode(',',$tags);
  48 +
  49 + $string = self::$measurement .','.$tags.' message="'.$message.'",meta="'.var_export($meta,true).'"';
  50 + self::send($string);
  51 + }
  52 +
  53 + /**
  54 + * send by udp
  55 + * @param $string
  56 + */
  57 + private static function send($string) {
  58 + $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  59 + $len = strlen($string);
  60 + socket_sendto($sock, $string, $len, 0, self::$url, self::$port);
  61 + socket_close($sock);
  62 + }
  63 +
  64 + /**
  65 + * info log
  66 + * @param $message
  67 + * @param $meta
  68 + */
  69 + public static function info($message,$meta='') {
  70 + self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
  71 + }
  72 +
  73 + /**
  74 + * warn log
  75 + * @param $message
  76 + * @param $meta
  77 + */
  78 + public static function warn($message,$meta='') {
  79 + self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
  80 + }
  81 +
  82 + /**
  83 + * error log
  84 + * @param $message
  85 + * @param $meta
  86 + */
  87 + public static function error($message,$meta='') {
  88 + self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
  89 + }
  90 +
  91 + /**
  92 + * debug log
  93 + * @param $message
  94 + * @param $meta
  95 + */
  96 + public static function debug($message,$meta='') {
  97 + self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
  98 + }
  99 +}
  100 +