forked from JamesMcFall/PHP-Error-Reporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorReporting.php
193 lines (157 loc) · 7.65 KB
/
ErrorReporting.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* PHP Custom Error Handler
*
* @package ErrorHandler
* @author James McFall <[email protected]>
* @author Charlie Lehardy <[email protected]>
*/
// Set the default error handler for php to this class
set_error_handler('ErrorHandler::handleError');
register_shutdown_function('ErrorHandler::shutdownHandler');
# @todo - Don't forget to set error numb threashold
# @todo - Log to screen in certain environments. Make so Bootstrapper can set up.
# @todo - Set default error log logation to apache's error log I guess
/**
* This class can be used to overwrite the default error handling behaviour
* of php and provide a more comprehensive error message
*
* @version 0.2
* @author James McFall <[email protected]>
* @author Charlie Lehardy <[email protected]>
*/
class ErrorHandler {
# Set up logging locations
public static $logViaFile = true;
public static $logViaEmail = true;
public static $logViaScreen = true;
# Error logging timezone
public static $errorLogTimezone = "America/Phoenix";
# Set who the error emails should be sent to
public static $emailTo = "[email protected]";
# Set error log file location
public static $logFileLocation = "/tmp/php_error_handler/errors.log";
/**
* This method is the main method that handles all of the functionality for
* the handler.
*
* @param int $errorCode - Level of the error raised.
* @param string $message - Error message.
* @param string $file - File of origin the error occurred in.
* @param int $lineNumber - Line number the error occured on in the file of origin.
* @param array $vars - A large array of all variables that exist in scope at the time of the error.
*/
public static function handleError($errorCode, $message, $file, $lineNumber, $vars = new Array()) {
# Send out email error notification.
if (self::$logViaEmail)
self::sendErrorEmail($errorCode, $message, $file, $lineNumber, $vars);
# Log the error to a file.
if (self::$logViaFile)
self::writeToErrorLog($errorCode, $message, $file, $lineNumber, $vars);
# Log the error to the screen
if (self::$logViaScreen) {
self::logErrorToScreen($errorCode, $message, $file, $lineNumber, $vars);
}
}
/**
* This method catches a shutdown of PHP and if it's because an E_ERROR was thrown, it
* will send the message to the error handler.
*
* @uses ErrorHandler::handleError()
*/
public static function handleShutdown() {
$last_error = error_get_last();
// Check if the last error is a fatal error.
if ($last_error['type'] === E_ERROR) {
self::handleError(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
}
}
/**
* This function can be used to overwrite the default error handling behaviour
* of php and provide a more comprehensive error message.
*
* This gives a large amount of data around the error as it can easily be fit
* into an email message.
*
* @param int $errorCode - Level of the error raised.
* @param string $message - Error message.
* @param string $file - File of origin the error occurred in.
* @param int $lineNumber - Line number the error occured on in the file of origin.
* @param array $vars - A large array of all variables that exist in scope at the time of the error.
*/
static function sendErrorEmail($errorCode, $message, $file, $lineNumber, $vars) {
$timezone = new DateTimeZone(self::$errorLogTimezone);
$time = new DateTime("now", $timezone);
# Start building an error message
$errorMessage = "<p><h2>An unhandled error occurred on http://" . $_SERVER['HTTP_HOST'] . "</h2></p>";
# Basic Error Details
$errorMessage .= "<p>";
$errorMessage .= "<b>Time Of Occurance</b>: " . $time->format('h:i:sa d M Y') . "<br />";
$errorMessage .= "<b>Error Number</b>: $errorCode<br />";
$errorMessage .= "<b>Line Number</b>: $lineNumber<br />";
$errorMessage .= "<b>File Name</b>: $file<br />";
$errorMessage .= "<b>Error</b>: $message<br />";
$errorMessage .= "</p>";
# Build Debug Backtrace
$backtrace = "<pre>" . print_r(debug_backtrace(), 1) . "</pre>";
# The current vars available at the time
$currentVars = "<pre>" . print_r($vars, 1) . "</pre>";
# Append additional details to error message
$errorMessage .= "<hr /><p><b>Debug Backtrace</b></p>" . $backtrace;
$errorMessage .= "<hr /><p><b>Current In Scope Variables</b></p>" . $currentVars;
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
error_log($errorMessage, 1, self::$emailTo, $headers);
}
/**
* This method writes the error message to the log file if it's available.
*
* @param int $errorCode - Level of the error raised.
* @param string $message - Error message.
* @param string $file - File of origin the error occurred in.
* @param int $lineNumber - Line number the error occured on in the file of origin.
* @param array $vars - A large array of all variables that exist in scope at the time of the error.
*/
public static function writeToErrorLog($errorCode, $message, $file, $lineNumber, $vars) {
$timezone = new DateTimeZone(self::$errorLogTimezone);
$time = new DateTime("now", $timezone);
# Start building an error message
$errorMessage = $time->format('h:i:sa d M Y') .": An unhandled error($errorCode) ";
$errorMessage .= "occurred on http://" . $_SERVER['HTTP_HOST'];
$errorMessage .= " in $file on line $lineNumber - $message";
# Prepend with new line
$errorMessage = "\n" . $errorMessage;
# Write to log file
error_log($errorMessage, 3, self::$logFileLocation);
}
/**
*
* @param int $number - Level of the error raised.
* @param string $message - Error message.
* @param string $file - File of origin the error occurred in.
* @param int $lineNumber - Line number the error occured on in the file of origin.
* @param array $vars - A large array of all variables that exist in scope at the time of the error.
*/
public static function logErrorToScreen($number, $message, $file, $lineNumber, $vars) {
$timezone = new DateTimeZone(self::$errorLogTimezone);
$time = new DateTime("now", $timezone);
# Start building an error message
$errorMessage = "<p><h2>An unhandled error occurred on http://" . $_SERVER['HTTP_HOST'] . "</h2></p>";
# Basic Error Details
$errorMessage .= "<p>";
$errorMessage .= "<b>Time Of Occurance</b>: " . $time->format('h:i:sa d M Y') . "<br />";
$errorMessage .= "<b>Error Number</b>: $number<br />";
$errorMessage .= "<b>Line Number</b>: $lineNumber<br />";
$errorMessage .= "<b>File Name</b>: $file<br />";
$errorMessage .= "<b>Error</b>: $message<br />";
$errorMessage .= "</p>";
# Build Debug Backtrace
$backtrace = "<pre>" . print_r(debug_backtrace(), 1) . "</pre>";
# The current vars available at the time
$currentVars = "<pre>" . print_r($vars, 1) . "</pre>";
# Append additional details to error message
$errorMessage .= "<hr /><p><b>Debug Backtrace</b></p>" . $backtrace;
$errorMessage .= "<hr /><p><b>Current In Scope Variables</b></p>" . $currentVars;
echo $errorMessage;
exit();
}
}