-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dd2f0a
commit 9541765
Showing
2 changed files
with
193 additions
and
28 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 |
---|---|---|
|
@@ -8,53 +8,105 @@ | |
namespace Bex\Monolog\Handler; | ||
|
||
use Monolog\Handler\AbstractProcessingHandler; | ||
use Monolog\Logger; | ||
use Monolog\Processor\WebProcessor; | ||
use Bex\Monolog\Processor\BitrixProcessor; | ||
use Bitrix\Main\ArgumentNullException; | ||
|
||
/** | ||
* @author Nik Samokhvalov <[email protected]> | ||
*/ | ||
class BitrixHandler extends AbstractProcessingHandler | ||
{ | ||
protected $audit; | ||
protected $module; | ||
private $event; | ||
private $module; | ||
|
||
/** | ||
* @param string $audit Type of event in the event log. | ||
* @param string $event Type of event in the event log. | ||
* @param string $module Code of the module in Bitrix. | ||
* @param int $level The minimum logging level at which this handler will be triggered | ||
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not | ||
* | ||
* @throws ArgumentNullException If audit is null. | ||
*/ | ||
public function __construct($audit, $module = null, $level = Logger::DEBUG, $bubble = true) | ||
public function __construct($event = null, $module = null, $level = Logger::DEBUG, $bubble = true) | ||
{ | ||
parent::__construct($level, $bubble); | ||
|
||
if (empty($audit)) | ||
{ | ||
throw new ArgumentNullException('audit'); | ||
} | ||
|
||
$this->audit = $audit; | ||
$this->module = $module; | ||
|
||
$this->setEvent($event); | ||
$this->setModule($module); | ||
$this->pushProcessor(new WebProcessor()); | ||
$this->pushProcessor(new BitrixProcessor()); | ||
} | ||
|
||
protected function write(array $record) | ||
{ | ||
/*$arFields = array( | ||
"SEVERITY" => array_key_exists($arFields["SEVERITY"], $arSeverity)? $arFields["SEVERITY"]: "UNKNOWN", | ||
"AUDIT_TYPE_ID" => strlen($arFields["AUDIT_TYPE_ID"]) <= 0? "UNKNOWN": $arFields["AUDIT_TYPE_ID"], | ||
"MODULE_ID" => strlen($arFields["MODULE_ID"]) <= 0? "UNKNOWN": $arFields["MODULE_ID"], | ||
"ITEM_ID" => strlen($arFields["ITEM_ID"]) <= 0? "UNKNOWN": $arFields["ITEM_ID"], | ||
"REMOTE_ADDR" => $_SERVER["REMOTE_ADDR"], | ||
"USER_AGENT" => $_SERVER["HTTP_USER_AGENT"], | ||
"REQUEST_URI" => $url, | ||
"SITE_ID" => strlen($arFields["SITE_ID"]) <= 0 ? $SITE_ID : $arFields["SITE_ID"], | ||
"USER_ID" => is_object($USER) && ($USER->GetID() > 0)? $USER->GetID(): false, | ||
"GUEST_ID" => (isset($_SESSION) && array_key_exists("SESS_GUEST_ID", $_SESSION) && $_SESSION["SESS_GUEST_ID"] > 0? $_SESSION["SESS_GUEST_ID"]: false), | ||
"DESCRIPTION" => $arFields["DESCRIPTION"], | ||
);*/ | ||
|
||
// \CEventLog::Add(); | ||
\CEventLog::Add(array( | ||
'SEVERITY' => static::toBitrixLevel($record['level']), | ||
'AUDIT_TYPE_ID' => $this->getEvent(), | ||
'MODULE_ID' => $this->getModule(), | ||
'ITEM_ID' => isset($record['context']['ITEM_ID']) ? $record['context']['ITEM_ID'] : 'UNKNOWN', | ||
'REMOTE_ADDR' => $record['extra']['ip'], | ||
'USER_AGENT' => $record['extra']['user_agent'], | ||
'REQUEST_URI' => $record['extra']['url'], | ||
'SITE_ID' => $record['extra']['site_id'], | ||
'USER_ID' => $record['extra']['user_id'], | ||
'GUEST_ID' => $record['extra']['guest_id'], | ||
'DESCRIPTION' => $record['message'], | ||
)); | ||
} | ||
|
||
public function setEvent($event) | ||
{ | ||
$this->event = $event; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getEvent() | ||
{ | ||
return ($this->event) ? $this->event : 'UNKNOWN'; | ||
} | ||
|
||
public function setModule($module) | ||
{ | ||
$this->module = $module; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getModule() | ||
{ | ||
return ($this->module) ? $this->module: 'UNKNOWN'; | ||
} | ||
|
||
/** | ||
* Converts PSR-3 levels to Bitrix ones if necessary. | ||
* | ||
* @param int $level Level number. | ||
* | ||
* @return string | ||
*/ | ||
public static function toBitrixLevel($level) | ||
{ | ||
$levels = 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', | ||
); | ||
|
||
if (isset($levels[$level])) | ||
{ | ||
return $levels[$level]; | ||
} | ||
|
||
return 'UNKNOWN'; | ||
} | ||
} |
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,113 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/bitrix-expert/niceaccess | ||
* @copyright Copyright © 2015 Nik Samokhvalov | ||
* @license MIT | ||
*/ | ||
|
||
namespace Bex\Monolog\Processor; | ||
|
||
use Bitrix\Main\Context; | ||
|
||
/** | ||
* @author Nik Samokhvalov <[email protected]> | ||
*/ | ||
class BitrixProcessor | ||
{ | ||
protected $extra = array(); | ||
|
||
/** | ||
* @param array $extra Extra fields. | ||
*/ | ||
public function __construct(array $extra = null) | ||
{ | ||
if ($extra !== null) | ||
{ | ||
$this->extra = $extra; | ||
} | ||
} | ||
|
||
/** | ||
* Adds extra field. | ||
* | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
public function addExtraField($name, $value) | ||
{ | ||
$this->extra[$name] = $value; | ||
} | ||
|
||
/** | ||
* Extra fields of processor. | ||
* | ||
* @return array | ||
*/ | ||
public function fields() | ||
{ | ||
return array( | ||
'site_id', | ||
'user_id', | ||
'guest_id', | ||
'user_agent', | ||
); | ||
} | ||
|
||
/** | ||
* @param array $record | ||
* | ||
* @return array | ||
*/ | ||
public function __invoke(array $record) | ||
{ | ||
// skip processing if for some reason request data | ||
// is not present (CLI or wonky SAPIs) | ||
if (Context::getCurrent()->getServer()->getRequestUri() === null) | ||
{ | ||
return $record; | ||
} | ||
|
||
$record['extra'] = $this->getExtraFields(); | ||
|
||
return $record; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getExtraFields() | ||
{ | ||
global $USER; | ||
|
||
$extra = array(); | ||
|
||
foreach ($this->fields() as $field) | ||
{ | ||
if (!isset($this->extra[$field])) | ||
{ | ||
switch ($field) | ||
{ | ||
case 'site_id': | ||
$value = Context::getCurrent()->getSite(); | ||
break; | ||
|
||
case 'user_id': | ||
$value = is_object($USER) && ($USER->GetID() > 0) ? $USER->GetID() : false; | ||
break; | ||
|
||
case 'guest_id': | ||
$value = (isset($_SESSION) && array_key_exists('SESS_GUEST_ID', $_SESSION) && $_SESSION['SESS_GUEST_ID'] > 0 ? $_SESSION['SESS_GUEST_ID'] : false); | ||
break; | ||
|
||
case 'user_agent': | ||
$value = Context::getCurrent()->getServer()->get('HTTP_USER_AGENT'); | ||
break; | ||
} | ||
} | ||
|
||
$extra[$field] = $value; | ||
} | ||
|
||
return $extra; | ||
} | ||
} |