You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
318 lines
9.8 KiB
318 lines
9.8 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
14 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\log;
|
||
12 years ago
|
|
||
|
use \yii\base\Component;
|
||
|
use \yii\base\InvalidConfigException;
|
||
12 years ago
|
|
||
14 years ago
|
/**
|
||
12 years ago
|
* 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;
|
||
|
* ~~~
|
||
14 years ago
|
*
|
||
12 years ago
|
* 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.
|
||
14 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class Logger extends Component
|
||
14 years ago
|
{
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Error message level. An error message is one that indicates the abnormal termination of the
|
||
|
* application and may require developer's handling.
|
||
|
*/
|
||
|
const LEVEL_ERROR = 0x01;
|
||
|
/**
|
||
|
* Warning message level. A warning message is one that indicates some abnormal happens but
|
||
|
* the application is able to continue to run. Developers should pay attention to this message.
|
||
|
*/
|
||
|
const LEVEL_WARNING = 0x02;
|
||
|
/**
|
||
|
* Informational message level. An informational message is one that includes certain information
|
||
|
* for developers to review.
|
||
|
*/
|
||
|
const LEVEL_INFO = 0x04;
|
||
|
/**
|
||
|
* Tracing message level. An tracing message is one that reveals the code execution flow.
|
||
|
*/
|
||
|
const LEVEL_TRACE = 0x08;
|
||
|
/**
|
||
|
* Profiling message level. This indicates the message is for profiling purpose.
|
||
|
*/
|
||
|
const LEVEL_PROFILE = 0x40;
|
||
|
/**
|
||
|
* Profiling message level. This indicates the message is for profiling purpose. It marks the
|
||
|
* beginning of a profiling block.
|
||
|
*/
|
||
|
const LEVEL_PROFILE_BEGIN = 0x50;
|
||
|
/**
|
||
|
* Profiling message level. This indicates the message is for profiling purpose. It marks the
|
||
|
* end of a profiling block.
|
||
|
*/
|
||
|
const LEVEL_PROFILE_END = 0x60;
|
||
|
|
||
14 years ago
|
|
||
|
/**
|
||
|
* @var integer how many messages should be logged before they are flushed from memory and sent to targets.
|
||
|
* Defaults to 1000, meaning the [[flush]] method will be invoked once every 1000 messages logged.
|
||
|
* Set this property to be 0 if you don't want to flush messages until the application terminates.
|
||
|
* This property mainly affects how much memory will be taken by the logged messages.
|
||
13 years ago
|
* A smaller value means less memory, but will increase the execution time due to the overhead of [[flush()]].
|
||
14 years ago
|
*/
|
||
|
public $flushInterval = 1000;
|
||
|
/**
|
||
12 years ago
|
* @var array logged messages. This property is managed by [[log()]] and [[flush()]].
|
||
14 years ago
|
* Each log message is of the following structure:
|
||
|
*
|
||
|
* ~~~
|
||
|
* array(
|
||
12 years ago
|
* [0] => message (mixed, can be a string or some complex data, such as an exception object)
|
||
12 years ago
|
* [1] => level (integer)
|
||
14 years ago
|
* [2] => category (string)
|
||
|
* [3] => timestamp (float, obtained by microtime(true))
|
||
|
* )
|
||
|
* ~~~
|
||
14 years ago
|
*/
|
||
|
public $messages = array();
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var array the log targets. Each array element represents a single [[Target|log target]] instance
|
||
|
* or the configuration for creating the log target instance.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $targets = array();
|
||
12 years ago
|
|
||
14 years ago
|
|
||
|
/**
|
||
12 years ago
|
* @var string
|
||
|
*/
|
||
|
private $_tag;
|
||
|
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Initializes the logger by registering [[flush()]] as a shutdown function.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
register_shutdown_function(array($this, 'flush'), true);
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Logs a message with the given type and category.
|
||
|
* If `YII_DEBUG` is true and `YII_TRACE_LEVEL` is greater than 0, then additional
|
||
|
* call stack information about application code will be appended to the message.
|
||
|
* @param string $message the message to be logged.
|
||
12 years ago
|
* @param integer $level the level of the message. This must be one of the following:
|
||
|
* `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`,
|
||
|
* `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`.
|
||
14 years ago
|
* @param string $category the category of the message.
|
||
|
*/
|
||
13 years ago
|
public function log($message, $level, $category = 'application')
|
||
14 years ago
|
{
|
||
13 years ago
|
$time = microtime(true);
|
||
|
if (YII_DEBUG && YII_TRACE_LEVEL > 0) {
|
||
14 years ago
|
$traces = debug_backtrace();
|
||
|
$count = 0;
|
||
|
foreach ($traces as $trace) {
|
||
13 years ago
|
if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII_PATH) !== 0) {
|
||
13 years ago
|
$message .= "\nin {$trace['file']} ({$trace['line']})";
|
||
14 years ago
|
if (++$count >= YII_TRACE_LEVEL) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
13 years ago
|
$this->messages[] = array($message, $level, $category, $time);
|
||
12 years ago
|
if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) {
|
||
13 years ago
|
$this->flush();
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Flushes log messages from memory to targets.
|
||
12 years ago
|
* This method will trigger an [[EVENT_FLUSH]] or [[EVENT_FINAL_FLUSH]] event depending on the $final value.
|
||
12 years ago
|
* @param boolean $final whether this is a final call during a request.
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function flush($final = false)
|
||
14 years ago
|
{
|
||
12 years ago
|
if ($this->router) {
|
||
|
$this->router->dispatch($this->messages, $final);
|
||
|
}
|
||
14 years ago
|
$this->messages = array();
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* @return string a tag that uniquely identifies the current request.
|
||
|
*/
|
||
|
public function getTag()
|
||
|
{
|
||
|
if ($this->_tag === null) {
|
||
|
$this->_tag = date('Ymd-His', microtime(true));
|
||
|
}
|
||
|
return $this->_tag;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $tag a tag that uniquely identifies the current request.
|
||
|
*/
|
||
|
public function setTag($tag)
|
||
|
{
|
||
|
$this->_tag = $tag;
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Returns the total elapsed time since the start of the current request.
|
||
|
* This method calculates the difference between now and the timestamp
|
||
|
* defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning
|
||
|
* of [[YiiBase]] class file.
|
||
|
* @return float the total elapsed time in seconds for current request.
|
||
|
*/
|
||
12 years ago
|
public function getElapsedTime()
|
||
14 years ago
|
{
|
||
|
return microtime(true) - YII_BEGIN_TIME;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the profiling results.
|
||
14 years ago
|
*
|
||
|
* By default, all profiling results will be returned. You may provide
|
||
|
* `$categories` and `$excludeCategories` as parameters to retrieve the
|
||
|
* results that you are interested in.
|
||
|
*
|
||
|
* @param array $categories list of categories that you are interested in.
|
||
|
* You can use an asterisk at the end of a category to do a prefix match.
|
||
|
* For example, 'yii\db\*' will match categories starting with 'yii\db\',
|
||
12 years ago
|
* such as 'yii\db\Connection'.
|
||
12 years ago
|
* @param array $excludeCategories list of categories that you want to exclude
|
||
14 years ago
|
* @return array the profiling results. Each array element has the following structure:
|
||
13 years ago
|
* `array($token, $category, $time)`.
|
||
14 years ago
|
*/
|
||
14 years ago
|
public function getProfiling($categories = array(), $excludeCategories = array())
|
||
14 years ago
|
{
|
||
14 years ago
|
$timings = $this->calculateTimings();
|
||
|
if (empty($categories) && empty($excludeCategories)) {
|
||
|
return $timings;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
foreach ($timings as $i => $timing) {
|
||
|
$matched = empty($categories);
|
||
|
foreach ($categories as $category) {
|
||
|
$prefix = rtrim($category, '*');
|
||
13 years ago
|
if (strpos($timing[1], $prefix) === 0 && ($timing[1] === $category || $prefix !== $category)) {
|
||
14 years ago
|
$matched = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($matched) {
|
||
|
foreach ($excludeCategories as $category) {
|
||
|
$prefix = rtrim($category, '*');
|
||
|
foreach ($timings as $i => $timing) {
|
||
13 years ago
|
if (strpos($timing[1], $prefix) === 0 && ($timing[1] === $category || $prefix !== $category)) {
|
||
14 years ago
|
$matched = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!$matched) {
|
||
|
unset($timings[$i]);
|
||
14 years ago
|
}
|
||
|
}
|
||
14 years ago
|
return array_values($timings);
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Returns the statistical results of DB queries.
|
||
|
* The results returned include the number of SQL statements executed and
|
||
|
* the total time spent.
|
||
|
* @return array the first element indicates the number of SQL statements executed,
|
||
|
* and the second element the total time spent in SQL execution.
|
||
|
*/
|
||
|
public function getDbProfiling()
|
||
|
{
|
||
|
$timings = $this->getProfiling(array('yii\db\Command::query', 'yii\db\Command::execute'));
|
||
|
$count = count($timings);
|
||
|
$time = 0;
|
||
|
foreach ($timings as $timing) {
|
||
|
$time += $timing[1];
|
||
|
}
|
||
|
return array($count, $time);
|
||
|
}
|
||
|
|
||
14 years ago
|
private function calculateTimings()
|
||
|
{
|
||
14 years ago
|
$timings = array();
|
||
14 years ago
|
|
||
|
$stack = array();
|
||
|
foreach ($this->messages as $log) {
|
||
12 years ago
|
list($token, $level, $category, $timestamp) = $log;
|
||
|
if ($level == self::LEVEL_PROFILE_BEGIN) {
|
||
14 years ago
|
$stack[] = $log;
|
||
12 years ago
|
} elseif ($level == self::LEVEL_PROFILE_END) {
|
||
13 years ago
|
if (($last = array_pop($stack)) !== null && $last[0] === $token) {
|
||
|
$timings[] = array($token, $category, $timestamp - $last[3]);
|
||
|
} else {
|
||
12 years ago
|
throw new InvalidConfigException("Unmatched profiling block: $token");
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$now = microtime(true);
|
||
|
while (($last = array_pop($stack)) !== null) {
|
||
|
$delta = $now - $last[3];
|
||
13 years ago
|
$timings[] = array($last[0], $last[2], $delta);
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
return $timings;
|
||
14 years ago
|
}
|
||
|
}
|