From e2bef98506001ad5960cb1a4b479f30b6ddfd530 Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Thu, 1 May 2014 16:03:07 +0000 Subject: [PATCH] CRM-14494 add system logger files CRM-14449 Add system Log table to schema Conflicts: CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl CRM-14449 add system log CRM-14449 remove debug --- .gitignore | 1 + CRM/Core/Payment.php | 18 +++++ .../Incremental/sql/4.5.alpha1.mysql.tpl | 19 +++++ CRM/Utils/System/SystemLogger.php | 52 ++++++++++++++ CRM/Utils/SystemLogger.php | 50 ++++++++++++++ api/v3/System.php | 59 +++++++++++++++- api/v3/SystemLog.php | 43 ++++++++++++ tests/phpunit/api/v3/SystemTest.php | 10 +++ xml/schema/Core/SystemLog.xml | 69 +++++++++++++++++++ xml/schema/Core/files.xml | 1 + 10 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 CRM/Utils/System/SystemLogger.php create mode 100644 CRM/Utils/SystemLogger.php create mode 100644 api/v3/SystemLog.php create mode 100644 xml/schema/Core/SystemLog.xml diff --git a/.gitignore b/.gitignore index 2035d739f2..c1c0357721 100644 --- a/.gitignore +++ b/.gitignore @@ -80,6 +80,7 @@ CRM/Core/DAO/PriceSetEntity.php CRM/Core/DAO/PrintLabel.php CRM/Core/DAO/Setting.php CRM/Core/DAO/StateProvince.php +CRM/Core/DAO/SystemLog.php CRM/Core/DAO/Tag.php CRM/Core/DAO/Timezone.php CRM/Core/DAO/UFField.php diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 58700a3c97..a23447718e 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -124,6 +124,23 @@ abstract class CRM_Core_Payment { return self::$_singleton[$cacheKey]; } + /** + * @param $params + * + * @return mixed + */ + public static function logPaymentNotification($params) { + $message = ''; + if (!empty($params['processor_name'])) { + $message = 'processor_name=' . $params['processor_name']; + } + if (!empty($params['processor_id'])) { + $message .= 'processor_id=' . $params['processor_id']; + } + $log = CRM_Utils_SystemLogger(); + $log->log('alert', $message, $_REQUEST); + } + /** * Setter for the payment form that wants to use the processor * @@ -215,6 +232,7 @@ abstract class CRM_Core_Payment { if (!isset($params['processor_id']) && !isset($params['processor_name'])) { CRM_Core_Error::fatal("Either 'processor_id' or 'processor_name' param is required for payment callback"); } + self::logPaymentNotification($params); // Query db for processor .. $mode = @$params['mode']; diff --git a/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl index 7d8a5b3cd8..1cbf053425 100644 --- a/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl @@ -288,5 +288,24 @@ ALTER TABLE `civicrm_option_group` UPDATE `civicrm_option_group` SET is_locked = 1 WHERE name IN ('contribution_status','activity_contacts','advanced_search_options','auto_renew_options','contact_autocomplete_options','batch_status','batch_type','batch_mode','contact_edit_options','contact_reference_options','contact_smart_group_display','contact_view_options','financial_item_status','mapping_type','pcp_status','user_dashboard_options','tag_used_for'); + -- CRM-14522 ALTER TABLE `civicrm_msg_template` DROP FOREIGN KEY `FK_civicrm_msg_template_pdf_format_id`; + +-- CRM-14449 +CREATE TABLE `civicrm_system_log` ( +`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key: ID.', +`message` VARCHAR(128) NOT NULL COMMENT 'Standardized message', +`context` LONGTEXT NULL COMMENT 'JSON encoded data', +`level` VARCHAR(9) NOT NULL DEFAULT 'info' COMMENT 'error level per PSR3', +`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Timestamp of when event occurred.', +`contact_id` INT(11) NULL DEFAULT NULL COMMENT 'Optional Contact ID that created the log. Not an FK as we keep this regardless', + hostname VARCHAR(128) NOT NULL COMMENT 'Optional Name of logging host', +PRIMARY KEY (`id`), +INDEX `message` (`message`), +INDEX `contact_id` (`contact_id`), +INDEX `level` (`level`) +) +COMMENT='Table that contains logs of all system events.' +COLLATE='utf8_general_ci'; + diff --git a/CRM/Utils/System/SystemLogger.php b/CRM/Utils/System/SystemLogger.php new file mode 100644 index 0000000000..602cf62d33 --- /dev/null +++ b/CRM/Utils/System/SystemLogger.php @@ -0,0 +1,52 @@ +{$separateField} = $context[$separateField]; + unset($context[$separateField]); + } + } + print_r($rec); + $rec->level = $level; + $rec->message = $message; + $rec->context = json_encode($context); + $rec->save(); + } +} diff --git a/CRM/Utils/SystemLogger.php b/CRM/Utils/SystemLogger.php new file mode 100644 index 0000000000..df2a25d752 --- /dev/null +++ b/CRM/Utils/SystemLogger.php @@ -0,0 +1,50 @@ +{$separateField} = $context[$separateField]; + unset($context[$separateField]); + } + } + $rec->level = $level; + $rec->message = $message; + $rec->context = json_encode($context); + $rec->save(); + } +} diff --git a/api/v3/System.php b/api/v3/System.php index 1c7875d588..3203f23547 100644 --- a/api/v3/System.php +++ b/api/v3/System.php @@ -98,4 +98,61 @@ function civicrm_api3_system_check($params) { // Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL) return civicrm_api3_create_success($returnValues, $params, 'System', 'Check'); -} \ No newline at end of file +} + +/** + * @param $params + * + * @return array + */ +function civicrm_api3_system_log($params) { + $log = new CRM_Utils_SystemLogger(); + // this part means fields with separate db storage are accepted as params which kind of seems more intuitive to me + // because I felt like not doing this required a bunch of explanation in the spec function - but perhaps other won't see it as helpful? + if(!isset($params['context'])) { + $params['context'] = array(); + } + $specialFields = array('contact_id', 'hostname'); + foreach($specialFields as $specialField) { + if(isset($params[$specialField]) && !isset($params['context'])) { + $params['context'][$specialField] = $params[$specialField]; + } + } + $returnValues = $log->log($params['level'], $params['message'], $params['context']); + return civicrm_api3_create_success($returnValues, $params, 'System', 'Log'); +} + +/** + * Metadata for log function + * @param $params + */ +function _civicrm_api3_system_log_spec($params) { + $params['level'] = array( + 'title' => 'Log Level', + 'description' => 'Log level as described in PSR3 (info, debug, warning etc)', + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => TRUE, + ); + $params['message'] = array( + 'title' => 'Log Message', + 'description' => 'Standardised message string, you can also ', + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => TRUE, + ); + $params['context'] = array( + 'title' => 'Log Context', + 'description' => 'An array of additional data to store.', + 'type' => CRM_Utils_Type::T_LONGTEXT, + 'api.default' => array(), + ); + $params['contact_id'] = array( + 'title' => 'Log Contact ID', + 'description' => 'Optional ID of relevant contact', + 'type' => CRM_Utils_Type::T_INT, + ); + $params['hostname'] = array( + 'title' => 'Log Hostname', + 'description' => 'Optional name of host', + 'type' => CRM_Utils_Type::T_STRING, + ); +} diff --git a/api/v3/SystemLog.php b/api/v3/SystemLog.php new file mode 100644 index 0000000000..2c54d4482b --- /dev/null +++ b/api/v3/SystemLog.php @@ -0,0 +1,43 @@ +assertTrue(NULL === CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH)); } + + /** + * Test system log function + */ + function testSystemLog() { + $this->callAPISuccess('system', 'log', array('level' => 'info', 'message' => 'We wish you a merry Christmas')); + $result = $this->callAPISuccess('SystemLog', 'getsingle', array('sequential' => 1, 'message' => array('LIKE' => '%Chris%'))); + $this->assertEquals($result['message'], 'We wish you a merry Christmas'); + $this->assertEquals($result['level'], 'info'); + } } diff --git a/xml/schema/Core/SystemLog.xml b/xml/schema/Core/SystemLog.xml new file mode 100644 index 0000000000..408337f913 --- /dev/null +++ b/xml/schema/Core/SystemLog.xml @@ -0,0 +1,69 @@ + + + + CRM/Core + SystemLog + civicrm_system_log + 4.5 + + id + int unsigned + System Log ID + true + Primary key ID + 4.4 + + + id + true + + + message + System Log Message + true + varchar + 128 + Standardized message + 4.5 + + + context + Detailed Log Data + longtext + JSON encoded data + 4.5 + + + level + Detailed Log Data + error level per PSR3 + varchar + 9 + info + 4.5 + + + timestamp + Log Timestamp + timestamp + Timestamp of when event occurred. + CURRENT_TIMESTAMP + 4.5 + + + contact_id + Log Contact ID + int unsigned + 11 + Optional Contact ID that created the log. Not an FK as we keep this regardless + 4.5 + + + hostname + Log Host + varchar + 128 + Optional Name of logging host + 4.5 + +
diff --git a/xml/schema/Core/files.xml b/xml/schema/Core/files.xml index b3c190f800..ca448f2106 100644 --- a/xml/schema/Core/files.xml +++ b/xml/schema/Core/files.xml @@ -35,6 +35,7 @@ + -- 2.25.1