Commit | Line | Data |
---|---|---|
6e5ad5ee TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
6e5ad5ee | 6 | | | |
bc77d7c0 TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
6e5ad5ee TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * Class CRM_Core_Error_Log | |
15 | * | |
16 | * A PSR-3 wrapper for CRM_Core_Error. | |
17 | */ | |
18 | class CRM_Core_Error_Log extends \Psr\Log\AbstractLogger { | |
19 | ||
2aafb0fc CW |
20 | /** |
21 | * @var array | |
22 | */ | |
23 | public $map; | |
24 | ||
8246bca4 | 25 | /** |
26 | * CRM_Core_Error_Log constructor. | |
27 | */ | |
6e5ad5ee | 28 | public function __construct() { |
82073f7b | 29 | $this->map = self::getMap(); |
30 | } | |
31 | ||
32 | /** | |
33 | * @return array | |
34 | */ | |
35 | public static function getMap():array { | |
36 | return [ | |
6e5ad5ee TO |
37 | \Psr\Log\LogLevel::DEBUG => PEAR_LOG_DEBUG, |
38 | \Psr\Log\LogLevel::INFO => PEAR_LOG_INFO, | |
39 | \Psr\Log\LogLevel::NOTICE => PEAR_LOG_NOTICE, | |
40 | \Psr\Log\LogLevel::WARNING => PEAR_LOG_WARNING, | |
41 | \Psr\Log\LogLevel::ERROR => PEAR_LOG_ERR, | |
42 | \Psr\Log\LogLevel::CRITICAL => PEAR_LOG_CRIT, | |
43 | \Psr\Log\LogLevel::ALERT => PEAR_LOG_ALERT, | |
44 | \Psr\Log\LogLevel::EMERGENCY => PEAR_LOG_EMERG, | |
be2fb01f | 45 | ]; |
6e5ad5ee TO |
46 | } |
47 | ||
48 | /** | |
49 | * Logs with an arbitrary level. | |
50 | * | |
51 | * @param mixed $level | |
52 | * @param string $message | |
53 | * @param array $context | |
54 | */ | |
be2fb01f | 55 | public function log($level, $message, array $context = []) { |
6e5ad5ee TO |
56 | // FIXME: This flattens a $context a bit prematurely. When integrating |
57 | // with external/CMS logs, we should pass through $context. | |
58 | if (!empty($context)) { | |
59 | if (isset($context['exception'])) { | |
60 | $context['exception'] = CRM_Core_Error::formatTextException($context['exception']); | |
61 | } | |
62 | $message .= "\n" . print_r($context, 1); | |
63 | } | |
9726c118 | 64 | CRM_Core_Error::debug_log_message($message, FALSE, '', $this->map[$level]); |
6e5ad5ee TO |
65 | } |
66 | ||
67 | } |