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.

237 lines
8.2 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\log;
13 years ago
12 years ago
use Yii;
12 years ago
use yii\base\Component;
use yii\base\InvalidConfigException;
13 years ago
/**
* Target is the base class for all log target classes.
*
13 years ago
* A log target object will filter the messages logged by [[Logger]] according
* to its [[levels]] and [[categories]] properties. It may also export the filtered
* messages to specific destination defined by the target, such as emails, files.
13 years ago
*
13 years ago
* Level filter and category filter are combinatorial, i.e., only messages
* satisfying both filter conditions will be handled. Additionally, you
* may specify [[except]] to exclude messages of certain categories.
13 years ago
*
* @property integer $levels the message levels that this target is interested in.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
12 years ago
abstract class Target extends Component
13 years ago
{
/**
* @var boolean whether to enable this log target. Defaults to true.
*/
public $enabled = true;
/**
13 years ago
* @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories.
* You can use an asterisk at the end of a category so that the category may be used to
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match
* categories starting with 'yii\db\', such as 'yii\db\Connection'.
13 years ago
*/
public $categories = array();
/**
* @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages.
* If this property is not empty, then any category listed here will be excluded from [[categories]].
* You can use an asterisk at the end of a category so that the category can be used to
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match
* categories starting with 'yii\db\', such as 'yii\db\Connection'.
13 years ago
* @see categories
*/
13 years ago
public $except = array();
13 years ago
/**
13 years ago
* @var boolean whether to log a message containing the current user name and ID. Defaults to false.
13 years ago
* @see \yii\web\User
13 years ago
*/
13 years ago
public $logUser = false;
13 years ago
/**
13 years ago
* @var array list of the PHP predefined variables that should be logged in a message.
* Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged.
* Defaults to `array('_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER')`.
13 years ago
*/
13 years ago
public $logVars = array('_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER');
13 years ago
/**
* @var integer how many messages should be accumulated before they are exported.
* Defaults to 1000. Note that messages will always be exported when the application terminates.
* Set this property to be 0 if you don't want to export messages until the application terminates.
13 years ago
*/
public $exportInterval = 1000;
13 years ago
/**
13 years ago
* @var array the messages that are retrieved from the logger so far by this log target.
12 years ago
* Please refer to [[Logger::messages]] for the details about the message structure.
13 years ago
*/
12 years ago
public $messages = array();
13 years ago
private $_levels = 0;
13 years ago
/**
12 years ago
* Exports log [[messages]] to a specific destination.
* Child classes must implement this method.
13 years ago
*/
12 years ago
abstract public function export();
13 years ago
/**
13 years ago
* Processes the given log messages.
* This method will filter the given messages with [[levels]] and [[categories]].
* And if requested, it will also export the filtering result to specific medium (e.g. email).
* @param array $messages log messages to be processed. See [[Logger::messages]] for the structure
* of each message.
* @param boolean $final whether this method is called at the end of the current application
13 years ago
*/
public function collect($messages, $final)
13 years ago
{
11 years ago
$this->messages = array_merge($this->messages, $this->filterMessages($messages, $this->getLevels(), $this->categories, $this->except));
12 years ago
$count = count($this->messages);
if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {
if (($context = $this->getContextMessage()) !== '') {
12 years ago
$this->messages[] = array($context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME);
13 years ago
}
12 years ago
$this->export();
12 years ago
$this->messages = array();
13 years ago
}
}
13 years ago
/**
* Generates the context information to be logged.
* The default implementation will dump user information, system variables, etc.
* @return string the context information. If an empty string, it means no context information.
*/
protected function getContextMessage()
13 years ago
{
13 years ago
$context = array();
12 years ago
if ($this->logUser && ($user = Yii::$app->getComponent('user', false)) !== null) {
/** @var $user \yii\web\User */
$context[] = 'User: ' . $user->getId();
13 years ago
}
foreach ($this->logVars as $name) {
if (!empty($GLOBALS[$name])) {
$context[] = "\${$name} = " . var_export($GLOBALS[$name], true);
}
13 years ago
}
13 years ago
return implode("\n\n", $context);
13 years ago
}
/**
* @return integer the message levels that this target is interested in. This is a bitmap of
* level values. Defaults to 0, meaning all available levels.
*/
public function getLevels()
{
return $this->_levels;
}
/**
* Sets the message levels that this target is interested in.
*
* The parameter can be either an array of interested level names or an integer representing
* the bitmap of the interested level values. Valid level names include: 'error',
* 'warning', 'info', 'trace' and 'profile'; valid level values include:
* [[Logger::LEVEL_ERROR]], [[Logger::LEVEL_WARNING]], [[Logger::LEVEL_INFO]],
* [[Logger::LEVEL_TRACE]] and [[Logger::LEVEL_PROFILE]].
*
* For example,
*
* ~~~
* array('error', 'warning')
* // which is equivalent to:
* Logger::LEVEL_ERROR | Logger::LEVEL_WARNING
* ~~~
*
* @param array|integer $levels message levels that this target is interested in.
* @throws InvalidConfigException if an unknown level name is given
*/
public function setLevels($levels)
{
static $levelMap = array(
'error' => Logger::LEVEL_ERROR,
'warning' => Logger::LEVEL_WARNING,
'info' => Logger::LEVEL_INFO,
'trace' => Logger::LEVEL_TRACE,
'profile' => Logger::LEVEL_PROFILE,
);
if (is_array($levels)) {
$this->_levels = 0;
foreach ($levels as $level) {
if (isset($levelMap[$level])) {
$this->_levels |= $levelMap[$level];
} else {
throw new InvalidConfigException("Unrecognized level: $level");
}
}
} else {
$this->_levels = $levels;
}
}
/**
13 years ago
* Filters the given messages according to their categories and levels.
* @param array $messages messages to be filtered
11 years ago
* @param integer $levels the message levels to filter by. This is a bitmap of
* level values. Value 0 means allowing all levels.
* @param array $categories the message categories to filter by. If empty, it means all categories are allowed.
* @param array $except the message categories to exclude. If empty, it means all categories are allowed.
13 years ago
* @return array the filtered messages.
13 years ago
*/
public static function filterMessages($messages, $levels = 0, $categories = array(), $except = array())
13 years ago
{
13 years ago
foreach ($messages as $i => $message) {
if ($levels && !($levels & $message[1])) {
13 years ago
unset($messages[$i]);
continue;
}
11 years ago
$matched = empty($categories);
foreach ($categories as $category) {
if ($message[2] === $category || substr($category, -1) === '*' && strpos($message[2], rtrim($category, '*')) === 0) {
13 years ago
$matched = true;
break;
}
}
if ($matched) {
11 years ago
foreach ($except as $category) {
13 years ago
$prefix = rtrim($category, '*');
12 years ago
if (strpos($message[2], $prefix) === 0 && ($message[2] === $category || $prefix !== $category)) {
$matched = false;
break;
13 years ago
}
}
}
if (!$matched) {
unset($messages[$i]);
}
13 years ago
}
13 years ago
return $messages;
13 years ago
}
/**
13 years ago
* Formats a log message.
* The message structure follows that in [[Logger::messages]].
* @param array $message the log message to be formatted.
* @return string the formatted message
13 years ago
*/
13 years ago
public function formatMessage($message)
13 years ago
{
list($text, $level, $category, $timestamp) = $message;
12 years ago
$level = Logger::getLevelName($level);
if (!is_string($text)) {
$text = var_export($text, true);
}
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
return date('Y/m/d H:i:s', $timestamp) . " [$ip] [$level] [$category] $text\n";
13 years ago
}
}