-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handler.php
177 lines (168 loc) · 6.52 KB
/
error_handler.php
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
<?php
$scriptStartTime=microtime();
$scriptStartMemory=memory_get_usage();
//error_reporting(0);
ini_set('display_errors',false);
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
register_shutdown_function('shutdownHandler');
/**
* 脚本结束时的处理函数
* @Author WindGreen<[email protected]>
* @DateTime 2017-03-20T13:29:22+0800
* @return [type] [description]
*/
function shutdownHandler()
{
//处理未处理的错误
$error = error_get_last();
if(is_array($error)){
$message=sprintf('SHUTDOWN_HANDLE %s[%d] %s:%d %s',
get_errno_name($error['type']),
isset($error['type']) ? $error['type'] : '',
isset($error['file']) ? $error['file'] : '',
isset($error['line']) ? $error['line'] : '',
isset($error['message']) ? $error['message'] : '');
exception_and_error_log($message,get_errno_type($error['type']));
}
//计算脚本执行时间
if(isset($GLOBALS['scriptStartTime']) && isset($GLOBALS['scriptStartMemory'])){
$scriptEndMemory=memory_get_usage();
$memoryUsed=$scriptEndMemory-$GLOBALS['scriptStartMemory'];
$memoryMaxUsed=memory_get_peak_usage()-$GLOBALS['scriptStartMemory'];
$scriptStartTime=explode(' ',$GLOBALS['scriptStartTime']);
$scriptEndTime=explode(' ',microtime());
$time=intval($scriptEndTime[1])-intval($scriptStartTime[1])+floatval($scriptEndTime[0])-floatval($scriptStartTime[0]);
$msg=sprintf('[Finished in %.4fs][MemoryOccupied/Peak/Used: %.2fMB/%.2fMB/%.2fMB] %s %s%s%s',
$time,
$memoryUsed/1024/1024,
$memoryMaxUsed/1024/1024,
$scriptEndMemory/1024/1024,
$_SERVER['REQUEST_METHOD'],
/*empty($_SERVER['HTTP_HOST']) ? '' :*/ $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'],
empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']);
exception_and_error_log($msg,'DEBUG');
}
}
/**
* 未处理的异常处理
* @Author WindGreen<[email protected]>
* @DateTime 2017-03-20T13:30:01+0800
* @param [type] $exception [description]
* @return [type] [description]
*/
function exceptionHandler($exception)
{
if($exception){
$message=sprintf('EXCEPTION_HANDLE %s:%d %s',
$exception->getFile(),
$exception->getLine(),
$exception->getMessage()
);
exception_and_error_log($message,'ERROR');
}
}
/**
* 错误处理,包括notice,warning和error级别的
* 参考 http://php.net/manual/zh/function.set-error-handler.php
* 这个函数可以被trigger_error触发
* @param [type] $errno [description]
* @param [type] $errstr [description]
* @param [type] $errfile [description]
* @param [type] $errline [description]
* @param [type] $errcontext [description]
* @return [type] [description]
*/
function errorHandler($errno,$errstr,$errfile,$errline,$errcontext=null)
{
//ref http://php.net/manual/zh/function.set-error-handler.php
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
$format='%s [%s] %s:%s %s';
switch ($errno) {
case E_USER_ERROR:
$message=sprintf($format,'E_USER_ERROR',$errno,$errfile,$errline,$errstr);
exception_and_error_log($message,get_errno_type($errno));
exit(1);
break;
/*
case E_USER_WARNING:
$message=sprintf($format,'USER_WARNING',$errno,$errfile,$errline,$errstr);
exception_and_error_log($message,'WARNING');
break;
case E_USER_NOTICE:
$message=sprintf($format,'USER_NOTICE',$errno,$errfile,$errline,$errstr);
exception_and_error_log($message,'NOTICE');
break;
*/
default:
//return false;
$message=sprintf($format,get_errno_name($errno),$errno,$errfile,$errline,$errstr);
exception_and_error_log($message,get_errno_type($errno));
break;
}
/* Don't execute PHP internal error handler */
return true;
}
/**
* 自定义错误日志处理
* @Author WindGreen<[email protected]>
* @DateTime 2017-03-20T13:32:12+0800
* @param [type] $message [description]
* @param string $level [description]
* @return [type] [description]
*/
function exception_and_error_log($message,$level='INFO')
{
echo $level,' ',$message,'<br>';
}
/**
* 通过错误码获取名称
* @Author WindGreen<[email protected]>
* @DateTime 2017-03-20T13:33:41+0800
* @param [type] $errno [description]
* @return [type] [description]
*/
function get_errno_name($errno)
{
$error=[
E_ERROR =>'E_ERROR', //1
E_WARNING =>'E_WARNING', //2
E_PARSE =>'E_PARSE', //4
E_NOTICE =>'E_NOTICE', //8
E_CORE_ERROR =>'E_CORE_ERROR', //16
E_CORE_WARNING =>'E_CORE_WARNING', //32
E_COMPILE_ERROR =>'E_COMPILE_ERROR', //64
E_COMPILE_WARNING =>'E_COMPILE_WARNING', //128
E_USER_ERROR =>'E_USER_ERROR', //256
E_USER_WARNING =>'E_USER_WARNING', //512
E_USER_NOTICE =>'E_USER_NOTICE', //1024
E_STRICT =>'E_STRICT', //2048
E_RECOVERABLE_ERROR =>'E_RECOVERABLE_ERROR',//4096
E_DEPRECATED =>'E_DEPRECATED', //8192
E_USER_DEPRECATED =>'E_USER_DEPRECATED', //16384
//E_ALL =>'E_ALL' //32767
];
if(isset($error[$errno]))
return $error[$errno];
else return '';
}
/**
* 通过错误码获取自定义错误类型
* @Author WindGreen<[email protected]>
* @DateTime 2017-03-20T13:33:54+0800
* @param [type] $errno [description]
* @return [type] [description]
*/
function get_errno_type($errno)
{
$error=get_errno_name($errno);
$type=substr($error, strrpos($error,'_')+1);
if(in_array($type,['ERROR','WARNING','NOTICE']))
return $type;
else return 'INFO';
}