UdpLog.php
4.25 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?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';
public static $filePath = '/Data/logs/pc_pay/';
const RECORD_MODE_FILE = 'FILE';
const RECORD_MODE_UDP = 'UDP';
const RECORD_MODE = 'FILE';//mode: FILE | UDP
/**
* proc line and send log to influxdb
* @param $level
* @param $message
* @param $meta
*/
private static function procLog($level, $message, $debugInfo, $meta = '') {
date_default_timezone_set('PRC');
$level = str_replace(__CLASS__.'::','',$level);
$file = $debugInfo[0]["file"];
$line = $debugInfo[0]["line"];
$string = '';
//make tags
$tags = array(
'time' => date('Y-m-d H:i:s',time()),
'level' => $level,
'host'=> gethostname(),
'file'=> $file,
'line'=> $line,
'message' => $message,
'meta' => serialize($meta)
);
//make a line
$string = implode('|', $tags); //format: time|level|host|file|line|message|meta
if(self::RECORD_MODE == self::RECORD_MODE_UDP) {
self::send($string);
}
else if(self::RECORD_MODE == self::RECORD_MODE_FILE) {
self::fileLog($level, $string);
}
}
/**
* 文件日志记录
*
* @param string $level
* @param string $message
*/
private static function fileLog($level, $message)
{
$filename = $level . '.log'; //日志文件
$logFile = self::createPath(self::$filePath, $filename);
if (!file_exists(self::$filePath))//判断文件路径是否存在
{
if (!empty(self::$filePath))//判断路径是否为空
{
if (!(self::createDir(self::$filePath))) {
return false;
}
if (!is_writable($logFile)) {
return false;
}
}
}
@error_log($message."\r\n", 3, $logFile);
}
/**
* 作用:创建目录
* 输入:要创建的目录
* 输出:true | false
*/
private static function createDir($dir)
{
return is_dir($dir) or (self::createDir(dirname($dir)) and mkdir($dir, 0777));
}
/**
* 作用:构建路径
* 输入:文件的路径,要写入的文件名
* 输出:构建好的路径字串
*/
private static function createPath($dir, $filename)
{
if (empty($dir)) {
return $filename;
} else {
return $dir . "/" . $filename;
}
}
/**
* 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 mixed $meta
*/
public static function info($message, $meta = '') {
self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
}
/**
* warn log
* @param $message
* @param mixed $meta
*/
public static function warn($message, $meta = '') {
self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
}
/**
* error log
* @param $message
* @param mixed $meta
*/
public static function error($message, $meta = '') {
self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
}
/**
* debug log
* @param $message
* @param mixed $meta
*/
public static function debug($message, $meta = '') {
self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
}
}