-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error log improvements: Provide severity level and use Civi::log() #14222
Conversation
…ethods could use Civi::log() internally; prefix param support for Civi::log().
(Standard links)
|
@totten I think this is the right direction - could use your thoughts here? |
@@ -39,6 +39,8 @@ | |||
|
|||
require_once 'Log.php'; | |||
|
|||
use Psr\Log\LogLevel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -232,7 +234,7 @@ public static function handle($pearError) { | |||
$errorDetails = CRM_Core_Error::debug('', $error, FALSE); | |||
$template->assign_by_ref('errorDetails', $errorDetails); | |||
|
|||
CRM_Core_Error::debug_var('Fatal Error Details', $error); | |||
CRM_Core_Error::debug_var('Fatal Error Details', $error, TRUE, TRUE, '', LogLevel::ERROR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -339,7 +341,7 @@ public static function fatal($message = NULL, $code = NULL, $email = NULL) { | |||
|
|||
if (self::$modeException) { | |||
// CRM-11043 | |||
CRM_Core_Error::debug_var('Fatal Error Details', $vars); | |||
CRM_Core_Error::debug_var('Fatal Error Details', $vars, TRUE, TRUE, '', LogLevel::ERROR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -381,7 +383,7 @@ function_exists($config->fatalErrorHandler) | |||
self::backtrace(); | |||
} | |||
|
|||
CRM_Core_Error::debug_var('Fatal Error Details', $vars); | |||
CRM_Core_Error::debug_var('Fatal Error Details', $vars, TRUE, TRUE, '', LogLevel::ERROR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -421,7 +423,7 @@ public static function handleUnhandledException($exception) { | |||
} | |||
catch (Exception $other) { | |||
// if the exception-handler generates an exception, then that sucks! oh, well. carry on. | |||
CRM_Core_Error::debug_var('handleUnhandledException_nestedException', self::formatTextException($other)); | |||
CRM_Core_Error::debug_var('handleUnhandledException_nestedException', self::formatTextException($other), TRUE, TRUE, '', LogLevel::ERROR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -459,7 +461,7 @@ function_exists($config->fatalErrorHandler) | |||
// Case C: Default error handler | |||
|
|||
// log to file | |||
CRM_Core_Error::debug_var('Fatal Error Details', $vars, FALSE); | |||
CRM_Core_Error::debug_var('Fatal Error Details', $vars, FALSE, TRUE, '', LogLevel::ERROR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
* | ||
* @return string | ||
* The generated output | ||
* | ||
* @see CRM_Core_Error::debug() | ||
* @see CRM_Core_Error::debug_log_message() | ||
*/ | ||
public static function debug_var($variable_name, $variable, $print = TRUE, $log = TRUE, $prefix = '') { | ||
public static function debug_var($variable_name, $variable, $print = TRUE, $log = TRUE, $prefix = '', $level = LogLevel::INFO) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -574,7 +578,15 @@ public static function debug_var($variable_name, $variable, $print = TRUE, $log | |||
reset($variable); | |||
} | |||
} | |||
return self::debug_log_message($out, FALSE, $prefix); | |||
Civi::log()->log($level, $out, ['civi.prefix' => $prefix]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is the biggest change in here - however it seems that Civi::log winds up calling CRM_Core_Error_Log::log
which itself calls CRM_Core_Error::debug_log_message($message, FALSE, '', $this->map[$level]);
So it would be functionally equivalent except that the param $out is not passed through or echo'd. I think this function might need to echo $out itself to retain that behaviour
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = []) {
// FIXME: This flattens a $context a bit prematurely. When integrating
// with external/CMS logs, we should pass through $context.
if (!empty($context)) {
if (isset($context['exception'])) {
$context['exception'] = CRM_Core_Error::formatTextException($context['exception']);
}
$message .= "\n" . print_r($context, 1);
if (CRM_Utils_System::isDevelopment() && CRM_Utils_Array::value('civi.tag', $context) === 'deprecated') {
trigger_error($message, E_USER_DEPRECATED);
}
}
CRM_Core_Error::debug_log_message($message, FALSE, '', $this->map[$level]);
}
/** | ||
* Generates debug HTML output. | ||
*/ | ||
public static function debugOutput($message) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -600,7 +612,7 @@ public static function debug_log_message($message, $out = FALSE, $prefix = '', $ | |||
$file_log = self::createDebugLogger($prefix); | |||
$file_log->log("$message\n", $priority); | |||
|
|||
$str = '<p/><code>' . htmlspecialchars($message) . '</code>'; | |||
$str = self::debugOutput($message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -962,7 +974,7 @@ public static function exceptionHandler($pearError) { | |||
* $obj | |||
*/ | |||
public static function nullHandler($obj) { | |||
CRM_Core_Error::debug_log_message("Ignoring exception thrown by nullHandler: {$obj->code}, {$obj->message}"); | |||
Civi::log()->error("Ignoring exception thrown by nullHandler: {$obj->code}, {$obj->message}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok - in the absense of $out - this is the same
This looks good to me - but I have a concern with might be losing the output (not passing $out & not echoing to compensate) |
@mfb did you have a response on ^^ |
debug_var() echos the output itself, and then when calling debug_log_message() always sets $out (the second parameter) to FALSE so no echoing happens there; just logging. |
Looks like there is an answer to the question I raised - this is fairly low risk / easily fixable if we do lose a bit of 'noise' - merging |
Revert #14222 on RC branch
Overview
I'm not sure if we want to deprecate
CRM_Core_Error::debug_var()
and stop using it, or make some improvements to it.If the latter, then we could provide a
$level
argument so that we can assign a severity level to fatal errors and other usages ofCRM_Core_Error::debug_var()
, and in addition, variousCRM_Core_Error
methods could useCivi::log()
internally rather thanCRM_Core_Error::debug_log_message()
.Note: I would say that
CRM_Core_Error::debug_log_message()
should also useCivi::log()
, but this isn't doable as long asCRM_Core_Error_Log::log()
callsCRM_Core_Error::debug_log_message()
.. This could be worked on in a future PR if desired.Before
A call like
CRM_Core_Error::debug_var('Fatal Error Details', ...)
logs at the default severity level, and doesn't useCivi::log()
.After
CRM_Core_Error::debug_var()
has an optional$level
arg;CRM_Core_Error
methods useCivi::log()
internally; andCivi::log()
supports acivi.prefix
context param to specify the log file prefix.