# Dispatcher

> Dispatcher manages a set of log targets (yii\log\Target).

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [log](/docs/api/yii/log/index.md) › Dispatcher

| class | `yii\log\Dispatcher` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/log/Dispatcher.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/log/Dispatcher.php#L69) |

Dispatcher manages a set of [log targets](/docs/api/yii/log/target/index.md).

Dispatcher implements the [`dispatch()`](/docs/api/yii/log/dispatcher/index.md#method-dispatch)-method that forwards the log messages from a [`Logger`](/docs/api/yii/log/logger/index.md) to
the registered log [`$targets`](/docs/api/yii/log/dispatcher/index.md#property-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`](/docs/api/yii/log/dispatcher/index.md#property-targets) property as follows:

```
Yii::$app->log->targets['file']->enabled = false;
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$flushInterval`](#property-flushInterval) | `int` | How many messages should be logged before they are sent to targets. | `Dispatcher` |
| [`$logger`](#property-logger) | [`Logger`](/docs/api/yii/log/logger/index.md)`\|string\|array` | The logger to be used. | `Dispatcher` |
| [`$targets`](#property-targets) | `array` | The log targets. Default: `[]`. | `Dispatcher` |
| [`$traceLevel`](#property-traceLevel) | `int` | How many application call stacks should be logged together with each message. | `Dispatcher` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `Dispatcher` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`dispatch()`](#method-dispatch) | Dispatches the logged messages to [`$targets`](/docs/api/yii/log/dispatcher/index.md#property-targets). | `Dispatcher` |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getFlushInterval()`](#method-getFlushInterval) |  | `Dispatcher` |
| [`getLogger()`](#method-getLogger) | Gets the connected logger. | `Dispatcher` |
| [`getTraceLevel()`](#method-getTraceLevel) |  | `Dispatcher` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `Dispatcher` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`setFlushInterval()`](#method-setFlushInterval) |  | `Dispatcher` |
| [`setLogger()`](#method-setLogger) | Sets the connected logger. | `Dispatcher` |
| [`setTraceLevel()`](#method-setTraceLevel) |  | `Dispatcher` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`generateTargetFailErrorMessage()`](#method-generateTargetFailErrorMessage) protected | Generate target error message | `Dispatcher` |

## Property details

### $flushInterval <span id="property-flushInterval"></span>

```php
public int $flushInterval
```

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

This
method returns the value of [`Logger::$flushInterval`](/docs/api/yii/log/logger/index.md#property-flushInterval).

### $logger <span id="property-logger"></span>

```php
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()`](/docs/api/yii/baseyii/index.md#method-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()`](/docs/api/yii/baseyii/index.md#method-setLogger) instead.

### $targets <span id="property-targets"></span>

```php
public array $targets = []
```

The log targets.

Each array element represents a single [log target](/docs/api/yii/log/target/index.md) instance
or the configuration for creating the log target instance.

### $traceLevel <span id="property-traceLevel"></span>

```php
public int $traceLevel
```

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

This method returns the value of [`Logger::$traceLevel`](/docs/api/yii/log/logger/index.md#property-traceLevel). Defaults to 0.

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(mixed $config = [])
```

Constructor.

The default implementation does two things:

- Initializes the object with the given configuration `$config`.
- Call [`init()`](/docs/api/yii/log/dispatcher/index.md#method-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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$config` | `mixed` | Name-value pairs that will be used to initialize the object properties |

### dispatch() <span id="method-dispatch"></span>

```php
public function dispatch(array<int|string, yii\log\LogMessage> $messages, bool $final): mixed
```

Dispatches the logged messages to [`$targets`](/docs/api/yii/log/dispatcher/index.md#property-targets).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$messages` | `array<int\|string, yii\log\LogMessage>` | The logged messages |
| `$final` | `bool` | Whether this method is called at the end of the current application |

**Returns**

`mixed`

### getFlushInterval() <span id="method-getFlushInterval"></span>

```php
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`](/docs/api/yii/log/logger/index.md#property-flushInterval).

### getLogger() <span id="method-getLogger"></span>

```php
public function getLogger(): yii\log\Logger
```

Gets the connected logger.

If not set, [`Yii::getLogger()`](/docs/api/yii/baseyii/index.md#method-getLogger) will be used.

**Returns**

[`Logger`](/docs/api/yii/log/logger/index.md): The logger.

### getTraceLevel() <span id="method-getTraceLevel"></span>

```php
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`](/docs/api/yii/log/logger/index.md#property-traceLevel). Defaults to 0.

### init() <span id="method-init"></span>

```php
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`

### setFlushInterval() <span id="method-setFlushInterval"></span>

```php
public function setFlushInterval(int $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `int` | How many messages should be logged before they are sent to targets. This method will set the value of [`Logger::$flushInterval`](/docs/api/yii/log/logger/index.md#property-flushInterval). Defaults to 1000, meaning the [`Logger::flush()`](/docs/api/yii/log/logger/index.md#method-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()`](/docs/api/yii/log/logger/index.md#method-flush). |

**Returns**

`mixed`

### setLogger() <span id="method-setLogger"></span>

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

Sets the connected logger.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | [`Logger`](/docs/api/yii/log/logger/index.md)`\|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()`](/docs/api/yii/baseyii/index.md#method-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()`](/docs/api/yii/baseyii/index.md#method-setLogger) instead. |

**Returns**

`mixed`

### setTraceLevel() <span id="method-setTraceLevel"></span>

```php
public function setTraceLevel(int $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `int` | How many application call stacks should be logged together with each message. This method will set the value of [`Logger::$traceLevel`](/docs/api/yii/log/logger/index.md#property-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`

### generateTargetFailErrorMessage() <span id="method-generateTargetFailErrorMessage"></span>

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

Generate target error message

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$target` | [`Target`](/docs/api/yii/log/target/index.md) | Log target object |
| `$throwable` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | Catched exception |
| `$method` | `string` | Full method path |

**Returns**

`yii\log\LogMessage`: Generated error message data

**Since**

2.0.32