diff --git a/framework/yii/YiiBase.php b/framework/yii/YiiBase.php index 5c48234..d91df88 100644 --- a/framework/yii/YiiBase.php +++ b/framework/yii/YiiBase.php @@ -10,7 +10,7 @@ use yii\base\Exception; use yii\base\InvalidConfigException; use yii\base\InvalidParamException; use yii\base\UnknownClassException; -use yii\logging\Logger; +use yii\log\Logger; /** * Gets the application start timestamp. @@ -478,7 +478,7 @@ class YiiBase public static function trace($message, $category = 'application') { if (YII_DEBUG) { - self::getLogger()->log($message, Logger::LEVEL_TRACE, $category); + self::$app->getLog()->log($message, Logger::LEVEL_TRACE, $category); } } @@ -491,7 +491,7 @@ class YiiBase */ public static function error($message, $category = 'application') { - self::getLogger()->log($message, Logger::LEVEL_ERROR, $category); + self::$app->getLog()->log($message, Logger::LEVEL_ERROR, $category); } /** @@ -503,7 +503,7 @@ class YiiBase */ public static function warning($message, $category = 'application') { - self::getLogger()->log($message, Logger::LEVEL_WARNING, $category); + self::$app->getLog()->log($message, Logger::LEVEL_WARNING, $category); } /** @@ -515,7 +515,7 @@ class YiiBase */ public static function info($message, $category = 'application') { - self::getLogger()->log($message, Logger::LEVEL_INFO, $category); + self::$app->getLog()->log($message, Logger::LEVEL_INFO, $category); } /** @@ -537,7 +537,7 @@ class YiiBase */ public static function beginProfile($token, $category = 'application') { - self::getLogger()->log($token, Logger::LEVEL_PROFILE_BEGIN, $category); + self::$app->getLog()->log($token, Logger::LEVEL_PROFILE_BEGIN, $category); } /** @@ -549,29 +549,7 @@ class YiiBase */ public static function endProfile($token, $category = 'application') { - self::getLogger()->log($token, Logger::LEVEL_PROFILE_END, $category); - } - - /** - * Returns the message logger object. - * @return \yii\logging\Logger message logger - */ - public static function getLogger() - { - if (self::$_logger !== null) { - return self::$_logger; - } else { - return self::$_logger = new Logger; - } - } - - /** - * Sets the logger object. - * @param Logger $logger the logger object. - */ - public static function setLogger($logger) - { - self::$_logger = $logger; + self::$app->getLog()->log($token, Logger::LEVEL_PROFILE_END, $category); } /** diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 2d2157c..9b1dec0 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -266,6 +266,15 @@ abstract class Application extends Module } /** + * Returns the log component. + * @return \yii\log\Logger the log component + */ + public function getLog() + { + return $this->getComponent('log'); + } + + /** * Returns the error handler component. * @return ErrorHandler the error handler application component. */ @@ -344,6 +353,9 @@ abstract class Application extends Module public function registerCoreComponents() { $this->setComponents(array( + 'log' => array( + 'class' => 'yii\log\Logger', + ), 'errorHandler' => array( 'class' => 'yii\base\ErrorHandler', ), diff --git a/framework/yii/classes.php b/framework/yii/classes.php index 81d02e4..ce40383 100644 --- a/framework/yii/classes.php +++ b/framework/yii/classes.php @@ -41,13 +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\logging\Target' => YII_PATH . '/logging/Target.php', -'yii\logging\DebugTarget' => YII_PATH . '/logging/DebugTarget.php', -'yii\logging\Router' => YII_PATH . '/logging/Router.php', -'yii\logging\Logger' => YII_PATH . '/logging/Logger.php', -'yii\logging\EmailTarget' => YII_PATH . '/logging/EmailTarget.php', -'yii\logging\DbTarget' => YII_PATH . '/logging/DbTarget.php', -'yii\logging\FileTarget' => YII_PATH . '/logging/FileTarget.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\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 584da6c..82aa127 100644 --- a/framework/yii/debug/LogTarget.php +++ b/framework/yii/debug/LogTarget.php @@ -8,7 +8,7 @@ namespace yii\debug; use Yii; -use yii\logging\Target; +use yii\log\Target; /** * @author Qiang Xue diff --git a/framework/yii/logging/DbTarget.php b/framework/yii/log/DbTarget.php similarity index 99% rename from framework/yii/logging/DbTarget.php rename to framework/yii/log/DbTarget.php index ce9d843..16b1eab 100644 --- a/framework/yii/logging/DbTarget.php +++ b/framework/yii/log/DbTarget.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\logging; +namespace yii\log; use Yii; use yii\db\Connection; diff --git a/framework/yii/logging/EmailTarget.php b/framework/yii/log/EmailTarget.php similarity index 98% rename from framework/yii/logging/EmailTarget.php rename to framework/yii/log/EmailTarget.php index 94e2c00..df4f9e0 100644 --- a/framework/yii/logging/EmailTarget.php +++ b/framework/yii/log/EmailTarget.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\logging; +namespace yii\log; /** * EmailTarget sends selected log messages to the specified email addresses. diff --git a/framework/yii/logging/FileTarget.php b/framework/yii/log/FileTarget.php similarity index 99% rename from framework/yii/logging/FileTarget.php rename to framework/yii/log/FileTarget.php index 2db43b5..c4cd40d 100644 --- a/framework/yii/logging/FileTarget.php +++ b/framework/yii/log/FileTarget.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\logging; +namespace yii\log; use Yii; use yii\base\InvalidConfigException; diff --git a/framework/yii/logging/Logger.php b/framework/yii/log/Logger.php similarity index 80% rename from framework/yii/logging/Logger.php rename to framework/yii/log/Logger.php index 4bd6bcc..c0bc16b 100644 --- a/framework/yii/logging/Logger.php +++ b/framework/yii/log/Logger.php @@ -5,13 +5,57 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\logging; +namespace yii\log; use \yii\base\Component; use \yii\base\InvalidConfigException; /** - * Logger records logged messages in memory. + * Logger records logged messages in memory and sends them to different targets as needed. + * + * Logger is registered as a core application component and can be accessed using `Yii::$app->log`. + * You can call the method [[log()]] to record a single log message. For convenience, a set of shortcut + * methods are provided for logging messages of various severity levels via the [[Yii]] class: + * + * - [[Yii::trace()]] + * - [[Yii::error()]] + * - [[Yii::warning()]] + * - [[Yii::info()]] + * - [[Yii::beginProfile()]] + * - [[Yii::endProfile()]] + * + * When enough messages are accumulated in the logger, or when the current request finishes, + * the logged messages will be sent to different [[targets]], such as log files, emails. + * + * You may configure the targets in application configuration, like the following: + * + * ~~~ + * array( + * 'components' => array( + * 'log' => array( + * '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; + * ~~~ * * When the application ends or [[flushInterval]] is reached, Logger will call [[flush()]] * to send logged messages to different log targets, such as file, email, Web. @@ -65,7 +109,7 @@ class Logger extends Component */ public $flushInterval = 1000; /** - * @var array logged messages. This property is mainly managed by [[log()]] and [[flush()]]. + * @var array logged messages. This property is managed by [[log()]] and [[flush()]]. * Each log message is of the following structure: * * ~~~ @@ -79,9 +123,10 @@ class Logger extends Component */ public $messages = array(); /** - * @var Router the log target router registered with this logger. + * @var array the log targets. Each array element represents a single [[Target|log target]] instance + * or the configuration for creating the log target instance. */ - public $router; + public $targets = array(); /** diff --git a/framework/yii/logging/Router.php b/framework/yii/log/Router.php similarity index 93% rename from framework/yii/logging/Router.php rename to framework/yii/log/Router.php index eae6de6..c7d9ac3 100644 --- a/framework/yii/logging/Router.php +++ b/framework/yii/log/Router.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\logging; +namespace yii\log; use Yii; use yii\base\Component; @@ -27,15 +27,15 @@ use yii\base\Component; * 'preload' => array('log'), * 'components' => array( * 'log' => array( - * 'class' => 'yii\logging\Router', + * 'class' => 'yii\log\Router', * 'targets' => array( * 'file' => array( - * 'class' => 'yii\logging\FileTarget', + * 'class' => 'yii\log\FileTarget', * 'levels' => array('trace', 'info'), * 'categories' => array('yii\*'), * ), * 'email' => array( - * 'class' => 'yii\logging\EmailTarget', + * 'class' => 'yii\log\EmailTarget', * 'levels' => array('error', 'warning'), * 'emails' => array('admin@example.com'), * ), diff --git a/framework/yii/logging/Target.php b/framework/yii/log/Target.php similarity index 99% rename from framework/yii/logging/Target.php rename to framework/yii/log/Target.php index 7be7001..a3276fc 100644 --- a/framework/yii/logging/Target.php +++ b/framework/yii/log/Target.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\logging; +namespace yii\log; use Yii; use yii\base\Component;