Final Class yii\log\PsrLogger
| Inheritance | yii\ |
|---|---|
| Available since version | 22.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 \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | yii\ |
|
| getLogger() | Returns the configured Yii logger or the current global logger. | yii\ |
| log() | Logs with an arbitrary level. | yii\ |
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 => \ |
Default mapping from PSR-3 levels to Yii log levels. | yii\ |
Method Details
| public mixed __construct ( yii\ | ||
| $logger | yii\ |
Yii logger to use. |
| $levelMap | array |
Mapping from supported PSR-3 levels to Yii log levels. |
| $category | string |
Default Yii log category. A string |
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.');
}
}
}
Returns the configured Yii logger or the current global logger.
| public yii\ |
public function getLogger(): Logger
{
return $this->logger ?? Yii::getLogger();
}
Logs with an arbitrary level.
| public void log ( mixed $level, \ | ||
| $level | mixed | |
| $message | \ |
|
| $context | array |
|
| throws | \ |
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,
);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.