We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

help to deal with exceptions and logger

I have declared the logger via DI

$di->set( 'logger', function () use ($config) {

    $fileNameMain = strtr($config->logger->fileNames->cli->main, ['[%date%]' => date('Y-m-d')]);
    $fileNameEaisto = strtr($config->logger->fileNames->cli->eaisto, ['[%date%]' => date('Y-m-d')]);
    $adapterMain = new Stream($fileNameMain);
    $adapterEaisto = new Stream($fileNameEaisto);

    $logger = new Logger(
        'messages',
        [
            'main' => $adapterMain,
            'eaisto' => $adapterEaisto
        ]
    );

    return $logger;
}

);

and there is a class for handling exceptions:

namespace App\Services\Exceptions;

class EaistoException extends \Exception { public function __construct($msg){ $this->logger->critical($msg); } }

How do I pass the exception object to the logger? The above code causes an error:

Undefined property: App\Services\Exceptions\EaistoException::$logger

sorry for the stupid question :)

You would have to create a base Exception class that is Phalcon\Di\Injectable, and then inherit all your errors from there, that way the Exception will have a logger property.

The tricky part is that you must inherit from either \Throwable or \Exception, so you must implement your own Injectable methods (setDI, getDI, __get).



4.3k

You would have to create a base Exception class that is Phalcon\Di\Injectable, and then inherit all your errors from there, that way the Exception will have a logger property.

The tricky part is that you must inherit from either \Throwable or \Exception, so you must implement your own Injectable methods (setDI, getDI, __get).

thanks! maybe you have an example? I will be very grateful to you.



77.7k
Accepted
answer
edited Feb '20

A very simple example. though not quite what i was talking aboug:

abstract class LoggingException extends \Exception {
    const LOGGER_SVC = 'logger';

    public function __construct($message='', $code=0, $previous=null) {
        $di = \Phalcon\Di\FactoryDefault::getDefault();
        $logger = $di->getShared(self::LOGGER_SVC);
        $logger->critical($message);
        parent::__construct($message, $code, $previous);
    }
}

The caveat is that this will always use the globally set default di - you cannot specify your own.



4.3k

A very simple example. though not quite what i was talking aboug:

abstract class LoggingException extends \Exception {
  const LOGGER_SVC = 'logger';

  public function __construct($message='', $code=0, $previous=null) {
      $di = \Phalcon\Di\FactoryDefault::getDefault();
      $logger = $di->getShared(self::LOGGER_SVC);
      $logger->critical($message);
      parent::__construct($message, $code, $previous);
  }
}

The caveat is that this will always use the globally set default di - you cannot specify your own.

thanks! I have a couple of days will return to this issue, I try your example. you helped me.