PHP message: PHP Fatal error: Declaration of Drupal\Core\Logger\LoggerChannel::log($level, Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerTrait::log($level, $message, array $context = [])

Created on 14 April 2023, over 1 year ago
Updated 28 May 2024, 7 months ago

Problem/Motivation

PHP message: PHP Fatal error: Declaration of Drupal\Core\Logger\LoggerChannel::log($level, Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerTrait::log($level, $message, array $context = [])

Steps to reproduce

On an Ubuntu 22.04 system,

sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-imap php8.1-redis php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring php8.1-curl libapache2-mod-php
sudo apt install composer

su - drupal
wget https://ftp.drupal.org/files/projects/drupal-10.1.x-dev.tar.gz
tar -xzvf drupal-10.1.x-dev.tar.gz
cd drupal-10.1.x-dev
composer

Expected composer to run.

Result was a PHP error:

PHP Fatal error: Declaration of Drupal\Core\Logger\LoggerChannel::log($level, Stringable|string $message, array $context = []): void must be compatible with PsrExt\Log\LoggerTrait::log($level, $message, array $context = [])

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ’¬ Support request
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated 19 minutes ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @billybaroo
  • Welcome to the Drupal Community! There are some things you should know:

    • Drupal 10.1.x is a development release. If you are brand-new to Drupal, you should use a supported release. But if you want to test the development release of Drupal, please do!
    • Downloading compressed archives is not the recommended way to install Drupal. Release pages β†’ explain the proper way if you are actually starting a website project.

    That said, I can't reproduce this as written. It works for me. You could have there an old version of Composer.

  • πŸ‡¨πŸ‡¦Canada alberto56

    I get a similar error if I try to use this code in PHP 8.1 and Drupal 10:

    namespace Drupal\my_module\Logger;
    
    use Drupal\Core\Logger\RfcLoggerTrait;
    use Psr\Log\LoggerInterface;
    
    /**
     * The logger which intercepts system messages.
     */
    class MyModuleLogger implements LoggerInterface {
    
      use RfcLoggerTrait;
    
      /**
       * {@inheritdoc}
       */
      public function log($level, $message, array $context = []) {
        ...
      }
    
    }
    

    The above works fine in Drupal 9, but causes Fatal error: Declaration of Drupal\my_module\Logger\MyModuleLogger::log($level, string $message, array $context = []) must be compatible with Drupal\Core\Logger\RfcLoggerTrait::log($level, Stringable|string $message, array $context = []): void in Drupal 10.

    If I change my code to be compatible, then when try the same code in Drupal 9 it no longer works.

  • πŸ‡¨πŸ‡¦Canada alberto56

    This seems to works. Not sure it passes the smell test though...

    
    namespace Drupal\my_module\Logger;
    
    use Drupal\Core\Logger\RfcLoggerTrait;
    use Psr\Log\LoggerInterface;
    
    /**
     * The logger which intercepts system messages.
     */
    abstract class MyModuleLoggerCommon implements LoggerInterface {
    
      use RfcLoggerTrait;
    
      public function logCommon($level, $message, array $context = []) {
        ...
      }
    
    }
    
    if (version_compare(\Drupal::VERSION, '10', '>=')) {
      class MyModuleLogger extends MyModuleLoggerCommon {
    
        /**
         * {@inheritdoc}
         */
        public function log($level, string|\Stringable $message, array $context = []): void {
          $this->logCommon($level, $message, $context);
        }
    
      }
    }
    else {
      class MyModuleLogger extends MyModuleLoggerCommon {
    
        /**
         * {@inheritdoc}
         */
    
        public function log($level, $message, array $context = []) {
          $this->logCommon($level, $message, $context);
        }
      };
    }
    
  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡¨πŸ‡­Switzerland olegiv

    I also had the "PHP Fatal error: Declaration of Drupal\Core\Logger\LoggerChannel..." issue while composer installing Drupal 10.0.9 and Drupal 10.1.0 under Debian 11.7 & PHP 8.1.

    The solution was to disable php-psr extension which had been active in my case.

    Hope it helps!

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    My own operating environment is using CentOS 7 with PHP 8.1 and httpd (apache). I did not have a module installed for php-psr so I'm still stuck. Here's my related forum post.

    https://www.drupal.org/forum/support/upgrading-drupal/2023-10-24/upgrade... β†’

    On the forum post, you'll find my composer.json file's contents.

  • How are you invoking Drush?

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    @cilefen I'm calling drush from the project's top-level directory. vendor/bin/drush -l foldername cr The issue is not drush itself, but the fact that the logger keeps throwing a PHP fatal error that prevents anything else from happening.

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    I was able to resolve the issue by making a small and direct change to the LoggerChannel.php file in core. Attached is the patch generated. The change simply updated the calls to match PSR.

  • Are you using the drupal/core-recommended Composer package? You are experiencing unusual things.

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    Yes, drupal/core-recommended is part of the project. Here is the composer show drupal/core-recommended output.

    name     : drupal/core-recommended
    descrip. : Core and its dependencies with known-compatible minor versions. Require this project INSTEAD OF drupal/core.
    keywords :
    versions : * 10.1.5
    type     : metapackage
    license  : GNU General Public License v2.0 or later (GPL-2.0-or-later) (OSI approved) https://spdx.org/licenses/GPL-2.0-or-later.html#licenseText
    homepage :
    source   : [git] https://github.com/drupal/core-recommended.git 2c5cf420ddb06f3e9b624d168b724ca1c7c326e2
    dist     : [zip] https://api.github.com/repos/drupal/core-recommended/zipball/2c5cf420ddb06f3e9b624d168b724ca1c7c326e2 2c5cf420ddb06f3e9b624d168b724ca1c7c326e2
    path     : null
    names    : drupal/core-recommended
    
    support
    source : https://github.com/drupal/core-recommended/tree/10.1.5
    
    requires
    asm89/stack-cors ~v2.1.1
    composer/semver ~3.3.2
    doctrine/annotations ~1.14.3
    doctrine/deprecations ~v1.1.1
    doctrine/lexer ~2.1.0
    drupal/core 10.1.5
    egulias/email-validator ~4.0.1
    guzzlehttp/guzzle ~7.7.0
    guzzlehttp/psr7 ~2.5.0
    masterminds/html5 ~2.8.0
    mck89/peast ~v1.15.4
    pear/archive_tar ~1.4.14
    pear/console_getopt ~v1.4.3
    pear/pear-core-minimal ~v1.10.13
    pear/pear_exception ~v1.0.2
    psr/cache ~3.0.0
    psr/container ~2.0.2
    psr/event-dispatcher ~1.0.0
    psr/http-client ~1.0.2
    psr/http-factory ~1.0.2
    psr/log ~3.0.0
    ralouphie/getallheaders ~3.0.3
    sebastian/diff ~4.0.5
    symfony/console ~v6.3.0
    symfony/dependency-injection ~v6.3.0
    symfony/deprecation-contracts ~v3.3.0
    symfony/error-handler ~v6.3.0
    symfony/event-dispatcher ~v6.3.0
    symfony/event-dispatcher-contracts ~v3.3.0
    symfony/http-foundation ~v6.3.0
    symfony/http-kernel ~v6.3.0
    symfony/mime ~v6.3.0
    symfony/polyfill-ctype ~v1.27.0
    symfony/polyfill-iconv ~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-php83 ~v1.27.0
    symfony/process ~v6.3.0
    symfony/psr-http-message-bridge ~v2.2.0
    symfony/routing ~v6.3.0
    symfony/serializer ~v6.3.0
    symfony/service-contracts ~v3.3.0
    symfony/string ~v6.3.0
    symfony/translation-contracts ~v3.3.0
    symfony/validator ~v6.3.0
    symfony/var-dumper ~v6.3.0
    symfony/var-exporter ~v6.3.0
    symfony/yaml ~v6.3.0
    twig/twig ~v3.6.0
    
    conflicts
    webflo/drupal-core-strict *
  • This has appeared in other forms https://drupal.stackexchange.com/questions/314072/drush-fatal-error-with.... In some cases it was that Drupal Core or other dependencies were installed twice.

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    psr/log 3.0.0 changed the interface of LoggerInterface::log() to include types: https://github.com/php-fig/log/blob/3.0.0/src/LoggerInterface.php#L124

    Your patch in #12 implies you have an earlier version of psr/log installed that does not have the types set.

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    Unfortunately, that is not the case. Please see the included composer show psr/log output.

    name     : psr/log
    descrip. : Common interface for logging libraries
    keywords : log, psr, psr-3
    versions : * 3.0.0
    type     : library
    license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
    homepage : https://github.com/php-fig/log
    source   : [git] https://github.com/php-fig/log.git fe5ea303b0887d5caefd3d431c3e61ad47037001
    dist     : [zip] https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001 fe5ea303b0887d5caefd3d431c3e61ad47037001
    path     : /var/www/html/magnolia/vendor/psr/log
    names    : psr/log
    
    support
    source : https://github.com/php-fig/log/tree/3.0.0
    
    autoload
    psr-4
    Psr\Log\ => src
    
    requires
    php >=8.0.0
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    But you said in #12 "The change simply updated the calls to match PSR." - where did you get the PSR code from? I still suspect that somewhere you have an older version of psr/log that is being found first.

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    Longwave - Your comments did remind me that I've got a few globally installed packages via composer global require. I'm going through my configuration now to be sure I don't have another version of PSR Log installed.

    cilefen - Yes, I read it.

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    Global packages shouldn't be an issue, as I was able to update all sites that I manage but this one site to D10 from D9. After removing all the global packages and being sure that no ~/.config/composer folder is left behind to potentially confuse the server.

  • πŸ‡ΊπŸ‡ΈUnited States jeffery.grantham

    I was able to resolve my issue without the codebase patch in the end. I had to reinstall a couple of packages.

    composer reinstall symfony/psr-http-message-bridge psr/log

    Once the packages were reinstalled, I was able to complete the original task.

  • Status changed to Closed: works as designed about 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Thank you for reporting the solution that worked for you, this may help others in the future.

  • For the record. I got this error today in CI. I upgraded PHP from 8.1 to 8.2 in CI and the error is gone now βœ…

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    If you use GitHub Actions, the CI error was likely due to https://github.com/shivammathur/setup-php/issues/799 which was fixed within a few hours.

    I realise from that issue that people encountering this might have the psr PHP extension installed, which apparently isn't compatible with psr/log 3.

  • πŸ‡¬πŸ‡§United Kingdom em-fast1

    We are getting this error currently on core 10.2 with either php 8.1 or 8.2 running. The php extension psr is not installed.

  • πŸ‡ΊπŸ‡ΈUnited States swatchion

    Ubuntu 22 with php8.2 here, confirmed that after removing php-psr extension, this error is fixed.

    If you disable php-psr by removing 15-psr.ini file, the error is still there, this extension has to be removed.

  • πŸ‡ΊπŸ‡ΈUnited States somap

    Still I am getting this error:
    Drupal Version
    10.2.1
    PHP Fatal error: Declaration of Consolidation\Log\Logger::log($level, Stringable|string $message, array $context = []): void must be compatible with Psr\Log\LoggerInterface::log($level, $message, array $context = []) in vendor/consolidation/log/src/Logger.php on line 146

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    Using psr/log 3.0, I also had to install php-psr, apt install php-psr php8.1-psr php8.2-psr on my machine. It seemed to work.

Production build 0.71.5 2024