-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorion_logger.php
More file actions
249 lines (213 loc) Β· 9.97 KB
/
Copy pathorion_logger.php
File metadata and controls
249 lines (213 loc) Β· 9.97 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
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/*
Module Name: Orion Logger
Description: Forwards PHP errors, warnings, and Perfex CRM hook events to the Orion centralized logging platform.
Version: 1.0.0
Module URI: https://github.com/TipiCode/PFX-Orion
Author: tipi(code)
Requires at least: 3.0.0
*/
// ββββββββββββββββββββββββββββββββββββββββββββββ
// Language registration
// ββββββββββββββββββββββββββββββββββββββββββββββ
register_language_files('orion_logger', ['orion_logger']);
// ββββββββββββββββββββββββββββββββββββββββββββββ
// Activation / deactivation hooks
// ββββββββββββββββββββββββββββββββββββββββββββββ
register_activation_hook('orion_logger', 'orion_logger_activation');
register_deactivation_hook('orion_logger', 'orion_logger_deactivation');
function orion_logger_activation(): void
{
// Seed default option values on first activation
$defaults = [
'orion_logger_base_url' => '',
'orion_logger_api_key' => '',
'orion_logger_environment' => 'production',
'orion_logger_capture_errors' => '1',
'orion_logger_capture_hooks' => '1',
'orion_logger_min_level' => 'Information',
];
foreach ($defaults as $key => $value) {
if (get_option($key) === '') {
add_option($key, $value);
}
}
}
function orion_logger_deactivation(): void
{
// Nothing to tear down β options are kept so re-activation is seamless.
}
// ββββββββββββββββββββββββββββββββββββββββββββββ
// Admin initialisation
// ββββββββββββββββββββββββββββββββββββββββββββββ
hooks()->add_action('admin_init', 'orion_logger_admin_init');
function orion_logger_admin_init(): void
{
$CI = &get_instance();
$CI->app_menu->add_setup_menu_item('orion_logger', [
'name' => _l('orion_logger'),
'icon' => 'fa fa-satellite-dish',
'position' => 60,
'href' => admin_url('orion_logger/settings'),
'badge' => [],
]);
orion_logger_boot();
}
// ββββββββββββββββββββββββββββββββββββββββββββββ
// Boot: load the library and register handlers
// ββββββββββββββββββββββββββββββββββββββββββββββ
function orion_logger_boot(): void
{
$CI = &get_instance();
$CI->load->library('orion_logger/Orion_logger_lib');
// PHP error / warning capture
if (get_option('orion_logger_capture_errors') == '1') {
orion_logger_register_error_handlers();
}
// Perfex hook event capture
if (get_option('orion_logger_capture_hooks') == '1') {
orion_logger_register_hook_listeners();
}
}
// ββββββββββββββββββββββββββββββββββββββββββββββ
// Global helper available everywhere in Perfex
// ββββββββββββββββββββββββββββββββββββββββββββββ
if (!function_exists('orion_log')) {
/**
* Send a log entry to Orion from anywhere in Perfex or custom modules.
*
* @param string $message Human-readable description
* @param string $level Debug | Info | Warning | Error | Critical
* @param string $sourceContext Dot-notation origin (e.g. "MyModule.Controller")
* @param string|null $exception Stack trace or extra context (optional)
*/
function orion_log(
string $message,
string $level = 'Error',
string $sourceContext = 'Perfex',
?string $exception = null
): void {
$CI = &get_instance();
if (!isset($CI->orion_logger_lib)) {
$CI->load->library('orion_logger/Orion_logger_lib');
}
$CI->orion_logger_lib->log($message, $level, $sourceContext, $exception);
}
}
// ββββββββββββββββββββββββββββββββββββββββββββββ
// PHP error / exception handlers
// ββββββββββββββββββββββββββββββββββββββββββββββ
function orion_logger_register_error_handlers(): void
{
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline): bool {
// Respect the current error_reporting mask (e.g. @ operator silences errors)
if (!(error_reporting() & $errno)) {
return false;
}
$CI = &get_instance();
$level = Orion_logger_lib::phpErrnoToLevel($errno);
$CI->orion_logger_lib->log(
$errstr,
$level,
'Perfex.PHP',
"File: {$errfile}:{$errline}"
);
// Return false so PHP's internal handler also runs (keeps native error logging)
return false;
});
set_exception_handler(function (Throwable $e): void {
$CI = &get_instance();
$CI->orion_logger_lib->log(
get_class($e) . ': ' . $e->getMessage(),
'Critical',
'Perfex.Exception',
$e->getTraceAsString()
);
});
}
// ββββββββββββββββββββββββββββββββββββββββββββββ
// Perfex hook event listeners
// ββββββββββββββββββββββββββββββββββββββββββββββ
function orion_logger_register_hook_listeners(): void
{
// Clients
hooks()->add_action('after_client_created', 'orion_logger_on_client_created');
// Leads
hooks()->add_action('lead_converted_to_customer', 'orion_logger_on_lead_converted');
// Invoices
hooks()->add_action('after_invoice_added', 'orion_logger_on_invoice_added');
// Estimates
hooks()->add_action('after_estimate_added', 'orion_logger_on_estimate_added');
hooks()->add_action('estimate_accepted', 'orion_logger_on_estimate_accepted');
hooks()->add_action('estimate_declined', 'orion_logger_on_estimate_declined');
// Payments
hooks()->add_action('after_payment_added', 'orion_logger_on_payment_added');
// Projects
hooks()->add_action('after_add_project', 'orion_logger_on_project_added');
// Tasks
hooks()->add_action('task_status_changed', 'orion_logger_on_task_status_changed');
// Tickets
hooks()->add_action('ticket_created', 'orion_logger_on_ticket_created');
hooks()->add_action('after_ticket_status_changed', 'orion_logger_on_ticket_status_changed');
}
// ββ Client ββββββββββββββββββββββββββββββββββββββ
function orion_logger_on_client_created(array $data): void
{
orion_log("Client created (ID: {$data['id']})", 'Information', 'Perfex.Clients');
}
// ββ Lead ββββββββββββββββββββββββββββββββββββββββ
function orion_logger_on_lead_converted(array $data): void
{
orion_log("Lead converted to client (Lead ID: {$data['lead_id']}, Client ID: {$data['customer_id']})", 'Information', 'Perfex.Leads');
}
// ββ Invoices ββββββββββββββββββββββββββββββββββββ
function orion_logger_on_invoice_added(int $invoiceId): void
{
orion_log("Invoice created (ID: {$invoiceId})", 'Information', 'Perfex.Invoices');
}
// ββ Estimates βββββββββββββββββββββββββββββββββββ
function orion_logger_on_estimate_added(int $estimateId): void
{
orion_log("Estimate created (ID: {$estimateId})", 'Information', 'Perfex.Estimates');
}
function orion_logger_on_estimate_accepted(int $estimateId): void
{
orion_log("Estimate accepted (ID: {$estimateId})", 'Information', 'Perfex.Estimates');
}
function orion_logger_on_estimate_declined(int $estimateId): void
{
orion_log("Estimate declined (ID: {$estimateId})", 'Warning', 'Perfex.Estimates');
}
// ββ Payments ββββββββββββββββββββββββββββββββββββ
function orion_logger_on_payment_added(int $paymentId): void
{
orion_log("Payment recorded (ID: {$paymentId})", 'Information', 'Perfex.Payments');
}
// ββ Projects ββββββββββββββββββββββββββββββββββββ
function orion_logger_on_project_added(int $projectId): void
{
orion_log("Project created (ID: {$projectId})", 'Information', 'Perfex.Projects');
}
// ββ Tasks βββββββββββββββββββββββββββββββββββββββ
function orion_logger_on_task_status_changed(array $data): void
{
orion_log(
"Task status changed to {$data['status']} (Task ID: {$data['task_id']})",
'Information',
'Perfex.Tasks'
);
}
// ββ Tickets βββββββββββββββββββββββββββββββββββββ
function orion_logger_on_ticket_created(int $ticketId): void
{
orion_log("Support ticket created (ID: {$ticketId})", 'Information', 'Perfex.Tickets');
}
function orion_logger_on_ticket_status_changed(array $data): void
{
orion_log(
"Ticket status changed to {$data['status']} (Ticket ID: {$data['id']})",
'Information',
'Perfex.Tickets'
);
}