Dispatcher
API reference › yii › log › Dispatcher
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;Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$flushInterval | int | How many messages should be logged before they are sent to targets. | Dispatcher |
$logger | Logger| | The logger to be used. | Dispatcher |
$targets | array | The log targets. Default: []. | Dispatcher |
$traceLevel | int | How many application call stacks should be logged together with each message. | Dispatcher |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | Dispatcher |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
dispatch() | Dispatches the logged messages to $targets. | Dispatcher |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
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\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | Dispatcher |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
setFlushInterval() | Dispatcher | |
setLogger() | Sets the connected logger. | Dispatcher |
setTraceLevel() | Dispatcher | |
trigger() | Triggers an event. | yii\ |
generateTargetFailErrorMessage() protected | Generate target error message | Dispatcher |
Property details
Section titled “Property details”$flushInterval
Section titled “$flushInterval ”public int $flushIntervalHow many messages should be logged before they are sent to targets.
This
method returns the value of Logger::$flushInterval.
$logger
Section titled “$logger ”public yii\log\Logger|string|array $loggerThe 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.
$targets
Section titled “$targets ”public array $targets = []The log targets.
Each array element represents a single log target instance or the configuration for creating the log target instance.
$traceLevel
Section titled “$traceLevel ”public int $traceLevelHow many application call stacks should be logged together with each message.
This method returns the value of Logger::$traceLevel. Defaults to 0.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”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
$confighere. - 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()
Section titled “dispatch() ”public function dispatch(array<int|string, yii\log\LogMessage> $messages, bool $final): mixedDispatches the logged messages to $targets.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array<int| | The logged messages |
$final | bool | Whether this method is called at the end of the current application |
Returns
mixed
getFlushInterval()
Section titled “getFlushInterval() ”public function getFlushInterval(): intReturns
int: How many messages should be logged before they are sent to targets. This method returns the value of Logger::$flushInterval.
getLogger()
Section titled “getLogger() ”public function getLogger(): yii\log\LoggerGets the connected logger.
If not set, Yii::getLogger() will be used.
Returns
Logger: The logger.
getTraceLevel()
Section titled “getTraceLevel() ”public function getTraceLevel(): intReturns
int: How many application call stacks should be logged together with each message. This method returns the value of Logger::$traceLevel. Defaults to 0.
init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
setFlushInterval()
Section titled “setFlushInterval() ”public function setFlushInterval(int $value): mixedParameters
| 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. 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
setLogger()
Section titled “setLogger() ”public function setLogger(yii\log\Logger|string|array $value): mixedSets the connected logger.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | 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. |
Returns
mixed
setTraceLevel()
Section titled “setTraceLevel() ”public function setTraceLevel(int $value): mixedParameters
| 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. 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()
Section titled “generateTargetFailErrorMessage() ”protected function generateTargetFailErrorMessage(yii\log\Target $target, Throwable $throwable, string $method): yii\log\LogMessageGenerate target error message