From 86eb1dddbd0d6c373ed80f8314d9918d0d82f557 Mon Sep 17 00:00:00 2001 From: Marek Miklewicz Date: Tue, 2 Jun 2026 22:57:23 +0200 Subject: [PATCH] feat: log plugin runtime errors --- exec/bootstrap.php | 4 +++ exec/handlers/create.php | 1 + exec/handlers/delete.php | 1 + exec/handlers/toggle.php | 27 ++++++++------ exec/handlers/update.php | 1 + exec/lib/PluginLogger.php | 70 ++++++++++++++++++++++++++++++++++++ plugin.conf | 2 +- tests/plugin_logger_test.php | 16 +++++++++ 8 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 exec/lib/PluginLogger.php create mode 100644 tests/plugin_logger_test.php diff --git a/exec/bootstrap.php b/exec/bootstrap.php index 2f600de..06ca659 100644 --- a/exec/bootstrap.php +++ b/exec/bootstrap.php @@ -10,6 +10,9 @@ if (!defined('IN_DA_PLUGIN') || IN_DA_PLUGIN !== true) { define('PLUGIN_ROOT', dirname(__DIR__)); define('PLUGIN_EXEC_DIR', __DIR__); +require_once PLUGIN_EXEC_DIR . '/lib/PluginLogger.php'; +PluginLogger::init(PLUGIN_ROOT . '/plugin.log'); + foreach ([ 'Settings.php', 'BackendRuntimeConfig.php', @@ -58,6 +61,7 @@ try { } $handler($ctx); } catch (Throwable $e) { + PluginLogger::exception($e, 'BOOTSTRAP'); http_response_code(500); header('Content-Type: text/html; charset=UTF-8'); echo 'Błąd'; diff --git a/exec/handlers/create.php b/exec/handlers/create.php index 5661994..f44f77e 100644 --- a/exec/handlers/create.php +++ b/exec/handlers/create.php @@ -18,6 +18,7 @@ return static function (AppContext $ctx): void { } Http::redirect($ctx->url('index.html', ['status' => 'created'])); } catch (Throwable $e) { + PluginLogger::exception($e, 'CREATE'); $errors[] = $e->getMessage(); } } diff --git a/exec/handlers/delete.php b/exec/handlers/delete.php index d9c225f..c2a3516 100644 --- a/exec/handlers/delete.php +++ b/exec/handlers/delete.php @@ -16,6 +16,7 @@ return static function (AppContext $ctx): void { $ctx->audit->record('delete', $ctx->daUser->username(), ['rule_id' => $id, 'subject_prefix' => (string)($rule['subject_prefix'] ?? $rule['subject'] ?? '')]); Http::redirect($ctx->url('index.html', ['status' => 'deleted'])); } catch (Throwable $e) { + PluginLogger::exception($e, 'DELETE'); $ctx->render('Confirm deletion', '
' . AppContext::e($e->getMessage()) . '
'); return; } diff --git a/exec/handlers/toggle.php b/exec/handlers/toggle.php index 72c800d..e7865d3 100644 --- a/exec/handlers/toggle.php +++ b/exec/handlers/toggle.php @@ -2,16 +2,21 @@ declare(strict_types=1); return static function (AppContext $ctx): void { - $ctx->requirePost(); - $id = $ctx->post('id'); - $ctx->requireCsrf('toggle:' . $id); - $rule = $ctx->rules->find($ctx->daUser->username(), $id); - if ($rule === null) { - throw new RuntimeException($ctx->t('Rule not found')); + try { + $ctx->requirePost(); + $id = $ctx->post('id'); + $ctx->requireCsrf('toggle:' . $id); + $rule = $ctx->rules->find($ctx->daUser->username(), $id); + if ($rule === null) { + throw new RuntimeException($ctx->t('Rule not found')); + } + $next = ['enabled' => empty($rule['enabled'])]; + enforce_rule_backend_constraints($ctx, $id, $next); + $ctx->rules->update($ctx->daUser->username(), $id, $next); + $ctx->syncActiveBackend(); + Http::redirect($ctx->url('index.html', ['status' => 'toggled'])); + } catch (Throwable $e) { + PluginLogger::exception($e, 'TOGGLE'); + throw $e; } - $next = ['enabled' => empty($rule['enabled'])]; - enforce_rule_backend_constraints($ctx, $id, $next); - $ctx->rules->update($ctx->daUser->username(), $id, $next); - $ctx->syncActiveBackend(); - Http::redirect($ctx->url('index.html', ['status' => 'toggled'])); }; diff --git a/exec/handlers/update.php b/exec/handlers/update.php index f360fb8..ae303e1 100644 --- a/exec/handlers/update.php +++ b/exec/handlers/update.php @@ -19,6 +19,7 @@ return static function (AppContext $ctx): void { $ctx->syncActiveBackend(); Http::redirect($ctx->url('index.html', ['status' => 'updated'])); } catch (Throwable $e) { + PluginLogger::exception($e, 'UPDATE'); $errors[] = $e->getMessage(); } } diff --git a/exec/lib/PluginLogger.php b/exec/lib/PluginLogger.php new file mode 100644 index 0000000..fa5ad07 --- /dev/null +++ b/exec/lib/PluginLogger.php @@ -0,0 +1,70 @@ +getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() . "\n" . $e->getTraceAsString()); + } + + public static function log(string $level, string $message): void + { + $path = self::$path !== '' ? self::$path : (defined('PLUGIN_ROOT') ? PLUGIN_ROOT . '/plugin.log' : __DIR__ . '/../../plugin.log'); + $line = '[' . gmdate('c') . '] ' . $level . ' ' . str_replace(["\r\n", "\r"], "\n", $message) . "\n"; + $ok = @file_put_contents($path, $line, FILE_APPEND | LOCK_EX); + if ($ok !== false && is_file($path)) { + @chmod($path, 0600); + } + } + + private static function severityName(int $severity): string + { + return match ($severity) { + 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', + default => 'E_' . $severity, + }; + } +} diff --git a/plugin.conf b/plugin.conf index d98456b..d6b47db 100644 --- a/plugin.conf +++ b/plugin.conf @@ -2,7 +2,7 @@ name=global-autoresponder id=global-autoresponder type=user author=HITME.PL -version=1.1.1 +version=1.1.2 active=no installed=no user_run_as=root diff --git a/tests/plugin_logger_test.php b/tests/plugin_logger_test.php new file mode 100644 index 0000000..0f00cdd --- /dev/null +++ b/tests/plugin_logger_test.php @@ -0,0 +1,16 @@ +