Larastan: Call to an undefined method Illuminate\Log\Logger::getHandlers()

Created on 22 Jan 2021  路  5Comments  路  Source: nunomaduro/larastan

  • Larastan Version: dev-master
  • --level used: 5

Description

Customizing Monolog i got error on phpstan/larastan.

 ------ -------------------------------------------------------------------
  Line   Logging\LineFormatter.php
 ------ -------------------------------------------------------------------
  17     Call to an undefined method Illuminate\Log\Logger::getHandlers().
 ------ -------------------------------------------------------------------

Laravel code where the issue was found

From Laravel doc

<?php

namespace App\Logging;

use Monolog\Formatter\LineFormatter;

class CustomizeFormatter
{
    /**
     * Customize the given logger instance.
     *
     * @param  \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($logger)
    {
        foreach ($logger->getHandlers() as $handler) {
            $handler->setFormatter(new LineFormatter(
                '[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
            ));
        }
    }
}

Most helpful comment

I test it with \Monolog\Processor\IntrospectionProcessor and i only need to set the docblock like this

/**
 * @var \Monolog\Handler\ProcessableHandlerInterface $handler
 */
foreach ($logger->getHandlers() as $handler) {
    $handler->pushProcessor($introspection);
}

Thank you @szepeviktor @canvural for your work, this is an amazing project

All 5 comments

I'm using dev-master because i need the patch 8aa4b6d

From the docs.

The Illuminate\Log\Logger instance proxies all method calls to the underlying Monolog instance

Hi,

Fixed with: https://github.com/nunomaduro/larastan/commit/208df29dab0ad80cabd6dc1b4e2232fc0f1044a0

You'll get additional error on line with $handler->setFormatter because getHandlers returns an array of HandlerInterface and setFormatter is not a method on that interface. Only some handlers have that method via FormattableHandlerInterface So you'd need to check if a handler implements that or check if the method exists before using it.

@Pe46dro You've got express service.

I test it with \Monolog\Processor\IntrospectionProcessor and i only need to set the docblock like this

/**
 * @var \Monolog\Handler\ProcessableHandlerInterface $handler
 */
foreach ($logger->getHandlers() as $handler) {
    $handler->pushProcessor($introspection);
}

Thank you @szepeviktor @canvural for your work, this is an amazing project

Was this page helpful?
0 / 5 - 0 ratings