CRM-11743 - Civi::log() - Add short hand for logging
[civicrm-core.git] / CRM / Core / Error / Log.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2015 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Class CRM_Core_Error_Log
31 *
32 * A PSR-3 wrapper for CRM_Core_Error.
33 */
34 class CRM_Core_Error_Log extends \Psr\Log\AbstractLogger {
35
36 public function __construct() {
37 $this->map = array(
38 \Psr\Log\LogLevel::DEBUG => PEAR_LOG_DEBUG,
39 \Psr\Log\LogLevel::INFO => PEAR_LOG_INFO,
40 \Psr\Log\LogLevel::NOTICE => PEAR_LOG_NOTICE,
41 \Psr\Log\LogLevel::WARNING => PEAR_LOG_WARNING,
42 \Psr\Log\LogLevel::ERROR => PEAR_LOG_ERR,
43 \Psr\Log\LogLevel::CRITICAL => PEAR_LOG_CRIT,
44 \Psr\Log\LogLevel::ALERT => PEAR_LOG_ALERT,
45 \Psr\Log\LogLevel::EMERGENCY => PEAR_LOG_EMERG,
46 );
47 }
48
49 /**
50 * Logs with an arbitrary level.
51 *
52 * @param mixed $level
53 * @param string $message
54 * @param array $context
55 */
56 public function log($level, $message, array $context = array()) {
57 // FIXME: This flattens a $context a bit prematurely. When integrating
58 // with external/CMS logs, we should pass through $context.
59 if (!empty($context)) {
60 if (isset($context['exception'])) {
61 $context['exception'] = CRM_Core_Error::formatTextException($context['exception']);
62 }
63 $message .= "\n" . print_r($context, 1);
64 }
65 CRM_Core_Error::debug_log_message($message, FALSE, '', $this->map[$level]);
66 }
67
68 }