Skip to content

Instantly share code, notes, and snippets.

@mystix
Last active June 19, 2021 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mystix/021f078b59f31f61e826d95e272e0c6a to your computer and use it in GitHub Desktop.
Save mystix/021f078b59f31f61e826d95e272e0c6a to your computer and use it in GitHub Desktop.
Magento 2: Quick and dirty Loggers
<?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