Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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).



3.1k

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.0k
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.



3.1k

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.