Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
feat(Logger): create Logger class for writing messages to bitrix even…
Browse files Browse the repository at this point in the history
…t log
  • Loading branch information
Constantine Karnaukhov committed Oct 8, 2018
1 parent bbb1a11 commit f6960d5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/Logger.php
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;
}
}
2 changes: 2 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Module {
private $MODULE_VERSION_DATE;

public $options;
public $logger;

/**
* spaceonfire\BMF\Module constructor.
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit f6960d5

Please sign in to comment.