From: Coleman Watts Date: Tue, 5 May 2020 19:19:08 +0000 (-0400) Subject: API - Display all logged warnings, errors, etc. in debug output and APIv4 explorer. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2aafb0fc5b76f43ac29b3a353ac822956eb8ff9b;p=civicrm-core.git API - Display all logged warnings, errors, etc. in debug output and APIv4 explorer. --- diff --git a/CRM/Core/Error.php b/CRM/Core/Error.php index a36bb5ce7d..206570e51b 100644 --- a/CRM/Core/Error.php +++ b/CRM/Core/Error.php @@ -608,7 +608,7 @@ class CRM_Core_Error extends PEAR_ErrorStack { * * @param string $prefix * - * @return Log + * @return Log_file */ public static function createDebugLogger($prefix = '') { self::generateLogFileName($prefix); diff --git a/CRM/Core/Error/Log.php b/CRM/Core/Error/Log.php index 61458ba900..6b7fd20c8e 100644 --- a/CRM/Core/Error/Log.php +++ b/CRM/Core/Error/Log.php @@ -17,6 +17,11 @@ */ class CRM_Core_Error_Log extends \Psr\Log\AbstractLogger { + /** + * @var array + */ + public $map; + /** * CRM_Core_Error_Log constructor. */ diff --git a/Civi.php b/Civi.php index 12b2119b45..ebd09e8afe 100644 --- a/Civi.php +++ b/Civi.php @@ -75,7 +75,7 @@ class Civi { } /** - * @return \Psr\Log\LoggerInterface + * @return \CRM_Core_Error_Log */ public static function log() { return Civi\Core\Container::singleton()->get('psr_log'); diff --git a/Civi/API/LogObserver.php b/Civi/API/LogObserver.php new file mode 100644 index 0000000000..b92eaa67d4 --- /dev/null +++ b/Civi/API/LogObserver.php @@ -0,0 +1,44 @@ +map; + $event['level'] = array_search($event['priority'], $levels); + // Extract [civi.tag] from message string + // As noted in \CRM_Core_Error_Log::log() the $context array gets prematurely converted to string with print_r() so we have to un-flatten it here + if (preg_match('/^(.*)\s*Array\s*\(\s*\[civi\.(\w+)] => (\w+)\s*\)/', $event['message'], $message)) { + $event['message'] = $message[1]; + $event[$message[2]] = $message[3]; + } + self::$messages[] = $event; + } + + /** + * @return array + */ + public function getMessages() { + return self::$messages; + } + +} diff --git a/Civi/API/Subscriber/XDebugSubscriber.php b/Civi/API/Subscriber/DebugSubscriber.php similarity index 61% rename from Civi/API/Subscriber/XDebugSubscriber.php rename to Civi/API/Subscriber/DebugSubscriber.php index aec0c1259e..88f529f972 100644 --- a/Civi/API/Subscriber/XDebugSubscriber.php +++ b/Civi/API/Subscriber/DebugSubscriber.php @@ -18,17 +18,34 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; * Class XDebugSubscriber * @package Civi\API\Subscriber */ -class XDebugSubscriber implements EventSubscriberInterface { +class DebugSubscriber implements EventSubscriberInterface { + + /** + * @var \Civi\API\LogObserver + */ + private $debugLog; /** * @return array */ public static function getSubscribedEvents() { return [ - Events::RESPOND => ['onApiRespond', Events::W_LATE], + Events::PREPARE => ['onApiPrepare', 999], + Events::RESPOND => ['onApiRespond', -999], ]; } + public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { + $apiRequest = $event->getApiRequest(); + if (!isset($this->debugLog) + && !empty($apiRequest['params']['debug']) + && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output')) + ) { + $this->debugLog = new \Civi\API\LogObserver(); + \CRM_Core_Error::createDebugLogger()->attach($this->debugLog); + } + } + /** * @param \Civi\API\Event\RespondEvent $event * API response event. @@ -36,9 +53,7 @@ class XDebugSubscriber implements EventSubscriberInterface { public function onApiRespond(\Civi\API\Event\RespondEvent $event) { $apiRequest = $event->getApiRequest(); $result = $event->getResponse(); - if ( - function_exists('xdebug_time_index') - && !empty($apiRequest['params']['debug']) + if (!empty($apiRequest['params']['debug']) && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output')) ) { if (is_a($result, '\Civi\Api4\Generic\Result')) { @@ -53,9 +68,14 @@ class XDebugSubscriber implements EventSubscriberInterface { else { return; } - $debug['peakMemory'] = xdebug_peak_memory_usage(); - $debug['memory'] = xdebug_memory_usage(); - $debug['timeIndex'] = xdebug_time_index(); + if (isset($this->debugLog) && $this->debugLog->getMessages()) { + $debug['log'] = $this->debugLog->getMessages(); + } + if (function_exists('xdebug_time_index')) { + $debug['peakMemory'] = xdebug_peak_memory_usage(); + $debug['memory'] = xdebug_memory_usage(); + $debug['timeIndex'] = xdebug_time_index(); + } $event->setResponse($result); } } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index f0bc4a2bf0..7493eff178 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -410,7 +410,7 @@ class Container { \CRM_Utils_API_ReloadOption::singleton(), \CRM_Utils_API_MatchOption::singleton(), ])); - $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); + $dispatcher->addSubscriber(new \Civi\API\Subscriber\DebugSubscriber()); $kernel = new \Civi\API\Kernel($dispatcher); $reflectionProvider = new \Civi\API\Provider\ReflectionProvider($kernel); diff --git a/ang/api4Explorer/Explorer.html b/ang/api4Explorer/Explorer.html index 56d6e72796..abff9b0162 100644 --- a/ang/api4Explorer/Explorer.html +++ b/ang/api4Explorer/Explorer.html @@ -169,7 +169,7 @@
-
+