From 286c32ac268ae9678f5b557813a3ba2221f9f13e Mon Sep 17 00:00:00 2001 From: Lai Wei Date: Tue, 4 Feb 2025 10:04:17 +0000 Subject: [PATCH] Move debug backtrace of auth_oidc error details from Moodle log to PHP error log --- auth/oidc/classes/utils.php | 43 +++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/auth/oidc/classes/utils.php b/auth/oidc/classes/utils.php index f0753f74d..c6d4ee141 100644 --- a/auth/oidc/classes/utils.php +++ b/auth/oidc/classes/utils.php @@ -118,18 +118,53 @@ public static function tostring($val) { public static function debug($message, $where = '', $debugdata = null) { $debugmode = (bool)get_config('auth_oidc', 'debugmode'); if ($debugmode === true) { - $backtrace = debug_backtrace(); - $otherdata = [ + $debugbacktrace = debug_backtrace(); + $debugbacktracechecksum = md5(json_encode($debugbacktrace)); + + $otherdata = static::make_json_safe([ 'other' => [ 'message' => $message, 'where' => $where, 'debugdata' => $debugdata, - 'backtrace' => $backtrace, + 'backtrace_checksum' => $debugbacktracechecksum, ], - ]; + ]); $event = action_failed::create($otherdata); $event->trigger(); + + $debugbacktracedata = [ + 'checksum' => $debugbacktracechecksum, + 'backtrace' => $debugbacktrace, + ]; + + error_log(print_r($debugbacktracedata, true)); + } + } + + /** + * Make a JSON structure safe for logging. + * + * @param mixed $data The data to make safe. + * @return mixed The safe data. + */ + private static function make_json_safe($data) { + if (is_array($data)) { + foreach ($data as $key => $value) { + $data[$key] = static::make_json_safe($value); + } + } else if (is_object($data)) { + $data = (array)$data; + foreach ($data as $key => $value) { + $data[$key] = static::make_json_safe($value); + } + } else if (is_bool($data)) { + $data = (int)$data; + } else if (is_null($data)) { + $data = null; + } else if (!is_scalar($data)) { + $data = (string)$data; } + return $data; } /**