SentryIntegration: send logged errors (fatal only by default)

This commit is contained in:
Benjamin Renard 2024-12-13 15:21:25 +01:00
parent 7693ea44ee
commit e9fb4cf504
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 65 additions and 13 deletions

View file

@ -168,12 +168,6 @@ class Log {
App::get('log.level', 'WARNING', 'string'):
'WARNING'
);
if (self :: $levels[$level] < self :: $levels[self :: $level]) return true;
if(self :: $filepath && is_null(self :: $file_fd)) {
self :: $file_fd = fopen(self :: $filepath, 'a');
if (self :: $file_fd === false)
self :: error('Fail to open log file (%s)', self :: $filepath);
}
// Extra arguments passed, format message using sprintf
if ($extra_args) {
@ -183,6 +177,15 @@ class Log {
);
}
SentryIntegration :: on_logged_message($level, $message);
if (!self :: check_message_level($level)) return true;
if(self :: $filepath && is_null(self :: $file_fd)) {
self :: $file_fd = fopen(self :: $filepath, 'a');
if (self :: $file_fd === false)
self :: error('Fail to open log file (%s)', self :: $filepath);
}
if (php_sapi_name() == "cli") {
$msg = implode(' - ', array(
date('Y/m/d H:i:s'),
@ -361,6 +364,25 @@ class Log {
}
}
/**
* Check specified log level exists
* @param mixed $level
* @return bool
*/
public static function check_level($level) {
return array_key_exists($level, self :: $levels);
}
/**
* Check specified level is included by configured level
* @param string $level
* @param string $configured_level
* @return bool
*/
public static function check_message_level($level, $configured_level=null) {
return self :: $levels[$level] >= self :: $levels[($configured_level ?? self :: $level)];
}
/*
*******************************************************************************
* Handle exception logging

View file

@ -20,6 +20,12 @@ class SentryIntegration {
*/
protected static $php_error_types;
/**
* Log level
* @var string|null
*/
protected static $log_level = null;
/**
* Initialization
* @return void
@ -31,14 +37,15 @@ class SentryIntegration {
// Set config default values
App :: set_default(
'sentry',
array(
[
'dsn' => null,
'traces_sample_rate' => 0.2,
'php_error_types' => array(
'php_error_types' => [
'E_ERROR', 'E_PARSE', 'E_CORE_ERROR', 'E_COMPILE_ERROR', 'E_USER_ERROR',
'E_RECOVERABLE_ERROR', 'E_DEPRECATED',
),
)
],
'log_level' => 'FATAL',
]
);
self :: $dsn = App::get('sentry.dsn');
@ -47,6 +54,17 @@ class SentryIntegration {
return;
}
self :: $log_level = App::get('sentry.log_level', null, 'string');
if (!Log::check_level(self :: $log_level)) {
$bad_level = self :: $log_level;
self :: $log_level = App::get_default("sentry.log_level");
Log::warning(
"Invalid log level configured for Sentry (%s), use default (%s)",
$bad_level,
self :: $log_level
);
}
\Sentry\init([
'dsn' => self :: $dsn,
'traces_sample_rate' => App::get('sentry.traces_sample_rate', null, 'float'),
@ -59,7 +77,7 @@ class SentryIntegration {
]);
});
self :: $php_error_types = array();
self :: $php_error_types = [];
foreach(App::get('sentry.php_error_types', null, 'array') as $php_error_type) {
if (is_string($php_error_type) && defined($php_error_type))
$php_error_type = constant($php_error_type);
@ -68,7 +86,7 @@ class SentryIntegration {
self :: $php_error_types[] = $php_error_type;
}
set_error_handler(
array('EesyPHP\\SentryIntegration', 'on_php_error'),
[static :: class, 'on_php_error'],
E_ALL
);
}
@ -89,7 +107,7 @@ class SentryIntegration {
if ($extra_args) {
$message = call_user_func_array(
'sprintf',
array_merge(array($message), $extra_args)
array_merge([$message], $extra_args)
);
}
Log :: debug('Error logged in Sentry');
@ -118,6 +136,18 @@ class SentryIntegration {
);
return false;
}
/**
* Method to run on logged message (directly called by Log::log())
* Important: This method can't log anything to avoid log loop.
* @param string $level
* @param string $message
* @return void
*/
public static function on_logged_message($level, $message) {
if (self :: $dsn && Log::check_message_level($level, self :: $log_level))
\Sentry\captureMessage($message);
}
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab