UdpLog.php
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?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);
}
}