forked from wp-staging/wp-staging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-staging-error-handler.php
135 lines (114 loc) · 4.31 KB
/
wp-staging-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
<?php
/*
* Low-level error handler and debugger for WP STAGING
*/
namespace WPStaging\functions;
/**
* @param string $message The debug message.
* @param string $logType A PSR-3 compatible-log type. If "debug", it only logs if WPSTG_DEBUG is true.
* @param bool $logInDebugLog Whether to log the message in the WP_DEBUG_LOG file.
*
* @see \Psr\Log\LogLevel
*/
function debug_log($message, $logType = 'info', $logInDebugLog = true)
{
// Keep the file handler open for the duration of the request for performance.
static $fileHandler;
if ($logType === 'debug' && !defined('WPSTG_DEBUG') || defined('WPSTG_DEBUG') && !WPSTG_DEBUG) {
return;
}
if ($logInDebugLog && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
error_log('[' . $logType . '] WP Staging - ' . $message, 0);
}
if (!defined('WPSTG_DEBUG_LOG_FILE')) {
return;
}
if (is_null($fileHandler)) {
// Open the file handler once per request, and keep it open, as we might need to write to it multiple times.
$fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a');
// On Windows OS we need to remove the lock handle first before locking it again.
if (stripos(PHP_OS, "WIN") === 0) {
flock($fileHandler, LOCK_UN);
}
// Make sure the lock is shared, as we might need to open the handler again if a fatal error occurs.
if (is_resource($fileHandler)) {
flock($fileHandler, LOCK_SH | LOCK_NB);
}
}
$message = sprintf(
"[WP STAGING Manual Logging][%s][%s] %s\n",
$logType,
current_time('mysql'),
$message
);
if (is_resource($fileHandler)) {
fwrite($fileHandler, $message, 5 * MB_IN_BYTES);
}
}
/**
* Logs fatal errors in the WP STAGING debug file.
*/
function shutdown_function()
{
if (!defined('WPSTG_DEBUG_LOG_FILE') || !defined('WPSTG_PLUGIN_SLUG')) {
return;
}
$error = error_get_last();
if (!is_array($error)) {
return;
}
// Errors that bring PHP to a halt.
$fatalErrorTypes = [
E_ERROR => 'E_ERROR',
E_PARSE => 'E_PARSE',
E_USER_ERROR => 'E_USER_ERROR',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
];
// Provide friendly-names for the error codes
$allErrorTypes = [
E_ERROR => "E_ERROR",
E_WARNING => "E_WARNING",
E_PARSE => "E_PARSE",
E_NOTICE => "E_NOTICE",
E_CORE_ERROR => "E_CORE_ERROR",
E_CORE_WARNING => "E_CORE_WARNING",
E_COMPILE_ERROR => "E_COMPILE_ERROR",
E_COMPILE_WARNING => "E_COMPILE_WARNING",
E_USER_ERROR => "E_USER_ERROR",
E_USER_WARNING => "E_USER_WARNING",
E_USER_NOTICE => "E_USER_NOTICE",
E_STRICT => "E_STRICT",
E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
E_DEPRECATED => "E_DEPRECATED",
E_USER_DEPRECATED => "E_USER_DEPRECATED",
E_ALL => "E_ALL",
];
$isFatalError = isset($fatalErrorTypes[$error['type']]);
$comesFromWpStaging = strpos($error['file'], WPSTG_PLUGIN_SLUG) !== false;
/*
* Logs fatal errors that happens anywhere,
* and notices, warnings that comes from a WP STAGING file.
*
* (It will only log notices and errors from WP STAGING
* if it was the last notice/warning triggered before PHP shutdown)
*/
if ($isFatalError || $comesFromWpStaging) {
// Opening a file handler gives us more control than error_log('foo', 3, 'custom-file.log');
$fileHandler = @fopen(WPSTG_DEBUG_LOG_FILE, 'a');
$message = sprintf(
"[WP STAGING Shutdown Function][%s][%s] %s - File: %s Line: %s | Is it Fatal Error? %s | Is it Thrown by WP STAGING? %s\n",
$allErrorTypes[$error['type']],
current_time('mysql'),
$error['message'],
$error['file'],
$error['line'],
$isFatalError ? 'Yes' : 'No',
$comesFromWpStaging ? 'Yes' : 'No'
);
if (is_resource($fileHandler)) {
fwrite($fileHandler, $message, 5 * MB_IN_BYTES);
}
}
}
register_shutdown_function('\WPStaging\functions\shutdown_function');