Authored by 周奇琪

修改日志路径,只有pc_pay有写的权限

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 - 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  
33 -  
34 - /**  
35 - * proc line and send log to influxdb  
36 - * @param $level  
37 - * @param $message  
38 - * @param $meta  
39 - */  
40 - private static function procLog($level, $message, $debugInfo, $meta = '') {  
41 - date_default_timezone_set('PRC');  
42 - $level = str_replace(__CLASS__.'::','',$level);  
43 - $file = $debugInfo[0]["file"];  
44 - $line = $debugInfo[0]["line"];  
45 - $string = '';  
46 - //make tags  
47 - $tags = array(  
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)  
55 - );  
56 - //make a line  
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 - }  
64 - }  
65 -  
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 - /**  
116 - * send by udp  
117 - * @param $string  
118 - */  
119 - private static function send($string) {  
120 - $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);  
121 - $len = strlen($string);  
122 - socket_sendto($sock, $string, $len, 0, self::$url, self::$port);  
123 - socket_close($sock);  
124 - }  
125 -  
126 - /**  
127 - * info log  
128 - * @param $message  
129 - * @param mixed $meta  
130 - */  
131 - public static function info($message, $meta = '') {  
132 - self::procLog(__METHOD__, $message, debug_backtrace(), $meta);  
133 - }  
134 -  
135 - /**  
136 - * warn log  
137 - * @param $message  
138 - * @param mixed $meta  
139 - */  
140 - public static function warn($message, $meta = '') {  
141 - self::procLog(__METHOD__, $message, debug_backtrace(), $meta);  
142 - }  
143 -  
144 - /**  
145 - * error log  
146 - * @param $message  
147 - * @param mixed $meta  
148 - */  
149 - public static function error($message, $meta = '') {  
150 - self::procLog(__METHOD__, $message, debug_backtrace(), $meta);  
151 - }  
152 -  
153 - /**  
154 - * debug log  
155 - * @param $message  
156 - * @param mixed $meta  
157 - */  
158 - public static function debug($message, $meta = '') {  
159 - self::procLog(__METHOD__, $message, debug_backtrace(), $meta);  
160 - } 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 + public static $filePath = '/Data/logs/pc_pay/';
  28 +
  29 + const RECORD_MODE_FILE = 'FILE';
  30 + const RECORD_MODE_UDP = 'UDP';
  31 +
  32 + const RECORD_MODE = 'FILE';//mode: FILE | UDP
  33 +
  34 + /**
  35 + * proc line and send log to influxdb
  36 + * @param $level
  37 + * @param $message
  38 + * @param $meta
  39 + */
  40 + private static function procLog($level, $message, $debugInfo, $meta = '') {
  41 + date_default_timezone_set('PRC');
  42 + $level = str_replace(__CLASS__.'::','',$level);
  43 + $file = $debugInfo[0]["file"];
  44 + $line = $debugInfo[0]["line"];
  45 + $string = '';
  46 + //make tags
  47 + $tags = array(
  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)
  55 + );
  56 + //make a line
  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 + }
  64 + }
  65 +
  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 + /**
  116 + * send by udp
  117 + * @param $string
  118 + */
  119 + private static function send($string) {
  120 + $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  121 + $len = strlen($string);
  122 + socket_sendto($sock, $string, $len, 0, self::$url, self::$port);
  123 + socket_close($sock);
  124 + }
  125 +
  126 + /**
  127 + * info log
  128 + * @param $message
  129 + * @param mixed $meta
  130 + */
  131 + public static function info($message, $meta = '') {
  132 + self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
  133 + }
  134 +
  135 + /**
  136 + * warn log
  137 + * @param $message
  138 + * @param mixed $meta
  139 + */
  140 + public static function warn($message, $meta = '') {
  141 + self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
  142 + }
  143 +
  144 + /**
  145 + * error log
  146 + * @param $message
  147 + * @param mixed $meta
  148 + */
  149 + public static function error($message, $meta = '') {
  150 + self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
  151 + }
  152 +
  153 + /**
  154 + * debug log
  155 + * @param $message
  156 + * @param mixed $meta
  157 + */
  158 + public static function debug($message, $meta = '') {
  159 + self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
  160 + }
161 } 161 }