0 follower

Final Class Yiisoft\Log\Message

InheritanceYiisoft\Log\Message

Message is a data object that stores log message data.

Psalm Types

Name Value
TraceItem array{file?: string, line?: integer, function?: string, class?: string, type?: string}

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Log\Message
category() Returns the log message category. {@see self::DEFAULT_CATEGORY} is returned if the category is not set. Yiisoft\Log\Message
context() Returns a value of the context parameter for the specified name. Yiisoft\Log\Message
level() Gets a log message level. Yiisoft\Log\Message
message() Gets a log message. Yiisoft\Log\Message
time() Returns the time of the log message. Yiisoft\Log\Message
trace() Returns the debug trace. Yiisoft\Log\Message

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_CATEGORY 'application' Yiisoft\Log\Message

Method Details

Hide inherited methods

__construct() public method

See also:

  • \Psr\Log\LoggerTrait::log()
  • \Psr\Log\LogLevel
public mixed __construct ( string $level, string|\Stringable $message, array $context = [] )
$level string

Log message level.

$message string|\Stringable

Log message.

$context array

Log message context.

throws \Psr\Log\InvalidArgumentException

for invalid log message level.

                public function __construct(string $level, string|Stringable $message, array $context = [])
{
    Logger::assertLevelIsSupported($level);
    $this->level = $level;
    $this->message = $this->parse($message, $context);
    $this->context = $context;
    $this->defaultTime = new DateTimeImmutable();
}

            
category() public method

Returns the log message category. {@see self::DEFAULT_CATEGORY} is returned if the category is not set.

public string category ( )
return string

The log message category.

                public function category(): string
{
    $category = $this->context['category'] ?? self::DEFAULT_CATEGORY;
    if (!is_string($category)) {
        throw new LogicException(
            'Invalid category value in log context. Expected "string", got "' . get_debug_type($category) . '".'
        );
    }
    return $category;
}

            
context() public method

Returns a value of the context parameter for the specified name.

If no name is specified, the entire context is returned.

public mixed context ( string|null $name null, mixed $default null )
$name string|null

The context parameter name.

$default mixed

If the context parameter does not exist, the $default will be returned.

return mixed

The context parameter value.

                public function context(?string $name = null, mixed $default = null): mixed
{
    if ($name === null) {
        return $this->context;
    }
    return $this->context[$name] ?? $default;
}

            
level() public method

Gets a log message level.

public string level ( )
return string

Log message level.

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

            
message() public method

Gets a log message.

public string message ( )
return string

Log message.

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

            
time() public method

Returns the time of the log message.

public DateTimeImmutable time ( )
return DateTimeImmutable

The log message time.

                public function time(): DateTimeImmutable
{
    $time = $this->context['time'] ?? $this->defaultTime;
    if ($time instanceof DateTimeInterface) {
        return DateTimeImmutable::createFromInterface($time);
    }
    if (is_int($time) || is_float($time)) {
        try {
            return new DateTimeImmutable('@' . $time);
        } catch (Exception $e) {
            throw new LogicException('Invalid time value in log context: ' . $time . '.', previous: $e);
        }
    }
    if (is_string($time)) {
        $format = match (true) {
            str_contains($time, '.') => 'U.u',
            str_contains($time, ',') => 'U,u',
            default => 'U',
        };
        $date = DateTimeImmutable::createFromFormat($format, $time);
        if ($date === false) {
            throw new LogicException('Invalid time value in log context: "' . $time . '".');
        }
        return $date;
    }
    throw new LogicException('Invalid time value in log context. Got "' . get_debug_type($time) . '".');
}

            
trace() public method

Returns the debug trace.

public array[]|null trace ( )
return array[]|null

The debug trace or null if the trace is not set.

                public function trace(): ?array
{
    $trace = $this->context['trace'] ?? null;
    if ($trace === null) {
        return null;
    }
    /**
     * @psalm-var list<TraceItem> $trace We believe that the debug trace in context is always received as result of call
     * `debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)`.
     */
    return $trace;
}