Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Created April 26, 2017 07:35
Show Gist options
  • Save molotovbliss/0c5ce8be5b7c227d64cf4110004b6c46 to your computer and use it in GitHub Desktop.
Save molotovbliss/0c5ce8be5b7c227d64cf4110004b6c46 to your computer and use it in GitHub Desktop.
Mage::Log or similar logging in M2
<?php
# Logging mechanisms in Magento 2 as Mage::Log is not globally
# inherited anymore like in M1, and new vendor options exist.
# Original sauce: https://magento.stackexchange.com/a/92442/69
# Mage::Log in M2
# You can also write to the logs using the Zend library:
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
Edited
# You can also print PHP objects and arrays:
$logger->info(print_r($yourArray, true));
# OR Mono/PSR Logger which is new in M2
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
# You use debug, exception, system for psr logger for example
$this->logger->info($message);
$this->logger->debug($message);
@MrDaar
Copy link

MrDaar commented Oct 5, 2017

What about:

class Mage {
    public static function log($message)
    {
        // log things
    }
}
use \My\Thing\Helper\Logger as Mage;
Mage::log($e->getMessage());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment