Showing
1 changed file
with
73 additions
and
11 deletions
@@ -23,6 +23,13 @@ class UdpLog | @@ -23,6 +23,13 @@ class UdpLog | ||
23 | public static $port = '4444'; | 23 | public static $port = '4444'; |
24 | //influxdb measurement | 24 | //influxdb measurement |
25 | public static $measurement = 'php_log'; | 25 | public static $measurement = 'php_log'; |
26 | + | ||
27 | + public static $filePath = '/Data/logs/'; | ||
28 | + | ||
29 | + const RECORD_MODE_FILE = 'FILE'; | ||
30 | + const RECORD_MODE_UDP = 'UDP'; | ||
31 | + | ||
32 | + const RECORD_MODE = 'FILE';//mode: FILE | UDP | ||
26 | 33 | ||
27 | /** | 34 | /** |
28 | * proc line and send log to influxdb | 35 | * proc line and send log to influxdb |
@@ -30,27 +37,82 @@ class UdpLog | @@ -30,27 +37,82 @@ class UdpLog | ||
30 | * @param $message | 37 | * @param $message |
31 | * @param $meta | 38 | * @param $meta |
32 | */ | 39 | */ |
33 | - private static function procLog($level,$message,$debugInfo,$meta='') { | 40 | + private static function procLog($level, $message, $debugInfo, $meta = '') { |
41 | + date_default_timezone_set('PRC'); | ||
34 | $level = str_replace(__CLASS__.'::','',$level); | 42 | $level = str_replace(__CLASS__.'::','',$level); |
35 | $file = $debugInfo[0]["file"]; | 43 | $file = $debugInfo[0]["file"]; |
36 | $line = $debugInfo[0]["line"]; | 44 | $line = $debugInfo[0]["line"]; |
37 | - | 45 | + $string = ''; |
38 | //make tags | 46 | //make tags |
39 | $tags = array( | 47 | $tags = array( |
40 | - 'host='.gethostname(), | ||
41 | - 'level='.$level, | ||
42 | - 'file='.$file, | ||
43 | - 'line='.$line | 48 | + 'time' => date('Y-m-d H:i:s',time()), |
49 | + 'level' => $level, | ||
50 | + 'host'=> gethostname(), | ||
51 | + 'file'=> $file, | ||
52 | + 'line'=> $line, | ||
53 | + 'message' => $message, | ||
54 | + 'meta' => serialize($meta) | ||
44 | ); | 55 | ); |
45 | - | ||
46 | //make a line | 56 | //make a line |
47 | - $tags = implode(',',$tags); | ||
48 | - | ||
49 | - $string = self::$measurement .','.$tags.' message="'.$message.'",meta="'.var_export($meta,true).'"'; | ||
50 | - self::send($string); | 57 | + $string = implode('|', $tags);//format: time|level|host|file|line|message|meta |
58 | + if(self::RECORD_MODE == self::RECORD_MODE_UDP) { | ||
59 | + self::send($string); | ||
60 | + } | ||
61 | + else if(self::RECORD_MODE == self::RECORD_MODE_FILE) { | ||
62 | + self::fileLog($level, $string); | ||
63 | + } | ||
51 | } | 64 | } |
52 | 65 | ||
53 | /** | 66 | /** |
67 | + * 文件日志记录 | ||
68 | + * | ||
69 | + * @param string $level | ||
70 | + * @param string $message | ||
71 | + */ | ||
72 | + private static function fileLog($level, $message) | ||
73 | + { | ||
74 | + $filename = $level . '.log'; //日志文件 | ||
75 | + $logFile = self::createPath(self::$filePath, $filename); | ||
76 | + if (!file_exists(self::$filePath))//判断文件路径是否存在 | ||
77 | + { | ||
78 | + if (!empty(self::$filePath))//判断路径是否为空 | ||
79 | + { | ||
80 | + if (!(self::createDir(self::$filePath))) { | ||
81 | + return false; | ||
82 | + } | ||
83 | + if (!is_writable($logFile)) { | ||
84 | + return false; | ||
85 | + } | ||
86 | + } | ||
87 | + } | ||
88 | + @error_log($message."\r\n", 3, $logFile); | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
92 | + * 作用:创建目录 | ||
93 | + * 输入:要创建的目录 | ||
94 | + * 输出:true | false | ||
95 | + */ | ||
96 | + private static function createDir($dir) | ||
97 | + { | ||
98 | + return is_dir($dir) or (self::createDir(dirname($dir)) and mkdir($dir, 0777)); | ||
99 | + } | ||
100 | + | ||
101 | + /** | ||
102 | + * 作用:构建路径 | ||
103 | + * 输入:文件的路径,要写入的文件名 | ||
104 | + * 输出:构建好的路径字串 | ||
105 | + */ | ||
106 | + private static function createPath($dir, $filename) | ||
107 | + { | ||
108 | + if (empty($dir)) { | ||
109 | + return $filename; | ||
110 | + } else { | ||
111 | + return $dir . "/" . $filename; | ||
112 | + } | ||
113 | + } | ||
114 | + | ||
115 | + /** | ||
54 | * send by udp | 116 | * send by udp |
55 | * @param $string | 117 | * @param $string |
56 | */ | 118 | */ |
-
Please register or login to post a comment