CRM-11743 - Civi::log() - Add short hand for logging
authorTim Otten <totten@civicrm.org>
Fri, 21 Aug 2015 06:05:18 +0000 (23:05 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 17 Sep 2015 22:45:00 +0000 (15:45 -0700)
CRM/Core/Error.php
CRM/Core/Error/Log.php [new file with mode: 0644]
Civi.php
Civi/Core/Container.php

index 5517fd83334e4fc14f0ad8d19d77a62a4cfe8e79..0713da07d9c84c3291b1ef717c85812365737f10 100644 (file)
@@ -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 = '<p/><code>' . htmlspecialchars($message) . '</code>';
     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 (file)
index 0000000..8ad77bb
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Core_Error_Log
+ *
+ * A PSR-3 wrapper for CRM_Core_Error.
+ */
+class CRM_Core_Error_Log extends \Psr\Log\AbstractLogger {
+
+  public function __construct() {
+    $this->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]);
+  }
+
+}
index 20d78b670a25e772e5d4ebf10d9fcef720fb9347..c88efd0268cad606a856804373f91078a2943a41 100644 (file)
--- 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.
    *
index 2be91acca1ce14eaaa6e1fa26a685dca0f281b56..dd9ca9a0d32bf1510e3ce8ef1ba1cc7033469386 100644 (file)
@@ -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(