Target
API reference › yii › log › Target
6 subclasses
Target is the base class for all log target classes.
A log target object will filter the messages logged by Logger according
to its $levels and $categories properties. It may also export the filtered
messages to specific destination defined by the target, such as emails, files.
Level filter and category filter are combinatorial, i.e., only messages
satisfying both filter conditions will be handled. Additionally, you
may specify $except to exclude messages of certain categories.
For more details and usage information on Target, see the guide article on logging & targets.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$categories | list<string> | List of message categories that this target is interested in. Default: []. | Target |
$enabled | bool| | A boolean value or a callable to obtain the value from. | Target |
$except | list<string> | List of message categories that this target is NOT interested in. Default: []. | Target |
$exportInterval | int | How many messages should be accumulated before they are exported. Default: 1000. | Target |
$levels | array| | Message levels that this target is interested in. | Target |
$logVars | list<string> | List of the PHP predefined variables that should be logged in a message. Default: ['_GET',. | Target |
$maskVars | list<string> | List of the PHP predefined variables that should NOT be logged “as is” and should always be replaced with a mask *** before logging, when exist. Default: ['_SERVER.HTTP_AUTHORIZATION',. Available since 2.0.16. | Target |
$messages | list<yii\ | The messages that are retrieved from the logger so far by this log target. Default: []. | Target |
$microtime | bool | Whether to log time with microseconds. Default: false. Available since 2.0.13. | Target |
$prefix | callable(yii\ | A PHP callable that returns a string to be prefixed to every exported message. Default: null. | Target |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__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\ |
collect() | Processes the given log messages. | Target |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
export() abstract | Exports log $messages to a specific destination. | Target |
filterMessages() static | Filters the given messages according to their categories and levels. | Target |
formatMessage() | Formats a log message for display as a string. | Target |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getEnabled() | Check whether the log target is enabled. | Target |
getLevels() | Target | |
getMessagePrefix() | Returns a string to be prefixed to the given message. | Target |
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. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
setEnabled() | Sets a value indicating whether this log target is enabled. | Target |
setLevels() | Sets the message levels that this target is interested in. | Target |
trigger() | Triggers an event. | yii\ |
formatMessageText() protected | Converts a log message value to text. | Target |
getContextMessage() protected | Generates the context information to be logged. | Target |
getTime() protected | Returns formatted (‘Y-m-d H:i:s’) timestamp for message. | Target |
Property details
Section titled “Property details”$categories
Section titled “$categories ”public list<string> $categories = []List of message categories that this target is interested in.
Defaults to empty, meaning all categories. You can use an asterisk at the end of a category so that the category may be used to match those categories sharing the same common prefix. For example, ‘yii\db*’ will match categories starting with ‘yii\db’, such as ‘yii\db\Connection’.
$enabled
Section titled “$enabled ”public bool|callable $enabledA boolean value or a callable to obtain the value from.
The callable value is available since version 2.0.13.
A callable may be used to determine whether the log target should be enabled in a dynamic way. For example, to only enable a log if the current user is logged in you can configure the target as follows:
'enabled' => function() { return !Yii::$app->user->isGuest;}$except
Section titled “$except ”public list<string> $except = []List of message categories that this target is NOT interested in.
Defaults to empty, meaning no uninteresting messages.
If this property is not empty, then any category listed here will be excluded from $categories.
You can use an asterisk at the end of a category so that the category can be used to
match those categories sharing the same common prefix. For example, ‘yii\db*’ will match
categories starting with ‘yii\db’, such as ‘yii\db\Connection’.
$exportInterval
Section titled “$exportInterval ”public int $exportInterval = 1000How many messages should be accumulated before they are exported.
Defaults to 1000. Note that messages will always be exported when the application terminates. Set this property to be 0 if you don’t want to export messages until the application terminates.
$logVars
Section titled “$logVars ”public list<string> $logVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']List of the PHP predefined variables that should be logged in a message.
Note that a variable must be accessible via $GLOBALS. Otherwise it won’t be logged.
Defaults to ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER'].
Since version 2.0.9 additional syntax can be used: Each element could be specified as one of the following:
var-varwill be logged.var.key- onlyvar[key]key will be logged.!var.key-var[key]key will be excluded.
Note that if you need $_SESSION to logged regardless if session was used you have to open it right at the start of your request.
$maskVars
Section titled “$maskVars ”public list<string> $maskVars = ['_SERVER.HTTP_AUTHORIZATION', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']List of the PHP predefined variables that should NOT be logged “as is” and should always be replaced with a mask *** before logging, when exist.
Defaults to [ '_SERVER.HTTP_AUTHORIZATION', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']
Each element could be specified as one of the following:
var-varwill be logged as***var.key- onlyvar[key]will be logged as***
In addition, this property accepts (case-insensitive) patterns. For example:
_SERVER.*_SECRETmatches all ending with_SECRET, such as$_SERVER['TOKEN_SECRET']etc._SERVER.SECRET_*matches all starting withSECRET_, such as$_SERVER['SECRET_TOKEN']etc._SERVER.*SECRET*matches all containingSECRETi.e. both of the above.
Since
2.0.16
$messages
Section titled “$messages ”public list<yii\log\LogMessage> $messages = []The messages that are retrieved from the logger so far by this log target.
Please refer to Logger::$messages for the details about the message structure.
$microtime
Section titled “$microtime ”public bool $microtime = falseWhether to log time with microseconds.
Defaults to false.
Since
2.0.13
$prefix
Section titled “$prefix ”public callable(yii\log\LogMessage): string|null $prefix = nullA PHP callable that returns a string to be prefixed to every exported message.
If not set, getMessagePrefix() will be used, which prefixes the message with context information
such as user IP, user ID and session ID.
The signature of the callable should be function ($message).
Method details
Section titled “Method details”collect()
Section titled “collect() ”public function collect(array<int|string, yii\log\LogMessage> $messages, bool $final): mixedProcesses the given log messages.
This method will filter the given messages with $levels and $categories.
And if requested, it will also export the filtering result to specific medium (e.g. email).
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array<int| | Log messages to be processed. See Logger::$messages for the structure of each message. |
$final | bool | Whether this method is called at the end of the current application |
Returns
mixed
export()
Section titled “export() ”abstract public function export(): mixedExports log $messages to a specific destination.
Child classes must implement this method.
Returns
mixed
filterMessages()
Section titled “filterMessages() ”public static function filterMessages(array<yii\log\TKey, yii\log\LogMessage> $messages, int $levels = 0, list<string> $categories = [], list<string> $except = []): array<yii\log\TKey, yii\log\LogMessage>Filters the given messages according to their categories and levels.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array<yii\ | Messages to be filtered. The message structure follows that in Logger::$messages. |
$levels | int | The message levels to filter by. This is a bitmap of level values. Value 0 means allowing all levels. |
$categories | list<string> | The message categories to filter by. If empty, it means all categories are allowed. |
$except | list<string> | The message categories to exclude. If empty, it means all categories are allowed. |
Returns
array<yii\log\TKey, yii\log\LogMessage>: The filtered messages.
formatMessage()
Section titled “formatMessage() ”public function formatMessage(yii\log\LogMessage $message): stringFormats a log message for display as a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | yii\ | The log message to be formatted. The message structure follows that in Logger::$messages. |
Returns
string: The formatted message
getEnabled()
Section titled “getEnabled() ”public function getEnabled(): boolCheck whether the log target is enabled.
Returns
bool: A value indicating whether this log target is enabled.
getLevels()
Section titled “getLevels() ”public function getLevels(): intReturns
int: The message levels that this target is interested in. This is a bitmap of level values. Defaults to 0, meaning all available levels.
getMessagePrefix()
Section titled “getMessagePrefix() ”public function getMessagePrefix(yii\log\LogMessage $message): stringReturns a string to be prefixed to the given message.
If $prefix is configured it will return the result of the callback.
The default implementation will return user IP, user ID and session ID as a prefix.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | yii\ | The message being exported. The message structure follows that in Logger::$messages. |
Returns
string: The prefix string
setEnabled()
Section titled “setEnabled() ”public function setEnabled(bool|callable $value): mixedSets a value indicating whether this log target is enabled.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool| | A boolean value or a callable to obtain the value from. The callable value is available since version 2.0.13. A callable may be used to determine whether the log target should be enabled in a dynamic way. For example, to only enable a log if the current user is logged in you can configure the target as follows: ‘enabled’ => function() { return !Yii::$app->user->isGuest; } |
Returns
mixed
setLevels()
Section titled “setLevels() ”public function setLevels(array|int $levels): mixedSets the message levels that this target is interested in.
The parameter can be either an array of interested level names or an integer representing
the bitmap of the interested level values. Valid level names include: ‘error’,
‘warning’, ‘info’, ‘trace’ and ‘profile’; valid level values include:
Logger::LEVEL_ERROR, Logger::LEVEL_WARNING, Logger::LEVEL_INFO,
Logger::LEVEL_TRACE and Logger::LEVEL_PROFILE.
For example,
['error', 'warning']// which is equivalent to:Logger::LEVEL_ERROR | Logger::LEVEL_WARNINGParameters
| Parameter | Type | Description |
|---|---|---|
$levels | array| | Message levels that this target is interested in. |
Returns
mixed
Throws
yii\base\InvalidConfigException: if $levels value is not correct.
formatMessageText()
Section titled “formatMessageText() ”protected function formatMessageText(mixed $text): stringConverts a log message value to text.
Parameters
| Parameter | Type | Description |
|---|---|---|
$text | mixed | Log message value. |
Returns
string: Formatted message value.
Since
22.0
getContextMessage()
Section titled “getContextMessage() ”protected function getContextMessage(): stringGenerates the context information to be logged.
The default implementation will dump user information, system variables, etc.
Returns
string: The context information. If an empty string, it means no context information.
getTime()
Section titled “getTime() ”protected function getTime(float $timestamp): stringReturns formatted (‘Y-m-d H:i:s’) timestamp for message.
If $microtime is configured to true it will return format ‘Y-m-d H:i:s.u’.
Parameters
| Parameter | Type | Description |
|---|---|---|
$timestamp | float |
Returns
string
Since
2.0.13