Abstract Class yii\log\Target

继承yii\log\Target » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
子类yii\log\DbTarget, yii\log\EmailTarget, yii\log\FileTarget, yii\log\SyslogTarget
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/log/Target.php

Target is the base class for all log target classes.

A log target object will filter the messages logged by yii\log\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.

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.

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. yii\base\Component
$categories array List of message categories that this target is interested in. yii\log\Target
$enabled boolean Indicates whether this log target is enabled. Defaults to true. Note that the type of this property differs in getter and setter. See getEnabled() and setEnabled() for details. yii\log\Target
$except array List of message categories that this target is NOT interested in. yii\log\Target
$exportInterval integer How many messages should be accumulated before they are exported. yii\log\Target
$levels integer The message levels that this target is interested in. This is a bitmap of level values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter and setter. See getLevels() and setLevels() for details. For more details and usage information on Target, see the guide article on logging & targets. yii\log\Target
$logVars array List of the PHP predefined variables that should be logged in a message. yii\log\Target
$maskVars array List of the PHP predefined variables that should NOT be logged "as is" and should always be replaced with a mask *** before logging, when exist. yii\log\Target
$messages array The messages that are retrieved from the logger so far by this log target. yii\log\Target
$microtime boolean Whether to log time with microseconds. yii\log\Target
$prefix callable A PHP callable that returns a string to be prefixed to every exported message. yii\log\Target

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\BaseObject
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
collect() Processes the given log messages. yii\log\Target
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
export() Exports log $messages to a specific destination. yii\log\Target
filterMessages() Filters the given messages according to their categories and levels. yii\log\Target
formatMessage() Formats a log message for display as a string. yii\log\Target
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getEnabled() Check whether the log target is enabled. yii\log\Target
getLevels() yii\log\Target
getMessagePrefix() Returns a string to be prefixed to the given message. yii\log\Target
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() 初始化对象. yii\base\BaseObject
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
setEnabled() Sets a value indicating whether this log target is enabled. yii\log\Target
setLevels() Sets the message levels that this target is interested in. yii\log\Target
trigger() 触发事件. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述定义在
getContextMessage() Generates the context information to be logged. yii\log\Target
getTime() Returns formatted ('Y-m-d H:i:s') timestamp for message. yii\log\Target

属性详情

$categories public 属性

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'.

public array $categories = []
$enabled public 属性

Indicates whether this log target is enabled. Defaults to true. Note that the type of this property differs in getter and setter. See getEnabled() and setEnabled() for details.

public boolean $enabled null
$except public 属性

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'.

参见 $categories.

public array $except = []
$exportInterval public 属性

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.

public integer $exportInterval 1000
$levels public 属性

The message levels that this target is interested in. This is a bitmap of level values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter and setter. See getLevels() and setLevels() for details.

For more details and usage information on Target, see the guide article on logging & targets.

public integer $levels null
$logVars public 属性

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 ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER'].

Since version 2.0.9 additional syntax can be used: Each element could be specified as one of the following:

  • var - var will be logged.
  • var.key - only var[key] key will be logged.
  • !var.key - var[key] key will be excluded.

Note that if you need $_SESSION to logged regardless if session was used you have to open it right at the start of your request.

参见 yii\helpers\ArrayHelper::filter().

public array $logVars = ['_GET''_POST''_FILES''_COOKIE''_SESSION''_SERVER']
$maskVars public 属性 (可用自版本: 2.0.16)

List of the PHP predefined variables that should NOT be logged "as is" and should always be replaced with a mask *** before logging, when exist.

Defaults to [ '_SERVER.HTTP_AUTHORIZATION', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']

Each element could be specified as one of the following:

  • var - var will be logged as ***
  • var.key - only var[key] will be logged as ***
public array $maskVars = ['_SERVER.HTTP_AUTHORIZATION''_SERVER.PHP_AUTH_USER''_SERVER.PHP_AUTH_PW']
$messages public 属性

The messages that are retrieved from the logger so far by this log target. Please refer to yii\log\Logger::$messages for the details about the message structure.

public array $messages = []
$microtime public 属性 (可用自版本: 2.0.13)

Whether to log time with microseconds. Defaults to false.

public boolean $microtime false
$prefix public 属性

A PHP callable that returns a string to be prefixed to every exported message.

If not set, getMessagePrefix() will be used, which prefixes the message with context information such as user IP, user ID and session ID.

The signature of the callable should be function ($message).

public callable $prefix null

方法详情

collect() public 方法

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).

public void collect ( $messages, $final )
$messages array

Log messages to be processed. See yii\log\Logger::$messages for the structure of each message.

$final boolean

Whether this method is called at the end of the current application

export() public abstract 方法

Exports log $messages to a specific destination.

Child classes must implement this method.

public abstract void export ( )
filterMessages() public static 方法

Filters the given messages according to their categories and levels.

public static array filterMessages ( $messages, $levels 0, $categories = [], $except = [] )
$messages array

Messages to be filtered. The message structure follows that in yii\log\Logger::$messages.

$levels integer

The message levels to filter by. This is a bitmap of level values. Value 0 means allowing all levels.

$categories array

The message categories to filter by. If empty, it means all categories are allowed.

$except array

The message categories to exclude. If empty, it means all categories are allowed.

return array

The filtered messages.

formatMessage() public 方法

Formats a log message for display as a string.

public string formatMessage ( $message )
$message array

The log message to be formatted. The message structure follows that in yii\log\Logger::$messages.

return string

The formatted message

getContextMessage() protected 方法

Generates the context information to be logged.

The default implementation will dump user information, system variables, etc.

protected string getContextMessage ( )
return string

The context information. If an empty string, it means no context information.

getEnabled() public 方法

Check whether the log target is enabled.

public boolean getEnabled ( )
return boolean

A value indicating whether this log target is enabled.

getLevels() public 方法

public integer getLevels ( )
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.

getMessagePrefix() public 方法

Returns a string to be prefixed to the given message.

If $prefix is configured it will return the result of the callback. The default implementation will return user IP, user ID and session ID as a prefix.

public string getMessagePrefix ( $message )
$message array

The message being exported. The message structure follows that in yii\log\Logger::$messages.

return string

The prefix string

getTime() protected 方法 (可用自版本: 2.0.13)

Returns formatted ('Y-m-d H:i:s') timestamp for message.

If $microtime is configured to true it will return format 'Y-m-d H:i:s.u'.

protected string getTime ( $timestamp )
$timestamp float
setEnabled() public 方法

Sets a value indicating whether this log target is enabled.

public void setEnabled ( $value )
$value boolean|callable

A boolean value or a callable to obtain the value from. The callable value is available since version 2.0.13.

A callable may be used to determine whether the log target should be enabled in a dynamic way. For example, to only enable a log if the current user is logged in you can configure the target as follows:

'enabled' => function() {
    return !Yii::$app->user->isGuest;
}
setLevels() public 方法

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: yii\log\Logger::LEVEL_ERROR, yii\log\Logger::LEVEL_WARNING, yii\log\Logger::LEVEL_INFO, yii\log\Logger::LEVEL_TRACE and yii\log\Logger::LEVEL_PROFILE.

For example,

['error', 'warning']
// which is equivalent to:
Logger::LEVEL_ERROR | Logger::LEVEL_WARNING
public void setLevels ( $levels )
$levels array|integer

Message levels that this target is interested in.

throws yii\base\InvalidConfigException

if $levels value is not correct.