# PsrLogger

> Adapts PSR-3 log calls to a Yii logger.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [log](/docs/api/yii/log/index.md) › PsrLogger

| final class | `yii\log\PsrLogger` |
| --- | --- |
| Extends | `Psr\Log\AbstractLogger` |
| Since | 22.0 |
| Source | [`framework/log/PsrLogger.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/log/PsrLogger.php#L32) |

Adapts PSR-3 log calls to a Yii logger.

When no Yii logger is passed to the constructor, the current logger returned by [`Yii::getLogger()`](/docs/api/yii/baseyii/index.md#method-getLogger) is resolved
for every call. This ensures that replacing the global Yii logger does not leave this adapter with a stale reference.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) |  | `PsrLogger` |
| [`getLogger()`](#method-getLogger) | Returns the configured Yii logger or the current global logger. | `PsrLogger` |
| [`log()`](#method-log) | Logs with an arbitrary level. | `PsrLogger` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-DEFAULT_LEVEL_MAP"></span>`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. | `PsrLogger` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(yii\log\Logger|null $logger = null, array<string, int> $levelMap = \self::DEFAULT_LEVEL_MAP, string $category = 'application')
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$logger` | [`Logger`](/docs/api/yii/log/logger/index.md)`\|null` | Yii logger to use. `null` resolves the current global Yii logger for every call. |
| `$levelMap` | `array<string, int>` | 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. |

### getLogger() <span id="method-getLogger"></span>

```php
public function getLogger(): yii\log\Logger
```

Returns the configured Yii logger or the current global logger.

**Returns**

[`Logger`](/docs/api/yii/log/logger/index.md)

### log() <span id="method-log"></span>

```php
public function log(mixed $level, Stringable|string $message, array<array-key, mixed> $context = []): void
```

Logs with an arbitrary level.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$level` | `mixed` |  |
| `$message` | [`Stringable`](https://www.php.net/manual/en/class.stringable.php)`\|string` |  |
| `$context` | `array<array-key, mixed>` |  |

**Throws**

- `Psr\Log\InvalidArgumentException`: If the level is not supported.