Class yii\log\Dispatcher

Inheritanceyii\log\Dispatcher » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code 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;

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component. This property is read-only. 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

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\log\Dispatcher
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
dispatch() Dispatches the logged messages to $targets. yii\log\Dispatcher
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getFlushInterval() yii\log\Dispatcher
getLogger() Gets the connected logger. yii\log\Dispatcher
getTraceLevel() yii\log\Dispatcher
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\log\Dispatcher
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
setFlushInterval() yii\log\Dispatcher
setLogger() Sets the connected logger. yii\log\Dispatcher
setTraceLevel() yii\log\Dispatcher
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
generateTargetFailErrorMessage() Generate target error message yii\log\Dispatcher

Property Details

$flushInterval public property

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 property

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 property

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

$traceLevel public property

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

Method Details

__construct() public method

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

dispatch() public method

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 method (available since version 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 method

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 method

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 method

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 method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )
setFlushInterval() public method

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 method

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 method

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.