From 967ed4225b8fd6ecb8b5cc5cff638da068a26ee3 Mon Sep 17 00:00:00 2001 From: nguyenanhung Date: Wed, 29 Mar 2023 11:08:04 +0700 Subject: [PATCH 1/2] In log_message - If message not string, force string with json_encode --- helpers/common.php | 32 ++++++++++++++++++++++++++++++ system/core/Common.php | 45 +++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/helpers/common.php b/helpers/common.php index 72c9772..576ecaf 100644 --- a/helpers/common.php +++ b/helpers/common.php @@ -37,3 +37,35 @@ function bear_str_length($str) return strlen($str); } } +if (!function_exists('__get_error_message__')) { + /** + * Function __get_error_message__ + * + * @param \Exception $e + * + * @return string + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 29/03/2023 53:17 + */ + function __get_error_message__($e) + { + return "Error Code: " . $e->getCode() . " - File: " . $e->getFile() . " - Line: " . $e->getLine() . " - Message: " . $e->getMessage(); + } +} +if (!function_exists('__get_error_trace__')) { + /** + * Function __get_error_trace__ + * + * @param \Exception $e + * + * @return string + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 29/03/2023 53:48 + */ + function __get_error_trace__($e) + { + return "Error Trace: " . $e->getTraceAsString(); + } +} diff --git a/system/core/Common.php b/system/core/Common.php index 53bb8b7..9f4df8b 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -57,7 +57,7 @@ /** * Determines if the current version of PHP is equal to or greater than the supplied value * - * @param string + * @param string $version * @return bool TRUE if the current version is $version or higher */ function is_php($version) @@ -112,7 +112,7 @@ function is_linux() * on Unix servers if safe_mode is on. * * @link https://bugs.php.net/bug.php?id=54709 - * @param string + * @param string $file * @return bool */ function is_really_writable($file) @@ -160,9 +160,9 @@ function is_really_writable($file) * exist it is instantiated and set to a static variable. If it has * previously been instantiated the variable is returned. * - * @param string the class name being requested - * @param string the directory where the class should be found - * @param mixed an optional argument to pass to the class constructor + * @param string $class the class name being requested + * @param string $directory the directory where the class should be found + * @param mixed $param an optional argument to pass to the class constructor * @return object */ function &load_class($class, $directory = 'libraries', $param = NULL) @@ -233,7 +233,7 @@ function &load_class($class, $directory = 'libraries', $param = NULL) * Keeps track of which libraries have been loaded. This function is * called by the load_class() function above * - * @param string + * @param string $class * @return array */ function &is_loaded($class = '') @@ -259,10 +259,10 @@ function &is_loaded($class = '') * This function lets us grab the config file even if the Config class * hasn't been instantiated yet * - * @param array + * @param array $replace * @return array */ - function &get_config(Array $replace = array()) + function &get_config(array $replace = array()) { static $config; @@ -314,7 +314,7 @@ function &get_config(Array $replace = array()) /** * Returns the specified config item * - * @param string + * @param string $item * @return mixed */ function config_item($item) @@ -427,9 +427,9 @@ function is_cli() * This function will send the error page directly to the * browser and exit. * - * @param string - * @param int - * @param string + * @param string $message + * @param int|float $status_code + * @param string $heading * @return void */ function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') @@ -462,8 +462,8 @@ function show_error($message, $status_code = 500, $heading = 'An Error Was Encou * However, instead of the standard error template it displays * 404 errors. * - * @param string - * @param bool + * @param string $page + * @param bool $log_error * @return void */ function show_404($page = '', $log_error = TRUE) @@ -484,8 +484,8 @@ function show_404($page = '', $log_error = TRUE) * We use this as a simple mechanism to access the logging * class and send messages to be logged. * - * @param string the error level: 'error', 'debug' or 'info' - * @param string the error message + * @param string $level the error level: 'error', 'debug' or 'info' + * @param string $message the error message * @return void */ function log_message($level, $message) @@ -498,6 +498,11 @@ function log_message($level, $message) $_log[0] =& load_class('Log', 'core'); } + // 2023-03-29: In log_message - If message not string, force string with json_encode + if (!is_string($message)) { + $message = json_encode($message); + } + $_log[0]->write_log($level, $message); } } @@ -509,8 +514,8 @@ function log_message($level, $message) /** * Set HTTP Status Header * - * @param int the status code - * @param string + * @param int $code the status code + * @param string $text * @return void */ function set_status_header($code = 200, $text = '') @@ -804,8 +809,8 @@ function html_escape($var, $double_encode = TRUE) * Helper function used to convert a string, array, or object * of attributes to a string. * - * @param mixed string, array, object - * @param bool + * @param mixed $attributes string, array, object + * @param bool $js * @return string */ function _stringify_attributes($attributes, $js = FALSE) From 5305b27c98dd24ed260fd7ffd2c2b7104b85e642 Mon Sep 17 00:00:00 2001 From: nguyenanhung Date: Wed, 29 Mar 2023 11:19:05 +0700 Subject: [PATCH 2/2] Update Project at Wed Mar 29 11:19:05 +07 2023 --- helpers/common.php | 4 ++-- hungng/HungNG_CI_Base_Controllers.php | 15 +++++++-------- hungng/HungNG_CI_Base_Module.php | 15 +++++++-------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/helpers/common.php b/helpers/common.php index 576ecaf..63548e2 100644 --- a/helpers/common.php +++ b/helpers/common.php @@ -41,7 +41,7 @@ function bear_str_length($str) /** * Function __get_error_message__ * - * @param \Exception $e + * @param \Exception|\Throwable $e * * @return string * @author : 713uk13m @@ -57,7 +57,7 @@ function __get_error_message__($e) /** * Function __get_error_trace__ * - * @param \Exception $e + * @param \Exception|\Throwable $e * * @return string * @author : 713uk13m diff --git a/hungng/HungNG_CI_Base_Controllers.php b/hungng/HungNG_CI_Base_Controllers.php index c36ce8a..eea1119 100644 --- a/hungng/HungNG_CI_Base_Controllers.php +++ b/hungng/HungNG_CI_Base_Controllers.php @@ -205,9 +205,8 @@ protected function jsonResponsePretty($response = array(), $status = 200) */ protected function errorExceptionResponse($exception) { - $errorMessage = "Error Code: " . $exception->getCode() . " - Error File: " . $exception->getFile() . " - Error Line: " . $exception->getLine() . " - Error Message: " . $exception->getMessage(); - log_message('error', $errorMessage); - log_message('error', $exception->getTraceAsString()); + log_message('error', __get_error_message__($exception)); + log_message('error', __get_error_trace__($exception)); $response = array(); $response['code'] = StatusCodes::HTTP_BAD_REQUEST; @@ -366,11 +365,11 @@ protected function log($name = '', $message = '', $context = array(), $inputLeve log_message($inputLevel, $name . " | Error Message: " . $message . ' | Context: ' . json_encode($context)); } } catch (InvalidArgumentException $exception) { - log_message('error', $exception->getMessage()); - log_message('error', $exception->getTraceAsString()); + log_message('error', __get_error_message__($exception)); + log_message('error', __get_error_trace__($exception)); } catch (Exception $exception) { - log_message('error', $exception->getMessage()); - log_message('error', $exception->getTraceAsString()); + log_message('error', __get_error_message__($exception)); + log_message('error', __get_error_trace__($exception)); } } @@ -422,7 +421,7 @@ protected function default_base_flush_logs() $this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output($output . PHP_EOL)->_display(); exit; } catch (Exception $e) { - $message = 'Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage(); + $message = __get_error_message__($e); log_message('error', $message); echo $message . PHP_EOL; exit; diff --git a/hungng/HungNG_CI_Base_Module.php b/hungng/HungNG_CI_Base_Module.php index 93ad730..d02878a 100644 --- a/hungng/HungNG_CI_Base_Module.php +++ b/hungng/HungNG_CI_Base_Module.php @@ -205,9 +205,8 @@ protected function jsonResponsePretty($response = array(), $status = 200) */ protected function errorExceptionResponse($exception) { - $errorMessage = "Error Code: " . $exception->getCode() . " - Error File: " . $exception->getFile() . " - Error Line: " . $exception->getLine() . " - Error Message: " . $exception->getMessage(); - log_message('error', $errorMessage); - log_message('error', $exception->getTraceAsString()); + log_message('error', __get_error_message__($exception)); + log_message('error', __get_error_trace__($exception)); $response = array(); $response['code'] = StatusCodes::HTTP_BAD_REQUEST; @@ -366,11 +365,11 @@ protected function log($name = '', $message = '', $context = array(), $inputLeve log_message($inputLevel, $name . " | Error Message: " . $message . ' | Context: ' . json_encode($context)); } } catch (InvalidArgumentException $exception) { - log_message('error', $exception->getMessage()); - log_message('error', $exception->getTraceAsString()); + log_message('error', __get_error_message__($exception)); + log_message('error', __get_error_trace__($exception)); } catch (Exception $exception) { - log_message('error', $exception->getMessage()); - log_message('error', $exception->getTraceAsString()); + log_message('error', __get_error_message__($exception)); + log_message('error', __get_error_trace__($exception)); } } @@ -422,7 +421,7 @@ protected function default_base_flush_logs() $this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output($output . PHP_EOL)->_display(); exit; } catch (Exception $e) { - $message = 'Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage(); + $message = __get_error_message__($e); log_message('error', $message); echo $message . PHP_EOL; exit;