Fatal error: Uncaught Error: Call to undefined method Phalcon\Logger\Adapter\Stream::log()

I cant log a message using

$this->logger->log('hi');

i am getting undefined method call for

  • log
  • critical
  • error
  • info and others too...

I registered logger as a service such as

$di->set('logger', function () {
$loggerConfigs = $this->getConfig()->logger;
$filename = trim($loggerConfigs->get('filename'), '\\/');
$path     = rtrim($loggerConfigs->get('path'), '\\/') . DIRECTORY_SEPARATOR;

$formatter = new FormatterLine($loggerConfigs->get('format'), $loggerConfigs->get('date'));
$logger    = new FileLogger($path . $filename);

$logger->setFormatter($formatter);

return $logger;
});

Then using this service gives me error.

Any idea?

Have a look at this:

https://docs.phalcon.io/4.0/en/logger#creating-a-logger

You pretty much have to create an adapter (stream) and attach the formatter you want to it. After that you create a Logger component and add the adapter to it.

edited Dec '19

Thanks for the guidance Nikolaos... Now I am getting 2 entries for each logged message.

I set the service as follows now:

$di->set('logger', function () {
    $loggerConfigs = $this->getConfig()->logger;
    $filename = trim($loggerConfigs->get('filename'), '\\/');
    $path     = rtrim($loggerConfigs->get('path'), '\\/') . DIRECTORY_SEPARATOR;

    $formatter = new FormatterLine($loggerConfigs->get('format'), $loggerConfigs->get('date'));
    $fileLogger    = new FileLogger($path . $filename);

    $fileLogger->setFormatter($formatter);

    $logger = new Logger(
        'messages',
        [
            'main' => $fileLogger
        ]
        );

    return $logger;
});

Then in controller I am calling

$this->logger->error('hi'); 

Here are the logged messages:

Sat 28 19:41:17 [error] hi

Sat 28 19:41:17 [error] hi

Sat 28 19:41:45 [error] hi

Sat 28 19:41:45 [error] hi

Sat 28 19:47:19 [error] hi

Sat 28 19:47:19 [error] hi

What can be the issue?

Have a look at this:

https://docs.phalcon.io/4.0/en/logger#creating-a-logger

You pretty much have to create an adapter (stream) and attach the formatter you want to it. After that you create a Logger component and add the adapter to it.