-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessWireSentry.module.php
192 lines (160 loc) · 6.51 KB
/
ProcessWireSentry.module.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
<?php
namespace ProcessWire;
// Include Composer's autoload file
require_once (__DIR__ . '/vendor/autoload.php');
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class ProcessWireSentry extends WireData implements Module
{
private $previousErrorHandler;
private $previousExceptionHandler;
public static function getModuleInfo()
{
return [
'title' => 'Sentry Integration',
'version' => 1,
'summary' => 'Sentry integration for error logging in ProcessWire',
'author' => 'https://github.com/ryntab',
'icon' => 'bug',
'requires' => ['PHP>=8.2', 'ProcessWire>=3.0.0'],
'singular' => true,
'autoload' => true
];
}
public function init()
{
// Include Composer's autoload file
require_once (__DIR__ . '/vendor/autoload.php');
// Create a logger instance
$logger = new Logger('sentry');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));
// Get the DSN and traces_sample_rate from the module configuration
$dsn = $this->wire('modules')->getConfig('ProcessWireSentry', 'dsn');
$debug = $this->wire('modules')->getConfig('ProcessWireSentry', 'debug_mode');
$localLog = $this->wire('modules')->getConfig('ProcessWireSentry', 'local_log');
$tracesSampleRate = $this->wire('modules')->getConfig('ProcessWireSentry', 'traces_sample_rate');
$jsCDN = $this->wire('modules')->getConfig('ProcessWireSentry', 'js_cdn');
// Sentry initialization with enhanced logging
\Sentry\init([
'dsn' => $dsn,
'traces_sample_rate' => (float) $tracesSampleRate,
'logger' => $logger,
]);
if ($jsCDN) {
// Check if the current request is not in the admin backend
if (!$this->wire('config')->ajax && !$this->wire('config')->admin) {
$this->wire('config')->scripts->add($jsCDN);
}
}
// Debug: Check if DSN and traces_sample_rate are being set
if (empty($dsn)) {
$this->logLocally('DSN is not set in the configuration');
return;
}
if (empty($tracesSampleRate)) {
$this->logLocally('traces_sample_rate is not set in the configuration');
return;
}
if ($debug) {
try {
throw new \Exception('Test exception from ProcessWireSentry module');
} catch (\Exception $e) {
\Sentry\captureException($e);
$this->logLocally('Debug mode enabled, exception captured');
}
}
// Store previous error and exception handlers
$this->previousErrorHandler = set_error_handler([$this, 'handleError']);
$this->previousExceptionHandler = set_exception_handler([$this, 'handleException']);
register_shutdown_function([$this, 'handleShutdown']);
// Hook into ProcessPageView::finished to capture notices after page render
$this->addHookAfter('ProcessPageView::finished', $this, 'captureNotices');
// Hook into WireLog::save to capture error logs
$this->addHookAfter('WireLog::save', $this, 'captureLogErrors');
}
public function handleError($errno, $errstr, $errfile, $errline)
{
// Check if error reporting is turned off
if (!(error_reporting() & $errno)) {
return false;
}
$errorTypes = [
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict Notice',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
];
$errorType = isset($errorTypes[$errno]) ? $errorTypes[$errno] : 'Unknown Error';
$message = "$errorType: $errstr in $errfile on line $errline";
// Debug: Log the error message
wire('log')->save('sentry', 'Handling error: ' . $message);
// Send error to Sentry
\Sentry\captureMessage($message);
// Call previous error handler
if ($this->previousErrorHandler) {
return call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline);
}
// Ensure default error handling
return false;
}
public function handleException($exception)
{
// Debug: Log the exception message
$this->logLocally('Handling exception: ' . $exception->getMessage());
// Send exception to Sentry
\Sentry\captureException($exception);
// Call previous exception handler
if ($this->previousExceptionHandler) {
call_user_func($this->previousExceptionHandler, $exception);
} else {
// If no previous handler, use default handler
echo $exception;
}
}
public function handleShutdown()
{
$error = error_get_last();
if ($error !== null) {
$this->handleError($error['type'], $error['message'], $error['file'], $error['line']);
}
}
public function captureNotices(HookEvent $event)
{
$notices = wire('notices');
$this->logLocally('Capturing notices: ' . count($notices));
foreach ($notices as $notice) {
$text = $notice instanceof NoticeError ? "Error" : "Message";
if ($notice instanceof NoticeError) {
\Sentry\captureMessage("{$text}: {$notice->text}");
} else {
\Sentry\captureMessage("{$text}: {$notice->text}");
}
}
}
public function captureLogErrors(HookEvent $event)
{
$message = $event->arguments(0); // Log message
$name = $event->arguments(1); // Log type (name)
if ($name === 'errors' || $name === 'exceptions') {
$this->logLocally('Capturing log error: ' . $message);
\Sentry\captureMessage($message);
}
}
public function logLocally($message)
{
if ($this->wire('modules')->getConfig('ProcessWireSentry', 'local_log')) {
$this->wire('log')->save('sentry', $message);
}
}
}