Authored by whb

udp

<?php
/**
* Created by IntelliJ IDEA.
* User: hbomb_000
* Date: 2016/5/5
* Time: 18:17
*/
namespace WebPlugin;
/**
* 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);
}
}
\ No newline at end of file
... ...