-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExLogger.php
More file actions
380 lines (372 loc) · 11.8 KB
/
ExLogger.php
File metadata and controls
380 lines (372 loc) · 11.8 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* @author Lambda47
* @version 1.0
* @link https://github.com/lambda47/ExLogger
*/
/**
* Class ExLogger
* 记录每次访问的GET参数、POST参数、SESSION变量和执行的SQL语句
*/
class ExLogger {
protected $CI;
/**
* 所有执行的SQL语句
* @access protected
* @var array
*/
protected $queries = array();
/**
* 是否记录执行的SQL语句
* @access private
* @var bool
*/
private $log_query = false;
/**
* 是否记录GET参数
* @access private
* @var bool
*/
private $log_get = false;
/**
* 是否记录POST参数
* @access private
* @var bool
*/
private $log_post = false;
/**
* 是否记录SESSION变量
* @access private
* @var bool
*/
private $log_session = false;
/**
* 目录名
* @access private
* @var string
*/
private $directory_name;
/**
* 控制器名
* @access private
* @var string
*/
private $controller_name;
/**
* 调用方法名
* @access private
* @var string
*/
private $action_name;
/**
* 是否分析执行SQL
* @access private
* @var bool
*/
private $sql_explain = false;
/**
* 记录GET参数
* @var int
*/
const LOG_GET = 0b1;
/**
* 记录POST参数
* @var int
*/
const LOG_POST = 0b10;
/**
* 记录GET和POST参数
* @var int
*/
const LOG_REQUEST = 0b11;
/**
* 记录SESSION变量
* @var int
*/
const LOG_SESSION = 0b100;
/**
* 记录执行的SQL语句
* @var int
*/
const LOG_QUERY = 0b1000;
/**
* 记录GET、POST、SESSION、SQL
* @var int
*/
const LOG_ALL = 0b1111;
/**
* 分析执行SQL
* @var int
*/
const SQL_EXPLAIN = 0b10000;
/**
* 构造函数
*
* @access public
* @param int|null $log_option 要记录的内容(GET、POST、SESSION、QUERY)
*/
public function __construct($log_option = NULL) {
$this->CI =& get_instance();
$this->directory_name = empty($this->CI->router->directory) ? '' : substr($this->CI->router->directory, 0, - 1);
$this->controller_name = empty($this->CI->router->class) ? '' : $this->CI->router->class;
$this->action_name = empty($this->CI->router->method) ? '' : $this->CI->router->method;
if (isset($log_option))
{
$this->get($log_option & self::LOG_GET);
$this->post($log_option & self::LOG_POST);
$this->session($log_option & self::LOG_SESSION);
$this->queries($log_option & self::LOG_QUERY);
$this->explain_sql($log_option & self::SQL_EXPLAIN);
}
}
/**
* 魔术方法,处理未定义的方法名
*
* 处理的方法名包含get、post、session、require、queries、save_get、save_post、save_get_post、save_post_get、
* save_request_session_queries、console_get、console_post、console_get_post、console_post_get、
* console_request_session_queries ...
*
* @access public
* @param string $name 方法名
* @param array $arguments 调用方法时传递的参数
* @return ExLogger|void get、post、session、require和queries方法返回$this,save开头的方法返回void
*/
public function __call($name, $arguments) {
$arg = empty($arguments) ? true : $arguments[0];
switch ($name)
{
case 'get':
case 'post':
case 'session':
$this->{'log_'.$name} = $arg;
break;
case 'request':
$this->log_get = $arg;
$this->log_post = $arg;
break;
case 'queries':
if ($arg) {
$this->queries = $this->get_queries();
}
$this->log_query = $arg;
break;
default:
$func_name_ary = explode('_', $name);
if ($func_name_ary[0] === 'save' || $func_name_ary[0] === 'console')
{
$func_name_first = array_shift($func_name_ary);
if (!empty($func_name_ary) && $func_name_ary[0] === 'save' || $func_name_ary[0] === 'console')
{
$func_name_second = array_shift($func_name_ary);
if ($func_name_second === $func_name_first)
{
trigger_error('Call to undefined function ' . $name, E_USER_ERROR);
}
}
foreach ($func_name_ary as $item)
{
call_user_func(array($this, $item), empty($arguments) ? array(true) : $arguments);
}
$this->{$func_name_first}();
if (isset($func_name_second))
{
$this->{$func_name_second}();
}
}
else
{
trigger_error('Call to undefined function ' . $name, E_USER_ERROR);
}
break;
}
return $this;
}
/**
* 是否分析执行SQL
*
* @access public
* @param bool $sql_explain 是否分析执行SQL
* @return ExLogger
*/
public function explain_sql($sql_explain) {
$this->sql_explain = $sql_explain;
return $this;
}
/**
* 获取本次访问执行的所有SQL语句
*
* @access protected
* @return array 本次访问执行的所有SQL语句
*/
protected function get_queries() {
$queries = array();
$db = NULL;
foreach (get_object_vars($this->CI) as $property_name => $property_value)
{
if (is_object($property_value))
{
if ($property_value instanceof CI_DB)
{
$db = $property_value;
}
elseif ($property_value instanceof CI_Model)
{
foreach (get_object_vars($property_value) as $model_property_name => $model_property_value) {
if ($model_property_value instanceof CI_DB)
{
$db = $model_property_value;
break 2;
}
}
}
}
}
if (!is_null($db)) {
$query_sqls = $db->queries;
$query_times = $db->query_times;
foreach ($query_sqls as $index => $sql) {
$sql_detail = array('sql' => preg_replace('/\s+/', ' ', trim($sql)), 'time' => $query_times[$index]);
if ($this->sql_explain) {
$query = $db->query('EXPLAIN ' . $sql);
$sql_detail['explain'] = $query->result_array();
}
$queries[] = $sql_detail;
}
}
return $queries;
}
/**
* 将记录的数据保存到日志文件
*
* @access public
* @return void
*/
public function save() {
$log_path = ($this->CI->config->item('log_path') !== '') ? $this->CI->config('log_path')
: APPPATH . 'logs' . DIRECTORY_SEPARATOR;
if (!file_exists($log_path))
{
mkdir($log_path, 0755, $log_path);
}
$log_file = 'exlog-'.date('Y-m-d').'.php';
if ($fp = @fopen($log_path.$log_file, 'ab'))
{
flock($fp, LOCK_EX);
$request_message = date('Y-m-d H:i:s') . "\t" .
(empty($this->directory_name) ? '' : ($this->directory_name . '/')) .
$this->controller_name . ' => ' . $this->action_name . "\n";
fwrite($fp, $request_message);
if ($this->log_get)
{
fwrite($fp, str_repeat('=', 100)."\n");
fwrite($fp, 'GET:'.(empty($_GET) ? 'Empty' : '') . "\n");
if (!empty($_GET))
{
foreach ($_GET as $key => $value)
{
if (is_array($value))
{
fwrite($fp, $key . ":\t" . var_export($value, true) . "\n");
}
else
{
fwrite($fp, $key . ":\t" . $value . "\n");
}
}
}
}
if ($this->log_post)
{
fwrite($fp, str_repeat('-', 100)."\n");
fwrite($fp, 'POST:'.(empty($_POST) ? 'Empty' : '') . "\n");
if (!empty($_POST))
{
foreach ($_POST as $key => $value)
{
if (is_array($value))
{
fwrite($fp, $key . ":\t" . var_export($value, true) . "\n");
}
else
{
fwrite($fp, $key . ":\t" . $value . "\n");
}
}
}
}
if ($this->log_session)
{
fwrite($fp, str_repeat('-', 100) . "\n");
fwrite($fp, 'SESSION:'.(empty($_SESSION) ? 'Empty' : '') . "\n");
if (!empty($_SESSION))
{
foreach ($_SESSION as $key => $value)
{
if (is_array($value))
{
fwrite($fp, $key . ":\t" . var_export($value, true) . "\n");
}
else
{
fwrite($fp, $key . ":\t" . $value . "\n");
}
}
}
}
if ($this->log_query)
{
fwrite($fp, str_repeat('-', 100) . "\n");
fwrite($fp, 'QUERY:'.(empty($this->queries) ? 'Empty' : '') . "\n");
foreach($this->queries as $key => $value)
{
fwrite($fp, ($key + 1) . ":\t(" . $value['time'] . " second)\t" . $value['sql'] . "\n");
if ($this->sql_explain && isset($value['explain']))
{
foreach ($value['explain'] as $explain)
{
fwrite($fp, str_repeat('=', 100) . "\n");
foreach ($explain as $explain_key => $explain_value)
{
fwrite($fp, $explain_key . ":\t" . $explain_value . "\n");
}
}
fwrite($fp, "\n");
}
}
}
fwrite($fp, "\n\n");
flock($fp, LOCK_UN);
fclose($fp);
}
}
/**
* 将记录信息保存到HTTP HEAD中
*
* @access public
* @return void
*/
public function console() {
$profiler_data = array(
'DIRECTORY' => $this->directory_name,
'CONTROLLER' => $this->controller_name,
'ACTION' => $this->action_name
);
if ($this->log_get && !empty($_GET))
{
$profiler_data['GET'] = $_GET;
}
if ($this->log_post && !empty($_POST))
{
$profiler_data['POST'] = $_POST;
}
if ($this->log_session && !empty($_SESSION))
{
$profiler_data['SESSION'] = $_SESSION;
}
if ($this->log_query && !empty($this->queries))
{
$profiler_data['QUERIES'] = $this->queries;
}
header('EXLOGGER: '.json_encode($profiler_data));
}
}