Skip to content

Commit

Permalink
Bitrix formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
niksamokhvalov committed Nov 27, 2015
1 parent 2f0ecee commit 1123823
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
50 changes: 49 additions & 1 deletion src/Formatter/BitrixFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand All @@ -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',
);
}
}
81 changes: 29 additions & 52 deletions src/Handler/BitrixHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
}

0 comments on commit 1123823

Please sign in to comment.