From f6960d565d9aeb2814e9e52f07c7b01b2e35224e Mon Sep 17 00:00:00 2001 From: Constantine Karnaukhov <1119@zolotoykod.ru> Date: Mon, 8 Oct 2018 17:02:44 +0400 Subject: [PATCH] feat(Logger): create Logger class for writing messages to bitrix event log --- src/Logger.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Module.php | 2 ++ 2 files changed, 87 insertions(+) create mode 100644 src/Logger.php diff --git a/src/Logger.php b/src/Logger.php new file mode 100644 index 0000000..a772017 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,85 @@ +moduleId = $moduleId; + } + + /** + * Get custom audit type id + * @return string | null + */ + public function getAuditTypeId() { + return $this->auditTypeId; + } + + /** + * Set custom audit type id + * @param string $auditTypeId + */ + public function setAuditTypeId($auditTypeId): void { + $this->auditTypeId = $auditTypeId; + } + + /** + * Add message to event log + * @param string | array $message log message string or array with fields to pass into CEventLog::Add + */ + public function log($message): void { + $defaults = [ + 'SEVERITY' => 'INFO', + 'AUDIT_TYPE_ID' => $this->auditTypeId, + 'ITEM_ID' => '', + ]; + + $message = $this->logMessToArray($message); + + CEventLog::Add(array_merge($defaults, $message, ['MODULE_ID' => $this->moduleId])); + } + + public function info($message): void { + $this->log($message); + } + + public function debug($message): void { + $this->log([ + 'SEVERITY' => 'DEBUG', + 'DESCRIPTION' => $message, + ]); + } + + public function warning($message): void { + $message = $this->logMessToArray($message); + + $this->log(array_merge(['SEVERITY' => 'WARNING'], $message)); + } + + public function error($message): void { + $message = $this->logMessToArray($message); + + $this->log(array_merge(['SEVERITY' => 'ERROR'], $message)); + } + + public function security($message): void { + $message = $this->logMessToArray($message); + + $this->log(array_merge(['SEVERITY' => 'SECURITY'], $message)); + } + + private function logMessToArray($mess) { + if (!is_array($mess)) { + $mess = [ + 'DESCRIPTION' => $mess, + ]; + } + + return $mess; + } +} diff --git a/src/Module.php b/src/Module.php index d50cd33..d403ccf 100644 --- a/src/Module.php +++ b/src/Module.php @@ -10,6 +10,7 @@ class Module { private $MODULE_VERSION_DATE; public $options; + public $logger; /** * spaceonfire\BMF\Module constructor. @@ -26,6 +27,7 @@ public function __construct(array $options = []) { $this->MODULE_VERSION_DATE = $options['MODULE_VERSION_DATE']; $this->options = new Options\Manager($this->MODULE_ID); + $this->logger = new Logger($this->MODULE_ID); } /**