From 72627ae712423b1f484885eb85e88c811b0e9668 Mon Sep 17 00:00:00 2001 From: Jaap Jansma <jaap@edeveloper.nl> Date: Tue, 13 Oct 2015 11:23:57 +0200 Subject: [PATCH] added logging to the Action and Condition classes and implemented a basic psr3 log interface and log level class --- CRM/Civirules/Action.php | 27 +++++ CRM/Civirules/Condition.php | 28 +++++ CRM/Civirules/Engine.php | 7 +- CRM/Civirules/Utils/LoggerFactory.php | 8 ++ .../FieldValueComparison.php | 2 +- civirules.php | 6 + psr/log/LogLevel.php | 18 +++ psr/log/LoggerInterface.php | 114 ++++++++++++++++++ 8 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 psr/log/LogLevel.php create mode 100644 psr/log/LoggerInterface.php diff --git a/CRM/Civirules/Action.php b/CRM/Civirules/Action.php index ebb687c..96ec56d 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 431f07c..962384c 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 d4bfd29..cf539a5 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 4cfce4c..9dfc0a3 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 ad9bb6c..4afe847 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 0687daf..b7f3919 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 0000000..e6ecfe8 --- /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 0000000..db793c6 --- /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 -- GitLab