Answers:
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
您可以将调试,异常,系统用于PSR Logger,例如:
$this->logger->info($message);
$this->logger->debug($message);
在magento2中,您还可以使用以下Zend
库来写入日志:
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
已编辑
您还可以打印PHP对象和数组,如下所示:
$logger->info(print_r($yourArray, true));
PHP objects
不打印...
\Magento\Framework\App\ObjectManager::getInstance()
->get(\Psr\Log\LoggerInterface::class)->debug('message');
带有新文件的临时打印日志
$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
工厂方法
您需要将\ Psr \ Log \ LoggerInterface类注入构造函数以调用logger对象
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));
}
或者您直接在phtml文件中使用此代码:
在debug.log中打印字符串Output
\Magento\Framework\App\ObjectManager::getInstance()
->get('Psr\Log\LoggerInterface')->debug('Your Message');
在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));
如果要使用默认记录器,但要使用自定义文件进行记录(或其他自定义逻辑),则需要使用自定义记录器处理程序:
class Logger extends Magento\Framework\Logger\Handler\Base
{
/**
* @var string
*/
protected $fileName = '/var/log/my-log-file.log';
/**
* @var int
*/
protected $loggerType = MonologLogger::DEBUG;
}
然后将其添加为代码中的处理程序:
protected function addCustomLogHandler()
{
$logger = Data::getCustomLogger();
if(isset($this->_logger)){
$this->_logger->pushHandler($logger);
}
}
IMO便利性退后一步
$this->logger->info($message, $level);
-怎么说“使用我的上下文”?
使用然后在调用addDebug()
方法中将psr记录器类包括在文件中。这将在var/log/debug.log
文件中打印日志消息
use Psr\Log\LoggerInterface;
class demo {
function demo()
{
//EDIT: Using debug instead of addDebug for PSR compatiblity
$this->_objectManager->get('Psr\Log\LoggerInterface')->debug("your message goes here");
}
}
如果您正在寻找优雅的自定义日志处理程序,则建议您使用虚拟类型(不需要添加任何PHP代码)
女士们,先生们,从Petar Dzhambazov和halk的答案中得到启发,我向您介绍了一种更好,更短的方法,而不是一直重复自定义日志代码。
StackOverflow \ Example \ etc \ di.xml
<!-- Custom log file for StackOverflow ; Duplicate it as much as you want separate log file -->
<virtualType name="StackOverflow\Example\Model\Logger\VirtualDebug" type="Magento\Framework\Logger\Handler\Base">
<arguments>
<argument name="fileName" xsi:type="string">/var/log/stackoverflow/donald_trump.log</argument>
</arguments>
</virtualType>
<virtualType name="StackOverflow\Example\Model\Logger\VirtualLogger" type="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi:type="string">DonaldTrump</argument>
<argument name="handlers" xsi:type="array">
<item name="debug" xsi:type="object"> StackOverflow\Example\Model\Logger\VirtualDebug</item>
</argument>
</arguments>
</virtualType>
供应商\ Something \ Model \ DonaldTrump.php
<?php
/**
* Copyright © 2016 Toan Nguyen <https://nntoan.github.io>. All rights reserved.
* See COPYING.txt for license details.
*
* This is the file you want to inject your custom logger.
* Of course, your logger must be an instance of \Psr\Log\LoggerInterface.
*/
namespace Vendor\Something\Model;
/**
* DonaldTrump business logic file
*
* @package Vendor\Something\Model
* @author Toan Nguyen <https://github.com/nntoan>
*/
class DonaldTrump
{
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* DonaldTrump constructor.
*
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Psr\Log\LoggerInterface $logger,
) {
$this->logger = $logger;
}
// 1 billion lines of code after this line
}
StackOverflow \ Example \ etc \ frontend \ di.xml
<type name="Vendor\Something\Model\DonaldTrump">
<arguments>
<argument name="logger" xsi:type="object">StackOverflow\Example\Model\Logger\VirtualLogger</argument>
</arguments>
</type>
就是这样,没有多余的PHP文件或线-发挥Magento 2的优势:虚拟类型!!!
希望这可以帮助 ;)
2.2中的记录器有一个更新。您可以通过运行SQL为生产模式启用记录器:
"INSERT INTO core_config_data (scope, scope_id, path, value) VALUES ('default', '0', 'dev/debug/debug_logging', '1');"
然后,您可以\Psr\Log\LoggerInterface
像上面的答案一样使用打印日志:
protected $logger;
public function __construct(
\Psr\Log\LoggerInterface $logger
) {
$this->logger = $logger;
}
public function yourFunction() {
$data = ["test" => "testing"];
$this->logger->debug(var_export($data, true));
}
In the Magento admin panel, go to "Stores" -> "Configuration" -> "Advanced" -> "Developer" -> "Debug" -> "Log to File". Setting this to "Yes" will cause debug information to be logged to var/log/debug.log in your Magento application directory.
如果您需要在带有自定义日志文件的单个类中使用它:
public function __construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\App\Filesystem\DirectoryList $dir)
{
$this->logger = $logger;
$this->dir = $dir;
$this->logger->pushHandler(new \Monolog\Handler\StreamHandler($this->dir->getRoot().'/var/log/custom.log'));
}
将PSR记录器代码放入构造函数中:
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
那么您可以在函数中使用如下代码:
$this->logger->info($message);