0

Final Class yii\log\PsrLogger

Inheritanceyii\log\PsrLogger » Psr\Log\AbstractLogger
Available since version22.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/log/PsrLogger.php

Adapts PSR-3 log calls to a Yii logger.

When no Yii logger is passed to the constructor, the current logger returned by {@see \Yii::getLogger()} is resolved for every call. This ensures that replacing the global Yii logger does not leave this adapter with a stale reference.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() yii\log\PsrLogger
getLogger() Returns the configured Yii logger or the current global logger. yii\log\PsrLogger
log() Logs with an arbitrary level. yii\log\PsrLogger

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_LEVEL_MAP [ \Psr\Log\LogLevel::EMERGENCY => \yii\log\Logger::LEVEL_ERROR, \Psr\Log\LogLevel::ALERT => \yii\log\Logger::LEVEL_ERROR, \Psr\Log\LogLevel::CRITICAL => \yii\log\Logger::LEVEL_ERROR, \Psr\Log\LogLevel::ERROR => \yii\log\Logger::LEVEL_ERROR, \Psr\Log\LogLevel::WARNING => \yii\log\Logger::LEVEL_WARNING, \Psr\Log\LogLevel::NOTICE => \yii\log\Logger::LEVEL_WARNING, \Psr\Log\LogLevel::INFO => \yii\log\Logger::LEVEL_INFO, \Psr\Log\LogLevel::DEBUG => \yii\log\Logger::LEVEL_TRACE, ] Default mapping from PSR-3 levels to Yii log levels. yii\log\PsrLogger

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( yii\log\Logger|null $logger null, array $levelMap self::DEFAULT_LEVEL_MAP, string $category 'application' )
$logger yii\log\Logger|null

Yii logger to use. null resolves the current global Yii logger for every call.

$levelMap array

Mapping from supported PSR-3 levels to Yii log levels.

$category string

Default Yii log category. A string category context value overrides it for that call.

                public function __construct(
    private readonly Logger|null $logger = null,
    private readonly array $levelMap = self::DEFAULT_LEVEL_MAP,
    private readonly string $category = 'application',
) {
    foreach ($this->levelMap as $level => $yiiLevel) {
        if (!is_string($level) || !is_int($yiiLevel)) {
            throw new InvalidArgumentException('The PSR-3 level map must contain string keys and integer values.');
        }
    }
}

            
getLogger() public method

Returns the configured Yii logger or the current global logger.

public yii\log\Logger getLogger ( )

                public function getLogger(): Logger
{
    return $this->logger ?? Yii::getLogger();
}

            
log() public method

Logs with an arbitrary level.

public void log ( mixed $level, \Stringable|string $message, array $context = [] )
$level mixed
$message \Stringable|string
$context array
throws \Psr\Log\InvalidArgumentException

If the level is not supported.

                public function log($level, Stringable|string $message, array $context = []): void
{
    if (!is_string($level)) {
        throw new InvalidArgumentException(
            sprintf(
                'The PSR-3 log level must be a string, %s given.',
                get_debug_type($level),
            ),
        );
    }
    if (!array_key_exists($level, $this->levelMap)) {
        throw new InvalidArgumentException(
            "Unsupported PSR-3 log level: $level.",
        );
    }
    $category = isset($context['category']) && is_string($context['category'])
        ? $context['category']
        : $this->category;
    $this->getLogger()->log(
        new PsrMessage($message, $context, $level),
        $this->levelMap[$level],
        $category,
    );
}