Class yii\log\Dispatcher

继承yii\log\Dispatcher » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/log/Dispatcher.php

Dispatcher manages a set of log targets.

Dispatcher implements the dispatch()-method that forwards the log messages from a yii\log\Logger to the registered log $targets.

An instance of Dispatcher is registered as a core application component and can be accessed using Yii::$app->log.

You may configure the targets in application configuration, like the following:

[
    'components' => [
        'log' => [
            'targets' => [
                'file' => [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['trace', 'info'],
                    'categories' => ['yii\*'],
                ],
                'email' => [
                    'class' => 'yii\log\EmailTarget',
                    'levels' => ['error', 'warning'],
                    'message' => [
                        'to' => '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;

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. yii\base\Component
$flushInterval integer How many messages should be logged before they are sent to targets. This method returns the value of yii\log\Logger::$flushInterval. yii\log\Dispatcher
$logger yii\log\Logger The logger. If not set, Yii::getLogger() will be used. Note that the type of this property differs in getter and setter. See getLogger() and setLogger() for details. yii\log\Dispatcher
$targets array|yii\log\Target[] The log targets. yii\log\Dispatcher
$traceLevel integer How many application call stacks should be logged together with each message. This method returns the value of yii\log\Logger::$traceLevel. Defaults to 0. yii\log\Dispatcher

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\log\Dispatcher
__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
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
dispatch() Dispatches the logged messages to $targets. yii\log\Dispatcher
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getFlushInterval() yii\log\Dispatcher
getLogger() Gets the connected logger. yii\log\Dispatcher
getTraceLevel() yii\log\Dispatcher
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() 初始化对象. yii\log\Dispatcher
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
setFlushInterval() yii\log\Dispatcher
setLogger() Sets the connected logger. yii\log\Dispatcher
setTraceLevel() yii\log\Dispatcher
trigger() 触发事件. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述定义在
generateTargetFailErrorMessage() Generate target error message yii\log\Dispatcher

属性详情

$flushInterval public 属性

How many messages should be logged before they are sent to targets. This method returns the value of yii\log\Logger::$flushInterval.

public integer $flushInterval null
$logger public 属性

The logger. If not set, Yii::getLogger() will be used. Note that the type of this property differs in getter and setter. See getLogger() and setLogger() for details.

public yii\log\Logger $logger null
$targets public 属性

The log targets. Each array element represents a single log target instance or the configuration for creating the log target instance.

$traceLevel public 属性

How many application call stacks should be logged together with each message. This method returns the value of yii\log\Logger::$traceLevel. Defaults to 0.

public integer $traceLevel null

方法详情

__construct() public 方法

构造函数.

默认实现有两件事:

  • 使用给定的配置$config初始化对象.
  • 调用 init().

如果在子类中重写此方法, 则建议使用这种方法:

  • 构造函数的最后一个参数是一个配置数组, 如$config.
  • 在构造函数的末尾调用父实现.
public void __construct ( $config = [] )
$config array

将用于初始化对象属性的键值对(name-value).

dispatch() public 方法

Dispatches the logged messages to $targets.

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

The logged messages

$final boolean

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

generateTargetFailErrorMessage() protected 方法 (可用自版本: 2.0.32)

Generate target error message

protected array generateTargetFailErrorMessage ( $target, $throwable, $method )
$target yii\log\Target

Log target object

$throwable \Throwable|Exception

Catched exception

$method string

Full method path

return array

Generated error message data

getFlushInterval() public 方法

public integer getFlushInterval ( )
return integer

How many messages should be logged before they are sent to targets. This method returns the value of yii\log\Logger::$flushInterval.

getLogger() public 方法

Gets the connected logger.

If not set, Yii::getLogger() will be used.

public yii\log\Logger getLogger ( )
return yii\log\Logger

The logger.

getTraceLevel() public 方法

public integer getTraceLevel ( )
return integer

How many application call stacks should be logged together with each message. This method returns the value of yii\log\Logger::$traceLevel. Defaults to 0.

init() public 方法

初始化对象.

在使用给定的配置初始化对象之后, 在构造函数的末尾调用此方法.

public void init ( )
setFlushInterval() public 方法

public void setFlushInterval ( $value )
$value integer

How many messages should be logged before they are sent to targets. This method will set the value of yii\log\Logger::$flushInterval. Defaults to 1000, meaning the yii\log\Logger::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. A smaller value means less memory, but will increase the execution time due to the overhead of yii\log\Logger::flush().

setLogger() public 方法

Sets the connected logger.

public void setLogger ( $value )
$value yii\log\Logger|string|array

The logger to be used. This can either be a logger instance or a configuration that will be used to create one using Yii::createObject().

setTraceLevel() public 方法

public void setTraceLevel ( $value )
$value integer

How many application call stacks should be logged together with each message. This method will set the value of yii\log\Logger::$traceLevel. If the value is greater than 0, at most that number of call stacks will be logged. Note that only application call stacks are counted. Defaults to 0.