From 2988982cae05040180d1fe1f2de5f8aae8682af2 Mon Sep 17 00:00:00 2001 From: Nick Santamaria Date: Thu, 31 Oct 2024 15:21:28 +1100 Subject: [PATCH] Added feature to log the request uri when fatal errors encountered. --- images/php/log-fatals.php | 34 ++++++++++++++++++++++++++++++++++ images/php/settings.php | 5 +++++ 2 files changed, 39 insertions(+) create mode 100644 images/php/log-fatals.php diff --git a/images/php/log-fatals.php b/images/php/log-fatals.php new file mode 100644 index 00000000..01b7e9d3 --- /dev/null +++ b/images/php/log-fatals.php @@ -0,0 +1,34 @@ + 'E_ERROR', + E_PARSE => 'E_PARSE', + E_CORE_ERROR => 'E_CORE_ERROR', + E_COMPILE_ERROR => 'E_COMPILE_ERROR', + ]; +} + +register_shutdown_function(function() { + $error = error_get_last(); + $types = bay_fatal_error_types(); + if ($error && in_array($error['type'], array_keys($types))) { + $data = [ + 'type' => $types[$error['type']], + 'message' => $error['message'], + 'request_uri' => $_SERVER['REQUEST_URI'] ?: 'N/A', + ]; + error_log("[BAY_FATAL] " . json_encode($data)); + } +}); diff --git a/images/php/settings.php b/images/php/settings.php index 305716b8..755fee56 100755 --- a/images/php/settings.php +++ b/images/php/settings.php @@ -21,6 +21,11 @@ $contrib_path = $app_root . DIRECTORY_SEPARATOR . (is_dir('modules/contrib') ? 'modules/contrib' : 'modules'); $lagoon_env_type = getenv('LAGOON_ENVIRONMENT_TYPE') ?: 'local'; +// Feature flag that enables some enhanced logging of fatal errors. +if (getenv('BAY_PHP_FATAL_LOGS') == "true") { + include $bay_settings_path . '/log-fatals.php'; +} + // Database connection. $connection_info = [ 'driver' => 'mysql',