Showing
1 changed file
with
99 additions
and
0 deletions
library/WebPlugin/UdpLog.php
0 → 100644
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 WebPlugin; | ||
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 | +} |
-
Please register or login to post a comment