PsrTarget
API reference › yii › log › PsrTarget
Exports Yii log messages to a PSR-3 logger.
The Yii category, trace, and memory usage are passed as PSR-3 context. The original Yii timestamp can also be added
by enabling $addTimestampToContext. Context keys carried by a round-tripped PsrMessage take precedence:
trace, memory, category, and timestamp are only added when the original context does not define them, and
trace only when Yii collected traces. $extractExceptionTrace deliberately overwrites trace when enabled.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$addTimestampToContext | bool | Whether to add the original Yii log timestamp to the PSR-3 context. Default: false. | PsrTarget |
$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 |
$extractExceptionTrace | bool | Whether to export an exception message and trace separately instead of stringifying the exception. Default: false. | PsrTarget |
$levelMap | array<int, | Mapping from Yii levels to PSR-3 levels. Default: [\. | PsrTarget |
$levels | array| | Message levels that this target is interested in. | Target |
$logger | Psr\ | The PSR-3 logger used to export messages. | PsrTarget |
$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 |
$psrLevels | list<string>| | Exact PSR-3 levels this target exports after Yii level and category filtering. null means all PSR-3 levels. | PsrTarget |
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() | Exports accumulated messages to the configured PSR-3 logger. | PsrTarget |
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 | |
getLogger() | Returns the configured PSR-3 logger. | PsrTarget |
getMessagePrefix() | Returns a string to be prefixed to the given message. | Target |
getPsrLevels() | Returns the exact PSR-3 levels this target exports. | PsrTarget |
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. | PsrTarget |
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 |
setLogger() | Sets the PSR-3 logger. | PsrTarget |
setPsrLevels() | Sets the exact PSR-3 levels this target exports after normal Yii target filtering. | PsrTarget |
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 |
resolvePsrLevel() protected | Resolves the PSR-3 level for a Yii message. | PsrTarget |
Method details
Section titled “Method details”export()
Section titled “export() ”public function export(): voidExports accumulated messages to the configured PSR-3 logger.
getLogger()
Section titled “getLogger() ”public function getLogger(): Psr\Log\LoggerInterfaceReturns the configured PSR-3 logger.
getPsrLevels()
Section titled “getPsrLevels() ”public function getPsrLevels(): list<string>|nullReturns the exact PSR-3 levels this target exports.
Returns
list<string>|null: null means all PSR-3 levels.
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
setLogger()
Section titled “setLogger() ”public function setLogger(Psr\Log\LoggerInterface $logger): voidSets the PSR-3 logger.
Parameters
| Parameter | Type | Description |
|---|---|---|
$logger | Psr\ |
Throws
yii\base\InvalidConfigException: If a Yii-to-PSR adapter is configured as the destination.
setPsrLevels()
Section titled “setPsrLevels() ”public function setPsrLevels(list<string>|null $levels): voidSets the exact PSR-3 levels this target exports after normal Yii target filtering.
Parameters
| Parameter | Type | Description |
|---|---|---|
$levels | list<string>| | null or an empty array enables all PSR-3 levels. |
Throws
yii\base\InvalidConfigException: If a level is not recognized.
resolvePsrLevel()
Section titled “resolvePsrLevel() ”protected function resolvePsrLevel(array $message): stringResolves the PSR-3 level for a Yii message.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | array | Yii log message. |
Returns
string
Throws
LogRuntimeException: If the level cannot be mapped.