-
Notifications
You must be signed in to change notification settings - Fork 4
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
Dmitriy Sergeev
committed
Feb 14, 2017
0 parents
commit c3987b4
Showing
6 changed files
with
233 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,3 @@ | ||
.idea/ | ||
vendor/ | ||
bin/ |
Empty file.
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,18 @@ | ||
{ | ||
"name": "asuran/yii-monolog", | ||
"description": "Extension for using monolog with yii 1.x", | ||
"type": "project", | ||
"require": { | ||
"php": ">=5.4", | ||
"yiisoft/yii": "~1.1.14", | ||
"monolog/monolog": "@stable" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^4.8" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"YiiMonolog\\": "src" | ||
} | ||
} | ||
} |
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,109 @@ | ||
<?php | ||
|
||
namespace YiiMonolog; | ||
|
||
use Monolog\Logger; | ||
use Monolog\Registry; | ||
use Monolog\Handler\HandlerInterface; | ||
use Monolog\Formatter\FormatterInterface; | ||
|
||
class MonologComponent extends \CApplicationComponent | ||
{ | ||
/** @var string */ | ||
public $name = 'application'; | ||
/** @var string */ | ||
public $loggerName = 'main'; | ||
/** @var array */ | ||
public $handlers = []; | ||
/** @var array */ | ||
public $processors = []; | ||
|
||
/** | ||
* @inheritdoc | ||
* @throws RuntimeException | ||
*/ | ||
public function init() | ||
{ | ||
$logger = new Logger($this->name); | ||
|
||
foreach ($this->handlers as $handler) { | ||
$logger->pushHandler($this->createHandler($handler)); | ||
} | ||
|
||
foreach ($this->processors as $processor) { | ||
$logger->pushProcessor($this->createProcessor($processor)); | ||
} | ||
|
||
Registry::addLogger($logger, $this->loggerName); | ||
|
||
parent::init(); | ||
} | ||
|
||
/** | ||
* @param string|array $config | ||
* | ||
* @throws RuntimeException | ||
* @return HandlerInterface | ||
*/ | ||
protected function createHandler($config) | ||
{ | ||
if (isset($config['formatter'])) { | ||
$formatterConfig = $config['formatter']; | ||
unset($config['formatter']); | ||
} | ||
|
||
/** @var HandlerInterface $instance */ | ||
if (is_array($config)) { | ||
$instance = call_user_func_array(['Yii', 'createComponent'], $config); | ||
} else { | ||
$instance = \Yii::createComponent($config); | ||
} | ||
|
||
if (isset($formatterConfig)) { | ||
$formatter = $this->createFormatter($formatterConfig); | ||
$instance->setFormatter($formatter); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* @param array|string $config | ||
* | ||
* @throws RuntimeException | ||
* @return Closure | ||
*/ | ||
protected function createProcessor($config) | ||
{ | ||
try { | ||
if (is_array($config)) { | ||
$instance = call_user_func_array(['Yii', 'createComponent'], $config); | ||
} else { | ||
$instance = \Yii::createComponent($config); | ||
} | ||
if (is_callable($instance)) { | ||
return $instance; | ||
} | ||
} catch(Exception $exception) {} | ||
|
||
throw new RuntimeException( | ||
'Unknown processor type, must be a Closure or a valid config for an invokable component' | ||
); | ||
} | ||
|
||
/** | ||
* @param string|array $config | ||
* | ||
* @return FormatterInterface | ||
*/ | ||
protected function createFormatter($config) | ||
{ | ||
if (is_array($config)) { | ||
$instance = call_user_func_array(['Yii', 'createComponent'], $config); | ||
} else { | ||
$instance = \Yii::createComponent($config); | ||
} | ||
|
||
return $instance; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace YiiMonolog; | ||
|
||
use Monolog\Registry; | ||
use Monolog\ErrorHandler; | ||
|
||
class MonologErrorHandler extends \CErrorHandler | ||
{ | ||
/** @var string */ | ||
public $loggerName = 'main'; | ||
/** @var Monolog\ErrorHandler */ | ||
protected $errorHandler; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
$logger = Registry::getInstance($this->loggerName); | ||
$this->errorHandler = new ErrorHandler($logger); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function handleException($e) | ||
{ | ||
$this->errorHandler->handleException($e); | ||
parent::handleException($e); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function handleError($event) | ||
{ | ||
$this->errorHandler->handleError($event->code, $event->message, $event->file, $event->line, $event->params); | ||
parent::handleError($event); | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
namespace YiiMonolog; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Monolog\Registry; | ||
|
||
class MonologLogRoute extends \CLogRoute | ||
{ | ||
/** @var string */ | ||
public $loggerName = 'main'; | ||
/** @var LoggerInterface */ | ||
protected $logger; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
$this->logger = Registry::getInstance($this->loggerName); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function processLogs($logs) | ||
{ | ||
foreach ($logs as $log) { | ||
$this->logger->log( | ||
$this->levelToString($log[1]), | ||
$log[0], | ||
[ | ||
'category' => $log[2], | ||
'timestamp' => $log[3], | ||
] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Convert Yii level string to monolog format | ||
* @param string $level | ||
* | ||
* @return string | ||
*/ | ||
private function levelToString($level) | ||
{ | ||
switch ($level) { | ||
default: | ||
case \CLogger::LEVEL_PROFILE: | ||
case \CLogger::LEVEL_TRACE: | ||
return 'DEBUG'; | ||
case \CLogger::LEVEL_WARNING: | ||
return 'WARNING'; | ||
case \CLogger::LEVEL_ERROR: | ||
return 'ERROR'; | ||
case \CLogger::LEVEL_INFO: | ||
return 'INFO'; | ||
} | ||
} | ||
} |