Commit 12c48d1a by Qiang Xue

Refactored logging.

parent 689e5d16
...@@ -27,10 +27,9 @@ return array( ...@@ -27,10 +27,9 @@ return array(
'bundles' => require(__DIR__ . '/assets.php'), 'bundles' => require(__DIR__ . '/assets.php'),
), ),
'log' => array( 'log' => array(
'class' => 'yii\logging\Router',
'targets' => array( 'targets' => array(
array( array(
'class' => 'yii\logging\FileTarget', 'class' => 'yii\log\FileTarget',
'levels' => array('error', 'warning'), 'levels' => array('error', 'warning'),
), ),
), ),
......
...@@ -12,7 +12,6 @@ return array( ...@@ -12,7 +12,6 @@ return array(
'id' => 'app-console', 'id' => 'app-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'preload' => array('log'),
'controllerNamespace' => 'console\controllers', 'controllerNamespace' => 'console\controllers',
'modules' => array( 'modules' => array(
), ),
...@@ -20,10 +19,9 @@ return array( ...@@ -20,10 +19,9 @@ return array(
'db' => $params['components.db'], 'db' => $params['components.db'],
'cache' => $params['components.cache'], 'cache' => $params['components.cache'],
'log' => array( 'log' => array(
'class' => 'yii\logging\Router',
'targets' => array( 'targets' => array(
array( array(
'class' => 'yii\logging\FileTarget', 'class' => 'yii\log\FileTarget',
'levels' => array('error', 'warning'), 'levels' => array('error', 'warning'),
), ),
), ),
......
...@@ -9,7 +9,7 @@ return array( ...@@ -9,7 +9,7 @@ return array(
'log' => array( 'log' => array(
'targets' => array( 'targets' => array(
// array( // array(
// 'class' => 'yii\logging\DebugTarget', // 'class' => 'yii\log\DebugTarget',
// ) // )
), ),
), ),
......
...@@ -9,7 +9,7 @@ return array( ...@@ -9,7 +9,7 @@ return array(
'log' => array( 'log' => array(
'targets' => array( 'targets' => array(
// array( // array(
// 'class' => 'yii\logging\DebugTarget', // 'class' => 'yii\log\DebugTarget',
// ) // )
), ),
), ),
......
...@@ -12,7 +12,6 @@ return array( ...@@ -12,7 +12,6 @@ return array(
'id' => 'app-frontend', 'id' => 'app-frontend',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'preload' => array('log'),
'controllerNamespace' => 'frontend\controllers', 'controllerNamespace' => 'frontend\controllers',
'modules' => array( 'modules' => array(
), ),
...@@ -27,10 +26,9 @@ return array( ...@@ -27,10 +26,9 @@ return array(
'bundles' => require(__DIR__ . '/assets.php'), 'bundles' => require(__DIR__ . '/assets.php'),
), ),
'log' => array( 'log' => array(
'class' => 'yii\logging\Router',
'targets' => array( 'targets' => array(
array( array(
'class' => 'yii\logging\FileTarget', 'class' => 'yii\log\FileTarget',
'levels' => array('error', 'warning'), 'levels' => array('error', 'warning'),
), ),
), ),
......
...@@ -13,10 +13,9 @@ return array( ...@@ -13,10 +13,9 @@ return array(
'class' => 'yii\caching\FileCache', 'class' => 'yii\caching\FileCache',
), ),
'log' => array( 'log' => array(
'class' => 'yii\logging\Router',
'targets' => array( 'targets' => array(
array( array(
'class' => 'yii\logging\FileTarget', 'class' => 'yii\log\FileTarget',
'levels' => array('error', 'warning'), 'levels' => array('error', 'warning'),
), ),
), ),
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
return array( return array(
'id' => 'bootstrap', 'id' => 'bootstrap',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => array('log'),
'components' => array( 'components' => array(
'cache' => array( 'cache' => array(
'class' => 'yii\caching\FileCache', 'class' => 'yii\caching\FileCache',
...@@ -16,10 +15,9 @@ return array( ...@@ -16,10 +15,9 @@ return array(
'bundles' => require(__DIR__ . '/assets.php'), 'bundles' => require(__DIR__ . '/assets.php'),
), ),
'log' => array( 'log' => array(
'class' => 'yii\logging\Router',
'targets' => array( 'targets' => array(
array( array(
'class' => 'yii\logging\FileTarget', 'class' => 'yii\log\FileTarget',
'levels' => array('error', 'warning'), 'levels' => array('error', 'warning'),
), ),
), ),
......
...@@ -141,6 +141,16 @@ abstract class Application extends Module ...@@ -141,6 +141,16 @@ abstract class Application extends Module
} }
/** /**
* Loads components that are declared in [[preload]].
* @throws InvalidConfigException if a component or module to be preloaded is unknown
*/
public function preloadComponents()
{
$this->getComponent('log');
parent::preloadComponents();
}
/**
* Registers error handlers. * Registers error handlers.
*/ */
public function registerErrorHandlers() public function registerErrorHandlers()
......
...@@ -459,7 +459,6 @@ abstract class Module extends Component ...@@ -459,7 +459,6 @@ abstract class Module extends Component
if ($this->_components[$id] instanceof Object) { if ($this->_components[$id] instanceof Object) {
return $this->_components[$id]; return $this->_components[$id];
} elseif ($load) { } elseif ($load) {
Yii::trace("Loading component: $id", __METHOD__);
return $this->_components[$id] = Yii::createObject($this->_components[$id]); return $this->_components[$id] = Yii::createObject($this->_components[$id]);
} }
} }
......
...@@ -41,12 +41,12 @@ return array( ...@@ -41,12 +41,12 @@ return array(
'yii\web\AssetBundle' => YII_PATH . '/web/AssetBundle.php', 'yii\web\AssetBundle' => YII_PATH . '/web/AssetBundle.php',
'yii\web\AssetConverter' => YII_PATH . '/web/AssetConverter.php', 'yii\web\AssetConverter' => YII_PATH . '/web/AssetConverter.php',
'yii\web\HeaderCollection' => YII_PATH . '/web/HeaderCollection.php', 'yii\web\HeaderCollection' => YII_PATH . '/web/HeaderCollection.php',
'yii\log\Target' => YII_PATH . '/logging/Target.php', 'yii\log\Target' => YII_PATH . '/log/Target.php',
'yii\log\DebugTarget' => YII_PATH . '/logging/DebugTarget.php', 'yii\log\DebugTarget' => YII_PATH . '/log/DebugTarget.php',
'yii\log\Logger' => YII_PATH . '/logging/Logger.php', 'yii\log\Logger' => YII_PATH . '/log/Logger.php',
'yii\log\EmailTarget' => YII_PATH . '/logging/EmailTarget.php', 'yii\log\EmailTarget' => YII_PATH . '/log/EmailTarget.php',
'yii\log\DbTarget' => YII_PATH . '/logging/DbTarget.php', 'yii\log\DbTarget' => YII_PATH . '/log/DbTarget.php',
'yii\log\FileTarget' => YII_PATH . '/logging/FileTarget.php', 'yii\log\FileTarget' => YII_PATH . '/log/FileTarget.php',
'yii\widgets\ActiveField' => YII_PATH . '/widgets/ActiveField.php', 'yii\widgets\ActiveField' => YII_PATH . '/widgets/ActiveField.php',
'yii\widgets\Captcha' => YII_PATH . '/widgets/Captcha.php', 'yii\widgets\Captcha' => YII_PATH . '/widgets/Captcha.php',
'yii\widgets\ListPager' => YII_PATH . '/widgets/ListPager.php', 'yii\widgets\ListPager' => YII_PATH . '/widgets/ListPager.php',
......
...@@ -30,7 +30,7 @@ class LogTarget extends Target ...@@ -30,7 +30,7 @@ class LogTarget extends Target
if (!is_dir($path)) { if (!is_dir($path)) {
mkdir($path); mkdir($path);
} }
$file = $path . '/' . Yii::getLogger()->getTag() . '.log'; $file = $path . '/' . Yii::$app->getLog()->getTag() . '.log';
$data = array( $data = array(
'messages' => $messages, 'messages' => $messages,
'_SERVER' => $_SERVER, '_SERVER' => $_SERVER,
......
...@@ -39,7 +39,7 @@ class Module extends \yii\base\Module ...@@ -39,7 +39,7 @@ class Module extends \yii\base\Module
/** @var View $view */ /** @var View $view */
$id = 'yii-debug-toolbar'; $id = 'yii-debug-toolbar';
$url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array( $url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array(
'tag' => Yii::getLogger()->tag, 'tag' => Yii::$app->getLog()->getTag(),
)); ));
$view = $event->sender; $view = $event->sender;
$view->registerJs("yii.debug.load('$id', '$url');"); $view->registerJs("yii.debug.load('$id', '$url');");
......
...@@ -7,8 +7,9 @@ ...@@ -7,8 +7,9 @@
namespace yii\log; namespace yii\log;
use \yii\base\Component; use Yii;
use \yii\base\InvalidConfigException; use yii\base\Component;
use yii\base\InvalidConfigException;
/** /**
* Logger records logged messages in memory and sends them to different targets as needed. * Logger records logged messages in memory and sends them to different targets as needed.
...@@ -141,6 +142,11 @@ class Logger extends Component ...@@ -141,6 +142,11 @@ class Logger extends Component
public function init() public function init()
{ {
parent::init(); parent::init();
foreach ($this->targets as $name => $target) {
if (!$target instanceof Target) {
$this->targets[$name] = Yii::createObject($target);
}
}
register_shutdown_function(array($this, 'flush'), true); register_shutdown_function(array($this, 'flush'), true);
} }
...@@ -177,13 +183,15 @@ class Logger extends Component ...@@ -177,13 +183,15 @@ class Logger extends Component
/** /**
* Flushes log messages from memory to targets. * Flushes log messages from memory to targets.
* This method will trigger an [[EVENT_FLUSH]] or [[EVENT_FINAL_FLUSH]] event depending on the $final value.
* @param boolean $final whether this is a final call during a request. * @param boolean $final whether this is a final call during a request.
*/ */
public function flush($final = false) public function flush($final = false)
{ {
if ($this->router) { /** @var Target $target */
$this->router->dispatch($this->messages, $final); foreach ($this->targets as $target) {
if ($target->enabled) {
$target->collect($this->messages, $final);
}
} }
$this->messages = array(); $this->messages = array();
} }
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\log;
use Yii;
use yii\base\Component;
/**
* Router manages [[Target|log targets]] that record log messages in different media.
*
* For example, a [[FileTarget|file log target]] records log messages
* in files; an [[EmailTarget|email log target]] sends log messages
* to specific email addresses. Each log target may specify filters on
* message levels and categories to record specific messages only.
*
* Router and the targets it manages may be configured in application configuration,
* like the following:
*
* ~~~
* array(
* // preload log component when application starts
* 'preload' => array('log'),
* 'components' => array(
* 'log' => array(
* 'class' => 'yii\log\Router',
* 'targets' => array(
* 'file' => array(
* 'class' => 'yii\log\FileTarget',
* 'levels' => array('trace', 'info'),
* 'categories' => array('yii\*'),
* ),
* 'email' => array(
* 'class' => 'yii\log\EmailTarget',
* 'levels' => array('error', 'warning'),
* 'emails' => array('admin@example.com'),
* ),
* ),
* ),
* ),
* )
* ~~~
*
* Each log target can have a name and can be referenced via the [[targets]] property
* as follows:
*
* ~~~
* Yii::$app->log->targets['file']->enabled = false;
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Router extends Component
{
/**
* @var Target[] list of log target objects or configurations. If the latter, target objects will
* be created in [[init()]] by calling [[Yii::createObject()]] with the corresponding object configuration.
*/
public $targets = array();
/**
* Initializes this application component.
* This method is invoked when the Router component is created by the application.
* The method attaches the [[processLogs]] method to both the [[Logger::EVENT_FLUSH]] event
* and the [[Logger::EVENT_FINAL_FLUSH]] event.
*/
public function init()
{
parent::init();
foreach ($this->targets as $name => $target) {
if (!$target instanceof Target) {
$this->targets[$name] = Yii::createObject($target);
}
}
Yii::getLogger()->router = $this;
}
/**
* Dispatches log messages to [[targets]].
* This method is called by [[Logger]] when its [[Logger::flush()]] method is called.
* It will forward the messages to each log target registered in [[targets]].
* @param array $messages the messages to be processed
* @param boolean $final whether this is the final call during a request cycle
*/
public function dispatch($messages, $final = false)
{
foreach ($this->targets as $target) {
if ($target->enabled) {
$target->collect($messages, $final);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment