Last active
June 19, 2021 16:04
-
-
Save mystix/021f078b59f31f61e826d95e272e0c6a to your computer and use it in GitHub Desktop.
Magento 2: Quick and dirty Loggers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://magento.stackexchange.com/a/92441 | |
// method 1 (< M2.3.5) | |
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); | |
$logger = new \Zend\Log\Logger(); | |
$logger->addWriter($writer); | |
$logger->info('Your text message'); | |
// https://magento.stackexchange.com/a/320261 | |
// method 1 (>= M2.3.5) | |
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/test.log'); | |
$logger = new \Laminas\Log\Logger(); | |
$logger->addWriter($writer); | |
$logger->info('Your text message'); | |
// https://magento.stackexchange.com/a/92439 | |
// method 2 | |
\Magento\Framework\App\ObjectManager::getInstance() | |
->get(\Psr\Log\LoggerInterface::class)->debug('message'); | |
// https://magento.stackexchange.com/a/154592 | |
// method 3 (using DI) | |
protected $_logger; | |
public function __construct( | |
... | |
\Psr\Log\LoggerInterface $logger | |
... | |
) { | |
$this->_logger = $logger; | |
} | |
public function logExample() { | |
//To print string Output in debug.log | |
$this->_logger->addDebug('Your Text Or Variables'); | |
// To print array Output in system.log | |
$this->_logger->log('600', print_r($yourArray, true)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment