Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 10.x / PHP 8.2.x / PHP Fatal error: Declaration of Monolog ? #46165

Closed
Faks opened this issue Feb 18, 2023 · 25 comments
Closed

Laravel 10.x / PHP 8.2.x / PHP Fatal error: Declaration of Monolog ? #46165

Faks opened this issue Feb 18, 2023 · 25 comments

Comments

@Faks
Copy link

Faks commented Feb 18, 2023

  • Laravel Version: 10.0.3
  • PHP Version: 8.2.3
  • Database Driver MySQL & Version: 8.0.32

Description:

PHP Fatal error: Declaration of Monolog\Logger::emergency(Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerInterface::emergency($message, array $context = []) in /var/www/dev/corvid.dev/vendor/monolog/monolog/src/Monolog/Logger.php on line 669
PHP Fatal error: Uncaught Error: Class "Monolog\Logger" not found in /var/www/dev/corvid.dev/vendor/monolog/monolog/src/Monolog/Handler/AbstractHandler.php:59
Stack trace:
#0 /var/www/dev/corvid.dev/vendor/monolog/monolog/src/Monolog/Handler/AbstractHandler.php(38): Monolog\Handler\AbstractHandler->setLevel()
#1 /var/www/dev/corvid.dev/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php(49): Monolog\Handler\AbstractHandler->__construct()
#2 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Log/LogManager.php(185): Monolog\Handler\StreamHandler->__construct()
#3 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Log/LogManager.php(139): Illuminate\Log\LogManager->createEmergencyLogger()
#4 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Log/LogManager.php(122): Illuminate\Log\LogManager->get()
#5 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Log/LogManager.php(645): Illuminate\Log\LogManager->driver()
#6 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(274): Illuminate\Log\LogManager->error()
#7 /var/www/dev/corvid.dev/vendor/nunomaduro/collision/src/Adapters/Laravel/ExceptionHandler.php(46): Illuminate\Foundation\Exceptions\Handler->report()
#8 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(182): NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler->report()
#9 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(226): Illuminate\Foundation\Bootstrap\HandleExceptions->handleException()
#10 /var/www/dev/corvid.dev/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(250): Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown()
#11 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap{closure}()
#12 {main}
thrown in /var/www/dev/corvid.dev/vendor/monolog/monolog/src/Monolog/Handler/AbstractHandler.php on line 59

Steps To Reproduce:

easy to get this bug run in cli: php artisan asdas

composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^8.2",
        "guzzlehttp/guzzle": "^7.5",
        "laravel/framework": "^10.0.3",
        "laravel/sanctum": "^3.2.1",
        "laravel/telescope": "^4.13.2",
        "laravel/tinker": "^2.8"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.8",
        "barryvdh/laravel-ide-helper": "^2.13",
        "fakerphp/faker": "^1.21.0",
        "laravel/pint": "^1.5",
        "mockery/mockery": "^1.5.1",
        "nunomaduro/collision": "^7.0.4",
        "nunomaduro/larastan": "^2.4.1",
        "phpunit/phpunit": "^10.0.7",
        "spatie/laravel-ignition": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}
@eamon-straughn
Copy link

eamon-straughn commented Feb 18, 2023

I'm having this bug also

PHP: 8.1.15
Laravel: 10

