Temporary print log with new file
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Simple Text Log'); // Simple Text Log
$logger->info('Array Log'.print_r($myArrayVar, true)); // Array Log
Factory Method
You need to inject \Psr\Log\LoggerInterface class into constructor to call logger object
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));
}
Or you directly use this code in phtml file:
To print string Output in debug.log
\Magento\Framework\App\ObjectManager::getInstance()
->get('Psr\Log\LoggerInterface')->debug('Your Message');
To print array Output in system.log
$myArray = array('test1'=>'123', 'test2'=>'123', 'test3'=>'123');
$level = '100'; // use one of: 100, 200, 250, 300, 400, 500, 550, 600
\Magento\Framework\App\ObjectManager::getInstance()
->get('Psr\Log\LoggerInterface')
->log($level, print_r($myArray, true));
8+1 Thank you, that's a useful interface/class/type to know about -- but it's not clear from your answer where the information will be logged and how (if possible) to change that location. – Alan Storm – 2015-12-03T07:21:20.677
You check Manager.php for following class Magento\Framework\Event and add this line $this->logger->debug($eventName); than after refresh page and check debug.txt file you get all evant name for specific page. – Pratik – 2015-12-03T07:30:59.110
2Technically, this is the 'correct' way to instantiate a logger in your own custom classes - particularly if you intend to keep it around rather than just some quick debug. However, there are several core classes - particularly Block classes - which automatically instantiate and store a _logger property. If you extend one of these core classes then there's no need to repeat the logic.
Other answers dig into creating handlers to define your own log file, but the default logs are always /var/log/system.log or /var/log/debug.log. I believe the specific logging function determines which is used. – Jeremy Rimpo – 2017-07-06T16:11:36.397
4For me, the "debug" level started to work only when I enabled "Log to file" in Configuration > Advanced > Developer > Debug. Using 2.2 – Omer Sabic – 2017-11-15T09:45:14.053