0

Final Class yii\log\PsrMessage

Inheritanceyii\log\PsrMessage
ImplementsStringable
Available since version22.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/log/PsrMessage.php

Stores a PSR-3 message together with its original level and context.

String conversion performs the optional PSR-3 placeholder interpolation while ignoring context values that cannot be safely converted to strings.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() yii\log\PsrMessage
__toString() Returns the message with safely convertible context placeholders interpolated. yii\log\PsrMessage
getContext() Returns the PSR-3 context. yii\log\PsrMessage
getLevel() Returns the original PSR-3 level. yii\log\PsrMessage
getMessage() Returns the message without placeholder interpolation. yii\log\PsrMessage

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string|\Stringable $message, array $context, string $level )
$message string|\Stringable

The PSR-3 message, cast to string eagerly so a throwing {@see \Stringable} fails at the logging call site instead of during a later target flush.

$context array

The PSR-3 context.

$level string

The original PSR-3 level.

                public function __construct(
    Stringable|string $message,
    private array $context,
    private string $level,
) {
    $this->_message = (string) $message;
}

            
__toString() public method

Returns the message with safely convertible context placeholders interpolated.

public string __toString ( )

                public function __toString(): string
{
    $replace = [];
    foreach ($this->context as $key => $value) {
        if (is_scalar($value) || $value === null) {
            $replace['{' . $key . '}'] = (string) $value;
            continue;
        }
        if (!$value instanceof Stringable) {
            continue;
        }
        try {
            $replace['{' . $key . '}'] = (string) $value;
        } catch (Throwable) {
            // PSR-3 context values must never cause logging to fail.
        }
    }
    return strtr($this->_message, $replace);
}

            
getContext() public method

Returns the PSR-3 context.

public array getContext ( )

                public function getContext(): array
{
    return $this->context;
}

            
getLevel() public method

Returns the original PSR-3 level.

public string getLevel ( )

                public function getLevel(): string
{
    return $this->level;
}

            
getMessage() public method

Returns the message without placeholder interpolation.

public string getMessage ( )

                public function getMessage(): string
{
    return $this->_message;
}