diff --git a/CRM/Civirules/Action.php b/CRM/Civirules/Action.php index ebb687cc0c0ddb0bde0bb2b19c708c9399bb4fc3..96ec56db91100cabae87d418574dffe39ea56a70 100644 --- a/CRM/Civirules/Action.php +++ b/CRM/Civirules/Action.php @@ -97,6 +97,33 @@ abstract class CRM_Civirules_Action { return ''; } + /** + * Logs a message to the logger + * + * @param $message + * @param \CRM_Civirules_EventData_EventData|NULL $eventData + * @param string $level Should be one of \Psr\Log\LogLevel + */ + protected function logAction($message, CRM_Civirules_EventData_EventData $eventData=null, $level=\Psr\Log\LogLevel::INFO) { + $context = array(); + $context['message'] = $message; + $context['rule_id'] = $this->ruleAction['rule_id']; + $rule = new CRM_Civirules_BAO_Rule(); + $rule->id = $this->ruleAction['rule_id']; + $context['rule_title'] = ''; + if ($rule->find(true)) { + $context['rule_title'] = $rule->label; + } + $context['rule_action_id'] = $this->ruleAction['id']; + $context['action_label'] = CRM_Civirules_BAO_Action::getActionLabelWithId($this->ruleAction['action_id']); + $context['action_parameters'] = $this->userFriendlyConditionParams(); + $context['contact_id'] = $eventData ? $eventData->getContactId() : - 1; + $msg = "{action_label} ({rule_action_id}\r\n\r\n{message}\r\n\r\nRule: '{rule_title}' with id {rule_id}"; + if ($context['contact_id'] > 0) { + $msg .= "\r\nFor contact: {contact_id}"; + } + CRM_Civirules_Utils_LoggerFactory::log($msg, $context, $level); + } } \ No newline at end of file diff --git a/CRM/Civirules/Condition.php b/CRM/Civirules/Condition.php index 431f07c0859ddface9888980838bf04108a16a4f..962384c5dbfd929923691f028d315eb87459ccf0 100644 --- a/CRM/Civirules/Condition.php +++ b/CRM/Civirules/Condition.php @@ -66,6 +66,34 @@ abstract class CRM_Civirules_Condition { return array(); } + /** + * Logs a message to the logger + * + * @param $message + * @param \CRM_Civirules_EventData_EventData|NULL $eventData + * @param string $level Should be one of \Psr\Log\LogLevel + */ + protected function logCondition($message, CRM_Civirules_EventData_EventData $eventData=null, $level=\Psr\Log\LogLevel::INFO) { + $context = array(); + $context['message'] = $message; + $context['rule_id'] = $this->ruleCondition['rule_id']; + $rule = new CRM_Civirules_BAO_Rule(); + $rule->id = $this->ruleCondition['rule_id']; + $context['rule_title'] = ''; + if ($rule->find(true)) { + $context['rule_title'] = $rule->label; + } + $context['rule_condition_id'] = $this->ruleCondition['id']; + $context['condition_label'] = CRM_Civirules_BAO_Condition::getConditionLabelWithId($this->ruleCondition['condition_id']); + $context['condition_parameters'] = $this->userFriendlyConditionParams(); + $context['contact_id'] = $eventData ? $eventData->getContactId() : - 1; + $msg = "{condition_label} ({rule_condition_id}\r\n\r\n{message}\r\n\r\nRule: '{rule_title}' with id {rule_id}"; + if ($context['contact_id'] > 0) { + $msg .= "\r\nFor contact: {contact_id}"; + } + CRM_Civirules_Utils_LoggerFactory::log($msg, $context, $level); + } + } \ No newline at end of file diff --git a/CRM/Civirules/Engine.php b/CRM/Civirules/Engine.php index d4bfd29388c89e43b1a062ec2300ce7ae0767ba8..cf539a5e4ac030b1b5165e2e0d7585508e523174 100644 --- a/CRM/Civirules/Engine.php +++ b/CRM/Civirules/Engine.php @@ -32,7 +32,12 @@ class CRM_Civirules_Engine { return true; } } catch (Exception $e) { - CRM_Civirules_Utils_LoggerFactory::logError("Failed to execute rule", $e->getMessage(), $eventData); + $message = "Error on {file} (Line {line})\r\n\r\n{exception_message}"; + $context = array(); + $context['line'] = $e->getLine(); + $context['file'] = $e->getFile(); + $context['exception_message'] = $e->getMessage(); + CRM_Civirules_Utils_LoggerFactory::logError("Failed to execute rule", $message, $eventData, $context); } return false; } diff --git a/CRM/Civirules/Utils/LoggerFactory.php b/CRM/Civirules/Utils/LoggerFactory.php index 4cfce4c579ebc4152237bd45391f6e14bcbe7fba..9dfc0a3a9bee88716a98d8444d1fc976fef3cd60 100644 --- a/CRM/Civirules/Utils/LoggerFactory.php +++ b/CRM/Civirules/Utils/LoggerFactory.php @@ -18,6 +18,14 @@ class CRM_Civirules_Utils_LoggerFactory { return self::$logger; } + public static function log($message, $context=array(), $level=\Psr\Log\LogLevel::INFO) { + $logger = CRM_Civirules_Utils_LoggerFactory::getLogger(); + if (empty($logger)) { + return; + } + $logger->log($level, $message, $context); + } + public static function logError($reason, $original_error, CRM_Civirules_EventData_EventData $eventData, $context=array()) { $logger = CRM_Civirules_Utils_LoggerFactory::getLogger(); if (empty($logger)) { diff --git a/CRM/CivirulesConditions/FieldValueComparison.php b/CRM/CivirulesConditions/FieldValueComparison.php index ad9bb6c8c6ebed6c4940d11c3670c0014a72f10e..4afe8470099b79356c3f473f71ba4ed8080723b6 100644 --- a/CRM/CivirulesConditions/FieldValueComparison.php +++ b/CRM/CivirulesConditions/FieldValueComparison.php @@ -14,7 +14,7 @@ class CRM_CivirulesConditions_FieldValueComparison extends CRM_CivirulesConditio protected function getFieldValue(CRM_Civirules_EventData_EventData $eventData) { $entity = $this->conditionParams['entity']; $field = $this->conditionParams['field']; - + $data = $eventData->getEntityData($entity); if (isset($data[$field])) { return $this->normalizeValue($data[$field]); diff --git a/civirules.php b/civirules.php index 0687dafedfc7233a233884b4c70453f4ea4b9d8c..b7f3919998cc931a99d95356401af1f955a8d02f 100755 --- a/civirules.php +++ b/civirules.php @@ -1,6 +1,12 @@ <?php require_once 'civirules.civix.php'; +if (!interface_exists("\\Psr\\Log\\LoggerInterface")) { + require_once('psr/log/LoggerInterface.php'); +} +if (!class_exists("\\Psr\\Log\\LogLevel")) { + require_once('psr/log/LogLevel.php'); +} /** * Implementation of hook_civicrm_config diff --git a/psr/log/LogLevel.php b/psr/log/LogLevel.php new file mode 100644 index 0000000000000000000000000000000000000000..e6ecfe8b5d55fd0f78880f6bd158aa0e6a4dad9c --- /dev/null +++ b/psr/log/LogLevel.php @@ -0,0 +1,18 @@ +<?php + +namespace Psr\Log; + +/** + * Describes log levels + */ +class LogLevel +{ + const EMERGENCY = 'emergency'; + const ALERT = 'alert'; + const CRITICAL = 'critical'; + const ERROR = 'error'; + const WARNING = 'warning'; + const NOTICE = 'notice'; + const INFO = 'info'; + const DEBUG = 'debug'; +} \ No newline at end of file diff --git a/psr/log/LoggerInterface.php b/psr/log/LoggerInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..db793c6157af1728204f72885fccbd5cb82c7f1e --- /dev/null +++ b/psr/log/LoggerInterface.php @@ -0,0 +1,114 @@ +<?php + +namespace Psr\Log; + +/** + * Describes a logger instance + * + * The message MUST be a string or object implementing __toString(). + * + * The message MAY contain placeholders in the form: {foo} where foo + * will be replaced by the context data in key "foo". + * + * The context array can contain arbitrary data, the only assumption that + * can be made by implementors is that if an Exception instance is given + * to produce a stack trace, it MUST be in a key named "exception". + * + * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md + * for the full interface specification. + */ +interface LoggerInterface +{ + /** + * System is unusable. + * + * @param string $message + * @param array $context + * @return null + */ + public function emergency($message, array $context = array()); + + /** + * Action must be taken immediately. + * + * Example: Entire website down, database unavailable, etc. This should + * trigger the SMS alerts and wake you up. + * + * @param string $message + * @param array $context + * @return null + */ + public function alert($message, array $context = array()); + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param string $message + * @param array $context + * @return null + */ + public function critical($message, array $context = array()); + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * @return null + */ + public function error($message, array $context = array()); + + /** + * Exceptional occurrences that are not errors. + * + * Example: Use of deprecated APIs, poor use of an API, undesirable things + * that are not necessarily wrong. + * + * @param string $message + * @param array $context + * @return null + */ + public function warning($message, array $context = array()); + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * @return null + */ + public function notice($message, array $context = array()); + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param string $message + * @param array $context + * @return null + */ + public function info($message, array $context = array()); + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * @return null + */ + public function debug($message, array $context = array()); + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()); +} \ No newline at end of file