In Magento 1 I was able to catch exceptions and log them into the exception.log file using Mage::logException($e);

Now, in Magento 2, I can catch (\Exception $e) but what do I do with the caught exception? How do I log it into the exception.log? Or what is the typical way to handle this?

2 Answers 2


The easiest way to start logging your exceptions would be to inject the Psr\Log\LoggerInterface into your class's constructor:

private $logger;

public function __construct(\Psr\Log\LoggerInterface $logger)
    $this->logger = $logger;

And then in your catch statement:

public function doSomething()
    try {
        /* Some logic that could throw an Exception */
    } catch (\Exception $e) {

Anything else related to how to react after an exception has been caught shouldn't be any different between M1 and M2. Your strategy would also be very specific to your exception handling use case.

  • 6
    Pro tip: M2 has support for directly passing the exception: $this->logger->debug($e).
    – nevvermind
    Jun 9, 2016 at 1:22
  • 1
    Actually to log exceptions need to use the critical() method instead debug()
    – joni jones
    Jun 9, 2016 at 9:41
  • getMessage should be a function call: getMessage() Nov 10, 2016 at 13:51
  • 1
    @LM_Fielding Good catch, fixed.
    – brendanWeb
    Nov 19, 2016 at 0:30
  • @jonijones My mistake, fixed.
    – brendanWeb
    Nov 19, 2016 at 0:31

Magento2 has different types of Exception handlers, for example:

  • StateException
  • InputException
  • InvalidEmailOrPasswordException
  • MailException
  • NotFoundException
  • ValidatorException


All handler types and their classes exist in \vendor\magento\framework\Exception.

You need to choose the relevant Exception handler for your requirements and use that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.