Abstract Class yii\log\Target

Inheritanceyii\log\Target » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Subclassesyii\debug\LogTarget, yii\elasticsearch\ElasticsearchTarget, yii\log\DbTarget, yii\log\EmailTarget, yii\log\FileTarget, yii\log\SyslogTarget, yii\mongodb\log\MongoDbTarget
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/log/Target.php

Target is the base class for all log target classes.

A log target object will filter the messages logged by yii\log\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.

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
$categories array List of message categories that this target is interested in. yii\log\Target
$enabled boolean Indicates whether this log target is enabled. Defaults to true. Note that the type of this property differs in getter and setter. See getEnabled() and setEnabled() for details. yii\log\Target
$except array List of message categories that this target is NOT interested in. yii\log\Target
$exportInterval integer How many messages should be accumulated before they are exported. yii\log\Target
$levels integer The message levels that this target is interested in. This is a bitmap of level values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter and setter. See getLevels() and setLevels() for details. For more details and usage information on Target, see the guide article on logging & targets. yii\log\Target
$logVars array List of the PHP predefined variables that should be logged in a message. yii\log\Target
$maskVars array 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. yii\log\Target
$messages array The messages that are retrieved from the logger so far by this log target. yii\log\Target
$microtime boolean Whether to log time with microseconds. yii\log\Target
$prefix callable A PHP callable that returns a string to be prefixed to every exported message. yii\log\Target

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\base\BaseObject
__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
collect() Processes the given log messages. yii\log\Target
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
export() Exports log $messages to a specific destination. yii\log\Target
filterMessages() Filters the given messages according to their categories and levels. yii\log\Target
formatMessage() Formats a log message for display as a string. yii\log\Target
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getEnabled() Check whether the log target is enabled. yii\log\Target
getLevels() yii\log\Target
getMessagePrefix() Returns a string to be prefixed to the given message. yii\log\Target
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\base\BaseObject
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
setEnabled() Sets a value indicating whether this log target is enabled. yii\log\Target
setLevels() Sets the message levels that this target is interested in. yii\log\Target
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getContextMessage() Generates the context information to be logged. yii\log\Target
getTime() Returns formatted ('Y-m-d H:i:s') timestamp for message. yii\log\Target

Property Details

$categories public property

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'.

public array $categories = []
$enabled public property

Indicates whether this log target is enabled. Defaults to true. Note that the type of this property differs in getter and setter. See getEnabled() and setEnabled() for details.

public boolean $enabled null
$except public property

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'.

See also $categories.

public array $except = []
$exportInterval public property

How 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.

public integer $exportInterval 1000
$levels public property

The message levels that this target is interested in. This is a bitmap of level values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter and setter. See getLevels() and setLevels() for details.

For more details and usage information on Target, see the guide article on logging & targets.

public integer $levels null
$logVars public property

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 - var will be logged.
  • var.key - only var[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.

See also yii\helpers\ArrayHelper::filter().

public array $logVars = ['_GET''_POST''_FILES''_COOKIE''_SESSION''_SERVER']
$maskVars public property (available since version 2.0.16)

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 - var will be logged as ***
  • var.key - only var[key] will be logged as ***
public array $maskVars = ['_SERVER.HTTP_AUTHORIZATION''_SERVER.PHP_AUTH_USER''_SERVER.PHP_AUTH_PW']
$messages public property

The messages that are retrieved from the logger so far by this log target. Please refer to yii\log\Logger::$messages for the details about the message structure.

public array $messages = []
$microtime public property (available since version 2.0.13)

Whether to log time with microseconds. Defaults to false.

public boolean $microtime false
$prefix public property

A 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).

public callable $prefix null

Method Details

collect() public method

Processes 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).

public void collect ( $messages, $final )
$messages array

Log messages to be processed. See yii\log\Logger::$messages for the structure of each message.

$final boolean

Whether this method is called at the end of the current application

export() public abstract method

Exports log $messages to a specific destination.

Child classes must implement this method.

public abstract void export ( )
filterMessages() public static method

Filters the given messages according to their categories and levels.

public static array filterMessages ( $messages, $levels 0, $categories = [], $except = [] )
$messages array

Messages to be filtered. The message structure follows that in yii\log\Logger::$messages.

$levels integer

The message levels to filter by. This is a bitmap of level values. Value 0 means allowing all levels.

$categories array

The message categories to filter by. If empty, it means all categories are allowed.

$except array

The message categories to exclude. If empty, it means all categories are allowed.

return array

The filtered messages.

formatMessage() public method

Formats a log message for display as a string.

public string formatMessage ( $message )
$message array

The log message to be formatted. The message structure follows that in yii\log\Logger::$messages.

return string

The formatted message

getContextMessage() protected method

Generates the context information to be logged.

The default implementation will dump user information, system variables, etc.

protected string getContextMessage ( )
return string

The context information. If an empty string, it means no context information.

getEnabled() public method

Check whether the log target is enabled.

public boolean getEnabled ( )
return boolean

A value indicating whether this log target is enabled.

getLevels() public method

public integer getLevels ( )
return integer

The message levels that this target is interested in. This is a bitmap of level values. Defaults to 0, meaning all available levels.

getMessagePrefix() public method

Returns 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.

public string getMessagePrefix ( $message )
$message array

The message being exported. The message structure follows that in yii\log\Logger::$messages.

return string

The prefix string

getTime() protected method (available since version 2.0.13)

Returns 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'.

protected string getTime ( $timestamp )
$timestamp float
setEnabled() public method

Sets a value indicating whether this log target is enabled.

public void setEnabled ( $value )
$value boolean|callable

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;
}
setLevels() public method

Sets 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: yii\log\Logger::LEVEL_ERROR, yii\log\Logger::LEVEL_WARNING, yii\log\Logger::LEVEL_INFO, yii\log\Logger::LEVEL_TRACE and yii\log\Logger::LEVEL_PROFILE.

For example,

['error', 'warning']
// which is equivalent to:
Logger::LEVEL_ERROR | Logger::LEVEL_WARNING
public void setLevels ( $levels )
$levels array|integer

Message levels that this target is interested in.

throws yii\base\InvalidConfigException

if $levels value is not correct.