Skip to content

Commit

Permalink
Added feature to log the request uri when fatal errors encountered. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksantamaria authored Oct 31, 2024
1 parent 0ce137e commit 930ce50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
34 changes: 34 additions & 0 deletions images/php/log-fatals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @file
*
* Adds a shutdown function that logs fatal errors along with the REQUEST_URI.
*
* Intention is to allow tracking of specific routes that exhaust memory limit.
*/

/**
* Returns a list of error types the custom error handler should respond to.
*/
function bay_fatal_error_types() {
return [
E_ERROR => '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));
}
});
5 changes: 5 additions & 0 deletions images/php/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 930ce50

Please sign in to comment.