Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Sergeev committed Feb 14, 2017
0 parents commit c3987b4
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
bin/
Empty file added README.md
Empty file.
18 changes: 18 additions & 0 deletions composer.json
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"
}
}
}
109 changes: 109 additions & 0 deletions src/MonologComponent.php
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;
}
}
42 changes: 42 additions & 0 deletions src/MonologErrorHandler.php
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);
}
}
61 changes: 61 additions & 0 deletions src/MonologLogRoute.php
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';
}
}
}

0 comments on commit c3987b4

Please sign in to comment.