Skip to content
PHP Ecosystem
Explore

Dispatcher

API reference › yii › log › Dispatcher

classyii\log\Dispatcher
Extendsyii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/log/Dispatcher.php

Dispatcher manages a set of log targets.

Dispatcher implements the dispatch()-method that forwards the log messages from a 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;
PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$flushIntervalintHow many messages should be logged before they are sent to targets.Dispatcher
$loggerLogger|string|arrayThe logger to be used.Dispatcher
$targetsarrayThe log targets. Default: [].Dispatcher
$traceLevelintHow many application call stacks should be logged together with each message.Dispatcher
MethodDescriptionDefined by
__construct()Constructor.Dispatcher
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
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\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns 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.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()Dispatcher
getLogger()Gets the connected logger.Dispatcher
getTraceLevel()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\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.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()Dispatcher
setLogger()Sets the connected logger.Dispatcher
setTraceLevel()Dispatcher
trigger()Triggers an event.yii\base\Component
generateTargetFailErrorMessage() protectedGenerate target error messageDispatcher
public int $flushInterval

How many messages should be logged before they are sent to targets.

This method returns the value of Logger::$flushInterval.

public yii\log\Logger|string|array $logger

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(). If you are providing custom logger configuration and would like it to be used for the whole application and not just for the dispatcher you should use Yii::setLogger() instead.

public array $targets = []

The log targets.

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

public int $traceLevel

How many application call stacks should be logged together with each message.

This method returns the value of Logger::$traceLevel. Defaults to 0.

public function __construct(mixed $config = [])

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.

Parameters

ParameterTypeDescription
$configmixedName-value pairs that will be used to initialize the object properties
public function dispatch(array<int|string, yii\log\LogMessage> $messages, bool $final): mixed

Dispatches the logged messages to $targets.

Parameters

ParameterTypeDescription
$messagesarray<int|string, yii\log\LogMessage>The logged messages
$finalboolWhether this method is called at the end of the current application

Returns

mixed

public function getFlushInterval(): int

Returns

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

public function getLogger(): yii\log\Logger

Gets the connected logger.

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

Returns

Logger: The logger.

public function getTraceLevel(): int

Returns

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

public function init(): mixed

Initializes the object.

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

Returns

mixed

public function setFlushInterval(int $value): mixed

Parameters

ParameterTypeDescription
$valueintHow many messages should be logged before they are sent to targets. This method will set the value of Logger::$flushInterval. Defaults to 1000, meaning the 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 Logger::flush().

Returns

mixed

public function setLogger(yii\log\Logger|string|array $value): mixed

Sets the connected logger.

Parameters

ParameterTypeDescription
$valueLogger|string|arrayThe logger to be used. This can either be a logger instance or a configuration that will be used to create one using Yii::createObject(). If you are providing custom logger configuration and would like it to be used for the whole application and not just for the dispatcher you should use Yii::setLogger() instead.

Returns

mixed

public function setTraceLevel(int $value): mixed

Parameters

ParameterTypeDescription
$valueintHow many application call stacks should be logged together with each message. This method will set the value of 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.

Returns

mixed

protected function generateTargetFailErrorMessage(yii\log\Target $target, Throwable $throwable, string $method): yii\log\LogMessage

Generate target error message

Parameters

ParameterTypeDescription
$targetTargetLog target object
$throwableThrowableCatched exception
$methodstringFull method path

Returns

yii\log\LogMessage: Generated error message data

Since

2.0.32