Declaration of Monolog\Logger::emergency(Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerInterface::emergency($message, array $context = [])

`

bacon/bacon-qr-code 2.0.8
brick/math 0.10.2
clue/stream-filter v1.6.0
dasprid/enum 1.0.3
dflydev/dot-access-data v3.0.2
doctrine/cache 2.2.0
doctrine/dbal 3.6.0
doctrine/deprecations v1.0.0
doctrine/event-manager 2.0.0
doctrine/inflector 2.0.6
doctrine/lexer 3.0.0
dragonmantank/cron-expression v3.3.2
egulias/email-validator 4.0.1
fakerphp/faker v1.21.0
filp/whoops 2.14.6
fruitcake/php-cors v1.2.0
graham-campbell/result-type v1.1.0
guzzlehttp/guzzle 7.5.0
guzzlehttp/promises 1.5.2
guzzlehttp/psr7 2.4.3
guzzlehttp/uri-template v1.0.1
hamcrest/hamcrest-php v2.0.1
http-interop/http-factory-guzzle 1.2.0
jean85/pretty-package-versions 2.0.5
knplabs/knp-snappy v1.4.1
laravel/breeze v1.19.1
laravel/fortify v1.16.0
laravel/framework v10.0.0
laravel/laravel dev-main@7ee05b8
laravel/pint v1.5.0
laravel/sail v1.20.2
laravel/sanctum v3.2.1
laravel/serializable-closure v1.3.0
laravel/tinker v2.8.0
league/commonmark 2.3.9
league/config v1.2.0
league/flysystem 3.12.2
league/mime-type-detection 1.11.0
mockery/mockery 1.5.1
monolog/monolog 3.3.1
myclabs/deep-copy 1.11.0
nesbot/carbon 2.66.0
nette/schema v1.2.3
nette/utils v4.0.0
nikic/php-parser v4.15.3
nunomaduro/collision v7.0.2
nunomaduro/termwind v1.15.1
nyholm/psr7 1.5.1
paragonie/constant_time_encoding v2.6.3
phar-io/manifest 2.0.3
phar-io/version 3.2.1
php-http/client-common 2.6.0
php-http/discovery 1.14.3
php-http/httplug 2.3.0
php-http/message 1.13.0
php-http/message-factory v1.0.2
php-http/promise 1.1.0
phpoption/phpoption 1.9.0
phpunit/php-code-coverage 10.0.0
phpunit/php-file-iterator 4.0.1
phpunit/php-invoker 4.0.0
phpunit/php-text-template 3.0.0
phpunit/php-timer 6.0.0
phpunit/phpunit 10.0.7
pragmarx/google2fa v8.0.1
psr/cache 3.0.0
psr/container 2.0.2
psr/event-dispatcher 1.0.0
psr/http-client 1.0.1
psr/http-factory 1.0.1
psr/http-message 1.0.1
psr/log 3.0.0
psr/simple-cache 3.0.0
psy/psysh v0.11.12
ralouphie/getallheaders 3.0.3
ramsey/collection 2.0.0
ramsey/uuid 4.7.3
sebastian/cli-parser 2.0.0
sebastian/code-unit 2.0.0
sebastian/code-unit-reverse-lookup 3.0.0
sebastian/comparator 5.0.0
sebastian/complexity 3.0.0
sebastian/diff 5.0.0
sebastian/environment 6.0.0
sebastian/exporter 5.0.0
sebastian/global-state 6.0.0
sebastian/lines-of-code 2.0.0
sebastian/object-enumerator 5.0.0
sebastian/object-reflector 3.0.0
sebastian/recursion-context 5.0.0
sebastian/type 4.0.0
sebastian/version 4.0.1
sentry/sdk 3.3.0
sentry/sentry 3.13.1
sentry/sentry-laravel 3.2.0
spatie/backtrace 1.2.1
spatie/flare-client-php 1.3.5
spatie/ignition 1.4.3
spatie/laravel-ignition 2.0.0
spatie/laravel-permission 5.9.1
symfony/console v6.2.5
symfony/css-selector v6.2.5
symfony/deprecation-contracts v3.2.0
symfony/error-handler v6.2.5
symfony/event-dispatcher v6.2.5
symfony/event-dispatcher-contracts v3.2.0
symfony/finder v6.2.5
symfony/http-client v6.2.6
symfony/http-client-contracts v3.2.0
symfony/http-foundation v6.2.6
symfony/http-kernel v6.2.6
symfony/mailer v6.2.5
symfony/mime v6.2.5
symfony/options-resolver v6.2.5
symfony/polyfill-ctype v1.27.0
symfony/polyfill-intl-grapheme v1.27.0
symfony/polyfill-intl-idn v1.27.0
symfony/polyfill-intl-normalizer v1.27.0
symfony/polyfill-mbstring v1.27.0
symfony/polyfill-php72 v1.27.0
symfony/polyfill-php80 v1.27.0
symfony/polyfill-uuid v1.27.0
symfony/process v6.2.5
symfony/psr-http-message-bridge v2.1.4
symfony/routing v6.2.5
symfony/service-contracts v3.2.0
symfony/string v6.2.5
symfony/translation v6.2.5
symfony/translation-contracts v3.2.0
symfony/uid v6.2.5
symfony/var-dumper v6.2.5
symfony/yaml v6.2.5
theseer/tokenizer 1.2.1
tijsverkoyen/css-to-inline-styles 2.2.6
twilio/sdk 6.44.3
vlucas/phpdotenv v5.5.0
voku/portable-ascii 2.0.1
webmozart/assert 1.11.0

`

Only Occurs in Laravel 10

@eamon-straughn
Copy link

eamon-straughn commented Feb 18, 2023

Can't use the Logger and it breaks capturing Exceptions, displays a 500 error

This occurs when an exception is thrown. Because it throws a 500 and not the lovely laravel debugger screen, I only was able to catch this in sentry.

It's been happening when any error is thrown.

https://packagist.org/packages/monolog/monolog
https://packagist.org/packages/psr/log

Seems the issue is in psr/log.

Tested with APP_DEBUG=false and no maintenance page is displayed just the 500 error.

So can't use the framework in production on a live system

@henzeb
Copy link
Contributor

henzeb commented Feb 18, 2023

Composer version?

@eamon-straughn
Copy link

Composer version 2.5.2 2023-02-04 14:33:22

@AegirLeet
Copy link
Contributor

Do you have the PSR extension (https://github.com/jbboehr/php-psr) installed? Uninstall it and try again.

@eamon-straughn
Copy link

Removed the psr extension. removed vendor and reinstalled composer. Same issue

@eamon-straughn
Copy link

Yeah think that's fixed it.

@eamon-straughn
Copy link

Yeah it's logging to laravel.log now. think you need to let people know to disable the psr.so extension

@Faks
Copy link
Author

Faks commented Feb 18, 2023

@eamon-straughn, @AegirLeet AegirLeet Hi, tested did recompile my, laradock without extension and did fresh install from composer seems it is the cause.

@syntafin
Copy link

Okay, removing the PSR extension did the trick for me too.

@eamon-straughn
Copy link

@eamon-straughn, @AegirLeet AegirLeet Hi, tested did recompile my, laradock without extension and did fresh install from composer seems it is the cause.

nice to see you got it working, yeah removing the extension does work

@rodrigopedra
Copy link
Contributor

Hey @driesvints,

The unofficial PSR extension available in PECL ships old PSR versions, and thus conflicts with many libraries.

One option is to add a conflict entry to composer.json so developers are aware they shouldn't be using this extension with a newer Laravel version.

Much like what Laminas has already done:

https://github.com/laminas/laminas-servicemanager/blob/f32cdaea6dcba17b5701e46455a80ce4006970cb/composer.json#L55-L56

I would send a PR, but I am not sure which (if not all), composer.json files should be updated.

For reference, here are some related issues:

@filipac
Copy link

filipac commented Feb 20, 2023

Oh boy, I wish I could see this before losing 1 hour of debugging it on the weekend. I was looking at the interface and the class matched, I removed the vendor folder and tried everything. Then I noticed PsrExt namespace in error logs and that's when I learned about the PSR exception.

@driesvints
Copy link
Member

@rodrigopedra are you sure we can just add "ext-psr": "*"? Wondering if that might be disruptive for others..

@rodrigopedra
Copy link
Contributor

rodrigopedra commented Feb 20, 2023

@driesvints no, I am not sure.

I don't think extensions can be targeted by version, as regular packages are checked against their version and metadata on packagist, or on some other repository, whereas extensions are checked on the machine level. But I am not sure.

On the other hand, with this extension installed, Laravel 10 just can't run, as you can see from the reports above.

So I think adding it wouldn't make any harm. At least, users would have a saner error message to debug their installation problems.

@driesvints
Copy link
Member

Hi all. It seems there really isn't anything we can do here. Laravel v10 is already released so if we'd add ext-psr to the conflicts now it wouldn't prevent people from installing Laravel with the v10.0.0 tag. So that's not really a solution anymore. We're just going to have to ask you to uninstall that extension if you want to use Laravel v10.

@DanielTate
Copy link

DanielTate commented Mar 10, 2023

I had this issue with fresh laravel 10, php8.1 and composer 2.5. I fixed it by commenting out the ;extension=psr.so in my php config.

I don't think this is a good solution but I'm not sure how to solve this.

@yazeed
Copy link

yazeed commented Apr 14, 2023

Yeah it's logging to laravel.log now. think you need to let people know to disable the psr.so extension

This fixed it for me.

@nickpoulos
Copy link

Does that mean there will be a fix in later versions -- Laravel 10.x?

@g105b
Copy link

g105b commented May 2, 2023

I noticed in my logs that I was seeing a lot of inheritance errors from the PsrExt namespace, and realised that I'd inadvertently installed the php-psr package via my OS's package manager. In my case apt remove php8.1-psr resolved my issue.

@thalesbarbosab
Copy link

thalesbarbosab commented May 18, 2023

I had a same issue on dump-autoload

PHP Fatal error: Declaration of Monolog\Logger::emergency(Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerInterface::emergency($message, array $context = [])

PHP: 8.2.0
Laravel: v10.11.0

@qazihamayun
Copy link

I noticed in my logs that I was seeing a lot of inheritance errors from the PsrExt namespace, and realised that I'd inadvertently installed the php-psr package via my OS's package manager. In my case apt remove php8.1-psr resolved my issue.

this solve my Issue, I removed php8.1-psr and things are working fine now

@mfonisoebong
Copy link

Do you have the PSR extension (https://github.com/jbboehr/php-psr) installed? Uninstall it and try again.

Thanks.

@BayronVazquez
Copy link

It seems the problem arises from the php-psr extension installed on the system, To see if you have the extension installed, run this command.

sudo dpkg --get-selections | grep php

search something like

php8.2-psr
php7.4-psr

Depending on the version of PHP that you have installed on your system, proceed to remove the package.

sudo apt remove php8.2-psr

that should solve the problem

@alnutile
Copy link
Contributor

alnutile commented Dec 6, 2023

In case the future me needs help on this and github actions you can disable extension with : so the :psr did it

      - name: Setup PHP, with composer and extensions
        uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
        with:
          php-version: ${{ matrix.php-versions }}
          tools: phpmd
          extensions: mbstring, dom, fileinfo, mysql, grpc, :psr
          coverage: xdebug #optional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests