马琴托2:更换法师::日志方法?
-
13-12-2019 - |
解决方案
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}
例如,PSR记录器使用debug,exception,system:
$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));
\Magento\Framework\App\ObjectManager::getInstance()
->get(\Psr\Log\LoggerInterface::class)->debug('message');
阅读更多: blog.mageprince.com
带有新文件的临时打印日志
$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\日志\LoggerInterface class into构造函数调用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文件中使用此代码:
在调试中打印字符串输出.日志
\Magento\Framework\App\ObjectManager::getInstance()
->get('Psr\Log\LoggerInterface')->debug('Your Message');
在系统中打印数组输出.日志
$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);
}
}
方便国际海事组织退后一步
在一个简单的方法,如果你不想创建依赖注入或其他任何使用下面的代码,它将存储登录 system.log
档案
$logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
$logger->info('message');
仅此而已。.
不,没有直接的等价物。现在有点复杂了。
包括psr记录器类在您的文件中使用 使用方法 然后打电话 addDebug()
方法。这将打印日志消息 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");
}
}
更新后的:19/08/2019
如果您正在寻找优雅的自定义日志处理程序,我建议您使用虚拟类型(不需要添加任何PHP代码)
灵感来自于 Petar Dzhambazov 和 哈尔克, ,女士们,先生们,我向您介绍了一种更好,更短的方法,而不是一直重复的自定义日志代码。
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>
使用方法
Vendor\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文件或行-使用Magento2的优点:虚拟类型!!!
希望这有助于;)
在2.2中有一个logger的更新。您可以通过运行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));
}
注射;注射
$logger
构造函数中的类\Psr\Log\LoggerInterface $logger
这是通过传递$logger作为参数来实现的。初始化
$logger
在构造函数中$this->logger = $logger
在要记录的类中的函数中,使用下面的行
$this->logger->debug($message); $this->logger->log($level, $message);
如果您需要在具有自定义日志文件的单个类中使用它:
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);