From 6e5ad5eeebdd8f764b5f617d28a20c645a782932 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 20 Aug 2015 23:05:18 -0700 Subject: [PATCH] CRM-11743 - Civi::log() - Add short hand for logging --- CRM/Core/Error.php | 4 +-- CRM/Core/Error/Log.php | 68 +++++++++++++++++++++++++++++++++++++++++ Civi.php | 7 +++++ Civi/Core/Container.php | 2 ++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 CRM/Core/Error/Log.php diff --git a/CRM/Core/Error.php b/CRM/Core/Error.php index 5517fd8333..0713da07d9 100644 --- a/CRM/Core/Error.php +++ b/CRM/Core/Error.php @@ -571,11 +571,11 @@ class CRM_Core_Error extends PEAR_ErrorStack { * * */ - public static function debug_log_message($message, $out = FALSE, $comp = '') { + public static function debug_log_message($message, $out = FALSE, $comp = '', $priority = NULL) { $config = CRM_Core_Config::singleton(); $file_log = self::createDebugLogger($comp); - $file_log->log("$message\n"); + $file_log->log("$message\n", $priority); $str = '

' . htmlspecialchars($message) . ''; if ($out && CRM_Core_Permission::check('view debug output')) { diff --git a/CRM/Core/Error/Log.php b/CRM/Core/Error/Log.php new file mode 100644 index 0000000000..8ad77bb59d --- /dev/null +++ b/CRM/Core/Error/Log.php @@ -0,0 +1,68 @@ +map = array( + \Psr\Log\LogLevel::DEBUG => PEAR_LOG_DEBUG, + \Psr\Log\LogLevel::INFO => PEAR_LOG_INFO, + \Psr\Log\LogLevel::NOTICE => PEAR_LOG_NOTICE, + \Psr\Log\LogLevel::WARNING => PEAR_LOG_WARNING, + \Psr\Log\LogLevel::ERROR => PEAR_LOG_ERR, + \Psr\Log\LogLevel::CRITICAL => PEAR_LOG_CRIT, + \Psr\Log\LogLevel::ALERT => PEAR_LOG_ALERT, + \Psr\Log\LogLevel::EMERGENCY => PEAR_LOG_EMERG, + ); + } + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + */ + public function log($level, $message, array $context = array()) { + // 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); + } + CRM_Core_Error::debug_log_message($message, FALSE, '', $this->map[$level]); + } + +} diff --git a/Civi.php b/Civi.php index 20d78b670a..c88efd0268 100644 --- a/Civi.php +++ b/Civi.php @@ -54,6 +54,13 @@ class Civi { return Civi\Core\Container::singleton(); } + /** + * @return \Psr\Log\LoggerInterface + */ + public static function log() { + return Civi\Core\Container::singleton()->get('psr_log'); + } + /** * Fetch a service from the container. * diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 2be91acca1..dd9ca9a0d3 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -103,6 +103,8 @@ class Container { )) ->setFactoryClass('CRM_Cxn_BAO_Cxn')->setFactoryMethod('createRegistrationClient'); + $container->setDefinition('psr_log', new Definition('CRM_Core_Error_Log', array())); + $container->setDefinition('cache.settings', new Definition( 'CRM_Utils_Cache_SqlGroup', array( -- 2.25.1