PHPackages                             struggle-for-php/sfp-phpstan-psr-log - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. struggle-for-php/sfp-phpstan-psr-log

ActivePhpstan-extension[Logging &amp; Monitoring](/categories/logging)

struggle-for-php/sfp-phpstan-psr-log
====================================

Extra strict and opinionated psr/log (psr-3) rules for PHPStan

1.0.1(6mo ago)42257.6k↓31.8%3[1 PRs](https://github.com/struggle-for-php/sfp-phpstan-psr-log/pulls)8MITPHPPHP ^7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Mar 14Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/struggle-for-php/sfp-phpstan-psr-log)[ Packagist](https://packagist.org/packages/struggle-for-php/sfp-phpstan-psr-log)[ RSS](/packages/struggle-for-php-sfp-phpstan-psr-log/feed)WikiDiscussions 1.1.x Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (58)Used By (8)

psr/log(PSR-3) extensions for PHPStan
=====================================

[](#psrlogpsr-3-extensions-for-phpstan)

[![Latest Stable Version](https://camo.githubusercontent.com/3b58e0de54c4ecfef840b8fea633373b5a12d3e98dba728a2ac0ce4e90f0e9f2/68747470733a2f2f706f7365722e707567782e6f72672f7374727567676c652d666f722d7068702f7366702d7068707374616e2d7073722d6c6f672f762f737461626c65)](https://packagist.org/packages/struggle-for-php/sfp-phpstan-psr-log)[![License](https://camo.githubusercontent.com/420a4a4c549b8d744b1f0e985ce754124b8aa2a7a0320759a25bfd61a09c1189/68747470733a2f2f706f7365722e707567782e6f72672f7374727567676c652d666f722d7068702f7366702d7068707374616e2d7073722d6c6f672f6c6963656e7365)](https://packagist.org/packages/struggle-for-php/sfp-phpstan-psr-log)[![Psalm coverage](https://camo.githubusercontent.com/cb9426bc988a79c3b7566bd389aef4839dff9b4d6ae2414af99728a10ddd956d/68747470733a2f2f73686570686572642e6465762f6769746875622f7374727567676c652d666f722d7068702f7366702d7068707374616e2d7073722d6c6f672f636f7665726167652e737667)](https://shepherd.dev/github/struggle-for-php/sfp-phpstan-psr-log)

`struggle-for-php/sfp-phpstan-psr-log` is extra strict and opinionated psr/log (psr-3) rules for PHPStan.

- [PHPStan](https://phpstan.org/)
- [PSR-3: Logger Interface - PHP-FIG](https://www.php-fig.org/psr/psr-3/)
- [PSR-3 Meta Document](https://www.php-fig.org/psr/psr-3/meta/)

Installation
------------

[](#installation)

To use this extension, require it in [Composer](https://getcomposer.org/):

```
composer require --dev struggle-for-php/sfp-phpstan-psr-log
```

### Manual installation

[](#manual-installation)

include extension.neon &amp; rules.neon in your project's PHPStan config:

```
includes:
    - vendor/struggle-for-php/sfp-phpstan-psr-log/rules.neon
```

Recommendation Settings
-----------------------

[](#recommendation-settings)

Write these parameters to your project's `phpstan.neon`.

```
parameters:
    sfpPsrLog:
        enableMessageStaticStringRule: false # default:true
        enableContextRequireExceptionKeyRule: true
        reportContextExceptionLogLevel: 'info'
        contextKeyOriginalPattern: '#\A[A-Za-z0-9-_]+\z#'
```

Rules
-----

[](#rules)

This package provides the following rules.

### PlaceholderCharactersRule

[](#placeholdercharactersrule)

> Placeholder names SHOULD be composed only of the characters A-Z, a-z, 0-9, underscore \_, and period .

📌 *error identifier*sfpPsrLog.placeholderCharactersInvalidChar- reports when placeholder in `$message` characters are **not**, `A-Z`, `a-z`, `0-9`, underscore `_`, and period `.`

```
// bad
$logger->info('message are {foo-hyphen}');
```

📌 *error identifier*sfpPsrLog.placeholderCharactersDoubleBraces- reports when double braces pair `{{` `}}` are used.

```
// bad
$logger->info('message are {{foo}}');
```

### PlaceholderCorrespondToKeysRule

[](#placeholdercorrespondtokeysrule)

> Placeholder names MUST correspond to keys in the context array.

📌 *error identifier*sfpPsrLog.placeholderCorrespondToKeysMissedContext- reports when placeholder exists in message, but `$context` parameter is missed.

```
// bad
$logger->info('message has {nonContext} .');
```

📌 *error identifier*sfpPsrLog.placeholderCorrespondToKeysMissedKey- reports when placeholder exists in message, but key in `$context` does not exist against them.

```
// bad
$logger->info('user {user_id} gets an error {error} .', ['user_id' => $user_id]);
```

### ContextKeyRule

[](#contextkeyrule)

Note

PSR-3 has no provisions for array keys, but this is useful in many cases.

📌 *error identifier*sfpPsrLog.contextKeyNonEmptyString- reports when context key is not **non-empty-string**.

```
// bad
[123 => 'foo']`, `['' => 'bar']`, `['baz']
```

📌 *error identifier*sfpPsrLog.contextKeyOriginalPattern- reports when context key is not matched you defined pattern.
    - if `contextKeyOriginalPattern` parameter is not set, this check would be ignored.

#### Configuration

[](#configuration)

- You can set specific key pattern by regex.(`preg_match()`)

```
parameters:
    sfpPsrLog:
        contextKeyOriginalPattern: '#\A[A-Za-z0-9-]+\z#'
```

### ContextRequireExceptionKeyRule

[](#contextrequireexceptionkeyrule)

Note

This is not a rule for along with PSR-3 specification, but provides best practices.

📌 *error identifier*sfpPsrLog.contextRequireExceptionKey- It forces `exception` key into context parameter when current scope has `\Throwable` object.

#### Example

[](#example)

```
