This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Logger): create Logger class for writing messages to bitrix even…
…t log
- Loading branch information
Constantine Karnaukhov
committed
Oct 8, 2018
1 parent
bbb1a11
commit f6960d5
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace spaceonfire\BMF; | ||
|
||
use CEventLog; | ||
|
||
class Logger { | ||
private $moduleId = null; | ||
private $auditTypeId = null; | ||
|
||
public function __construct($moduleId) { | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters