From 12c48d1ae45885c71b3aeaff357c0dd276c9195e Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 2 Jul 2013 22:09:25 -0400 Subject: [PATCH] Refactored logging. --- apps/advanced/backend/config/main.php | 3 +- apps/advanced/console/config/main.php | 4 +- .../environments/dev/backend/config/main-local.php | 2 +- .../dev/frontend/config/main-local.php | 2 +- apps/advanced/frontend/config/main.php | 4 +- apps/basic/config/console.php | 3 +- apps/basic/config/web.php | 4 +- framework/yii/base/Application.php | 10 +++ framework/yii/base/Module.php | 1 - framework/yii/classes.php | 12 +-- framework/yii/debug/LogTarget.php | 2 +- framework/yii/debug/Module.php | 2 +- framework/yii/log/Logger.php | 18 ++-- framework/yii/log/Router.php | 98 ---------------------- 14 files changed, 38 insertions(+), 127 deletions(-) delete mode 100644 framework/yii/log/Router.php diff --git a/apps/advanced/backend/config/main.php b/apps/advanced/backend/config/main.php index 3140cd2..88838b9 100644 --- a/apps/advanced/backend/config/main.php +++ b/apps/advanced/backend/config/main.php @@ -27,10 +27,9 @@ return array( 'bundles' => require(__DIR__ . '/assets.php'), ), 'log' => array( - 'class' => 'yii\logging\Router', 'targets' => array( array( - 'class' => 'yii\logging\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => array('error', 'warning'), ), ), diff --git a/apps/advanced/console/config/main.php b/apps/advanced/console/config/main.php index 37db1d2..7a223c3 100644 --- a/apps/advanced/console/config/main.php +++ b/apps/advanced/console/config/main.php @@ -12,7 +12,6 @@ return array( 'id' => 'app-console', 'basePath' => dirname(__DIR__), 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', - 'preload' => array('log'), 'controllerNamespace' => 'console\controllers', 'modules' => array( ), @@ -20,10 +19,9 @@ return array( 'db' => $params['components.db'], 'cache' => $params['components.cache'], 'log' => array( - 'class' => 'yii\logging\Router', 'targets' => array( array( - 'class' => 'yii\logging\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => array('error', 'warning'), ), ), diff --git a/apps/advanced/environments/dev/backend/config/main-local.php b/apps/advanced/environments/dev/backend/config/main-local.php index f74bfa3..fdc131d 100644 --- a/apps/advanced/environments/dev/backend/config/main-local.php +++ b/apps/advanced/environments/dev/backend/config/main-local.php @@ -9,7 +9,7 @@ return array( 'log' => array( 'targets' => array( // array( -// 'class' => 'yii\logging\DebugTarget', +// 'class' => 'yii\log\DebugTarget', // ) ), ), diff --git a/apps/advanced/environments/dev/frontend/config/main-local.php b/apps/advanced/environments/dev/frontend/config/main-local.php index b77abed..f7d77e3 100644 --- a/apps/advanced/environments/dev/frontend/config/main-local.php +++ b/apps/advanced/environments/dev/frontend/config/main-local.php @@ -9,7 +9,7 @@ return array( 'log' => array( 'targets' => array( // array( -// 'class' => 'yii\logging\DebugTarget', +// 'class' => 'yii\log\DebugTarget', // ) ), ), diff --git a/apps/advanced/frontend/config/main.php b/apps/advanced/frontend/config/main.php index e53cfe8..c79df90 100644 --- a/apps/advanced/frontend/config/main.php +++ b/apps/advanced/frontend/config/main.php @@ -12,7 +12,6 @@ return array( 'id' => 'app-frontend', 'basePath' => dirname(__DIR__), 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', - 'preload' => array('log'), 'controllerNamespace' => 'frontend\controllers', 'modules' => array( ), @@ -27,10 +26,9 @@ return array( 'bundles' => require(__DIR__ . '/assets.php'), ), 'log' => array( - 'class' => 'yii\logging\Router', 'targets' => array( array( - 'class' => 'yii\logging\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => array('error', 'warning'), ), ), diff --git a/apps/basic/config/console.php b/apps/basic/config/console.php index bfb3ed7..12f13cd 100644 --- a/apps/basic/config/console.php +++ b/apps/basic/config/console.php @@ -13,10 +13,9 @@ return array( 'class' => 'yii\caching\FileCache', ), 'log' => array( - 'class' => 'yii\logging\Router', 'targets' => array( array( - 'class' => 'yii\logging\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => array('error', 'warning'), ), ), diff --git a/apps/basic/config/web.php b/apps/basic/config/web.php index 8063b7c..bea08cb 100644 --- a/apps/basic/config/web.php +++ b/apps/basic/config/web.php @@ -3,7 +3,6 @@ return array( 'id' => 'bootstrap', 'basePath' => dirname(__DIR__), - 'preload' => array('log'), 'components' => array( 'cache' => array( 'class' => 'yii\caching\FileCache', @@ -16,10 +15,9 @@ return array( 'bundles' => require(__DIR__ . '/assets.php'), ), 'log' => array( - 'class' => 'yii\logging\Router', 'targets' => array( array( - 'class' => 'yii\logging\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => array('error', 'warning'), ), ), diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 9b1dec0..55f1ab0 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -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. */ public function registerErrorHandlers() diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 3463474..dc9d7c6 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -459,7 +459,6 @@ abstract class Module extends Component if ($this->_components[$id] instanceof Object) { return $this->_components[$id]; } elseif ($load) { - Yii::trace("Loading component: $id", __METHOD__); return $this->_components[$id] = Yii::createObject($this->_components[$id]); } } diff --git a/framework/yii/classes.php b/framework/yii/classes.php index ce40383..9165ddd 100644 --- a/framework/yii/classes.php +++ b/framework/yii/classes.php @@ -41,12 +41,12 @@ return array( 'yii\web\AssetBundle' => YII_PATH . '/web/AssetBundle.php', 'yii\web\AssetConverter' => YII_PATH . '/web/AssetConverter.php', 'yii\web\HeaderCollection' => YII_PATH . '/web/HeaderCollection.php', -'yii\log\Target' => YII_PATH . '/logging/Target.php', -'yii\log\DebugTarget' => YII_PATH . '/logging/DebugTarget.php', -'yii\log\Logger' => YII_PATH . '/logging/Logger.php', -'yii\log\EmailTarget' => YII_PATH . '/logging/EmailTarget.php', -'yii\log\DbTarget' => YII_PATH . '/logging/DbTarget.php', -'yii\log\FileTarget' => YII_PATH . '/logging/FileTarget.php', +'yii\log\Target' => YII_PATH . '/log/Target.php', +'yii\log\DebugTarget' => YII_PATH . '/log/DebugTarget.php', +'yii\log\Logger' => YII_PATH . '/log/Logger.php', +'yii\log\EmailTarget' => YII_PATH . '/log/EmailTarget.php', +'yii\log\DbTarget' => YII_PATH . '/log/DbTarget.php', +'yii\log\FileTarget' => YII_PATH . '/log/FileTarget.php', 'yii\widgets\ActiveField' => YII_PATH . '/widgets/ActiveField.php', 'yii\widgets\Captcha' => YII_PATH . '/widgets/Captcha.php', 'yii\widgets\ListPager' => YII_PATH . '/widgets/ListPager.php', diff --git a/framework/yii/debug/LogTarget.php b/framework/yii/debug/LogTarget.php index 82aa127..d7fd98f 100644 --- a/framework/yii/debug/LogTarget.php +++ b/framework/yii/debug/LogTarget.php @@ -30,7 +30,7 @@ class LogTarget extends Target if (!is_dir($path)) { mkdir($path); } - $file = $path . '/' . Yii::getLogger()->getTag() . '.log'; + $file = $path . '/' . Yii::$app->getLog()->getTag() . '.log'; $data = array( 'messages' => $messages, '_SERVER' => $_SERVER, diff --git a/framework/yii/debug/Module.php b/framework/yii/debug/Module.php index 84bf399..a1f8aa0 100644 --- a/framework/yii/debug/Module.php +++ b/framework/yii/debug/Module.php @@ -39,7 +39,7 @@ class Module extends \yii\base\Module /** @var View $view */ $id = 'yii-debug-toolbar'; $url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array( - 'tag' => Yii::getLogger()->tag, + 'tag' => Yii::$app->getLog()->getTag(), )); $view = $event->sender; $view->registerJs("yii.debug.load('$id', '$url');"); diff --git a/framework/yii/log/Logger.php b/framework/yii/log/Logger.php index c0bc16b..e1d81e2 100644 --- a/framework/yii/log/Logger.php +++ b/framework/yii/log/Logger.php @@ -7,8 +7,9 @@ namespace yii\log; -use \yii\base\Component; -use \yii\base\InvalidConfigException; +use Yii; +use yii\base\Component; +use yii\base\InvalidConfigException; /** * Logger records logged messages in memory and sends them to different targets as needed. @@ -141,6 +142,11 @@ class Logger extends Component public function 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); } @@ -177,13 +183,15 @@ class Logger extends Component /** * 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. */ public function flush($final = false) { - if ($this->router) { - $this->router->dispatch($this->messages, $final); + /** @var Target $target */ + foreach ($this->targets as $target) { + if ($target->enabled) { + $target->collect($this->messages, $final); + } } $this->messages = array(); } diff --git a/framework/yii/log/Router.php b/framework/yii/log/Router.php deleted file mode 100644 index c7d9ac3..0000000 --- a/framework/yii/log/Router.php +++ /dev/null @@ -1,98 +0,0 @@ - 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 - * @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); - } - } - } -}