From 1123823dc7ec8b2743d334cf16b6959031007c1b Mon Sep 17 00:00:00 2001 From: Nik Samokhvalov <nik@samokhvalov.info> Date: Fri, 27 Nov 2015 09:53:43 +0300 Subject: [PATCH] Bitrix formatter --- src/Formatter/BitrixFormatter.php | 50 ++++++++++++++++++- src/Handler/BitrixHandler.php | 81 +++++++++++-------------------- 2 files changed, 78 insertions(+), 53 deletions(-) diff --git a/src/Formatter/BitrixFormatter.php b/src/Formatter/BitrixFormatter.php index 66bf388..b000323 100644 --- a/src/Formatter/BitrixFormatter.php +++ b/src/Formatter/BitrixFormatter.php @@ -19,11 +19,21 @@ class BitrixFormatter implements FormatterInterface */ public function format(array $record) { + $record['item_id'] = null; + $record['level'] = static::toBitrixLevel($record['level']); + if (!empty($record['context'])) { foreach ($record['context'] as $name => $value) { - $record['message'] .= "<br><b>" . $name . '</b>: ' . $value; + if ($name === 'item_id') + { + $record['item_id'] = $value; + } + else + { + $record['message'] .= "<br><b>" . $name . '</b>: ' . $value; + } } } @@ -44,4 +54,42 @@ public function formatBatch(array $records) return $formatted; } + + /** + * Converts Monolog levels to Bitrix ones if necessary. + * + * @param int $level Level number. + * + * @return string|bool + */ + public static function toBitrixLevel($level) + { + $levels = static::logLevels(); + + if (isset($levels[$level])) + { + return $levels[$level]; + } + + return false; + } + + /** + * Translates Monolog log levels to Bitrix levels. + * + * @return array + */ + public static function logLevels() + { + return array( + Logger::DEBUG => 'DEBUG', + Logger::INFO => 'INFO', + Logger::NOTICE => 'WARNING', + Logger::WARNING => 'WARNING', + Logger::ERROR => 'ERROR', + Logger::CRITICAL => 'ERROR', + Logger::ALERT => 'ERROR', + Logger::EMERGENCY => 'ERROR', + ); + } } diff --git a/src/Handler/BitrixHandler.php b/src/Handler/BitrixHandler.php index da638eb..01786ee 100644 --- a/src/Handler/BitrixHandler.php +++ b/src/Handler/BitrixHandler.php @@ -17,9 +17,9 @@ */ class BitrixHandler extends AbstractProcessingHandler { - protected $event; - protected $module; - protected $siteId; + private $event; + private $module; + private $siteId; /** * @param string $event Type of event in the event log. @@ -33,8 +33,8 @@ public function __construct($event = null, $module = null, $level = Logger::DEBU { parent::__construct($level, $bubble); - $this->event = $event; - $this->module = $module; + $this->setEvent($event); + $this->setModule($module); } /** @@ -43,73 +43,50 @@ public function __construct($event = null, $module = null, $level = Logger::DEBU protected function write(array $record) { \CEventLog::Log( - static::toBitrixLevel($record['level']), - $this->event, - $this->module, - (isset($record['context']['ITEM_ID'])) ? $record['context']['ITEM_ID'] : null, + $record['level'], + $this->getEvent(), + $this->getModule(), + $record['item_id'], $record['message'], - $this->siteId + $this->getSite() ); } + /** + * {@inheritdoc} + */ + protected function getDefaultFormatter() + { + return new BitrixFormatter(); + } + public function setEvent($event) { $this->event = $event; } - public function setModule($module) + public function getEvent() { - $this->module = $module; + return $this->event; } - public function setSite($siteId) + public function setModule($module) { - $this->siteId = $siteId; + $this->module = $module; } - /** - * {@inheritdoc} - */ - protected function getDefaultFormatter() + public function getModule() { - return new BitrixFormatter(); + return $this->module; } - - /** - * Converts Monolog levels to Bitrix ones if necessary. - * - * @param int $level Level number. - * - * @return string|bool - */ - public static function toBitrixLevel($level) - { - $levels = static::logLevels(); - if (isset($levels[$level])) - { - return $levels[$level]; - } - - return false; + public function setSite($siteId) + { + $this->siteId = $siteId; } - /** - * Translates Monolog log levels to Bitrix levels. - * - * @return array - */ - public static function logLevels() + public function getSite() { - return array( - Logger::DEBUG => 'DEBUG', - Logger::INFO => 'INFO', - Logger::NOTICE => 'WARNING', - Logger::WARNING => 'WARNING', - Logger::ERROR => 'ERROR', - Logger::CRITICAL => 'ERROR', - Logger::ALERT => 'ERROR', - Logger::EMERGENCY => 'ERROR', - ); + return $this->siteId; } } \ No newline at end of file