Tailor made Logging around Magento 3 When you are developing a website, many times the exact want for you to record the specifics or the custom adsmanager extensions messages for the website. While Magento couple of has a built-in log ability based on Monolog library, people come a time when you need to generate a custom logging. You can find this kind of package in the following spot: “MAGENTO2_ROOT/vendor/monolog“.
Custom Logging throughout Magento 2
The most important Magento 2 fire wood facility school is “Magento\Framework\Logger\Monolog“, and this can be defined in “MAGENTO2_ROOT/app/etc/di. xml” as:
<preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog" />
You can see that this category extends class “Monolog\Logger” via monolog package.
<? php
/**
* Copyright © 2015 Magento. All protection under the law reserved.
* See BURNING. txt for license aspects.
*/
namespace Magento\Framework\Logger;
implement Monolog\Logger;
class Monolog lengthens Logger
In class “Monolog\Logger” there are a couple of interesting methods creating logs.
These options acknowledge 2 quarrels, the main argument is message (string) and the second one is non-compulsory array parameter (you will be able to pass instance of object).
Some methods:
$this-> _logger-> addDebug($message); // log selection: var/log/system. log
$this-> _logger-> addInfo($message); // log selection: var/log/exception. log
$this-> _logger-> addNotice($message); // log selection: var/log/exception. log
$this-> _logger-> addError($message); // log selection: var/log/exception. log
$this-> _logger-> critical($e); // log selection: var/log/exception. log
One invaluable example for logging php exception:
In Magento 2 we are using static way
Mage:: logException($e);
In Magento 2 we are using an circumstance of “Magento\Framework\Logger\Monolog” and way “critical” for logging bar from try-catch
$this-> _logger-> critical($e);
// instance with $e will be converted to chain (magic metod __toString() are going to be called).
Let’s start with among the how to get instance of Magento\Framework\Logger\Monolog in your class.
Magento a pair of uses dependency injection and magento events calendar all sorts of instances of classes come through type constructor.
If you want to use concept “Magento\Framework\Logger\Monolog” then the instance needs to be passed through constructor of your elegance.
Example of this code is certainly shown below:
<? php
namespace Inchoo\Test\Model;
class Case study
protected $_logger;
public function __construct(
\Psr\Log\LoggerInterface $logger, //log injection
array $data = []
)
$this-> _logger = $logger;
parent::__construct($data);
public function someExampleMethod()
/*
some logic of method
*/
//accessing to logger instance and calling log method
$this-> _logger-> addDebug('some text or variable');
You may notice which all of us surpassed “\Psr\Log\LoggerInterface $logger” in class with constructor in order to be able to implement log object in this group.
After that we can use circumstance “$this-> _logger” in class “Inchoo\Test\Model\Example“.
If you want to write a log in your own personal custom file name, in the custom location, then the course of action is not that simple: -), you need to create a custom Log handler.
Main Magento 2 sign class has three handlers which are defined in the similar “MAGENTO2_ROOT/app/etc/di. xml” file. All these handlers are: exception, technique and debug.
<type name="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi: type="string"> principal </argument>
<argument name="handlers" xsi: type="array">
<item name="exception" xsi: type="object"> Magento\Framework\Logger\Handler\Critical </item>
<item name="system" xsi: type="object"> Magento\Framework\Logger\Handler\System </item>
<item name="debug" xsi: type="object"> Magento\Framework\Logger\Handler\Debug </item>
</argument>
</arguments>
</type>
In some classes we seldom need to pass log problem through constructor, because the journal object already exists.
Like we have this situation on every css block class which runs “\Magento\Framework\View\Element\Template” or in type class which extends “\Magento\Framework\Model\AbstractModel“.
These “parent” classes previously property “$_logger” instance for: Magento\Framework\Logger. This is just a standard into creating custom diary when working on Magento some base. People could discover of which people will need to specify the custom file in addition to tailor made site to firewood the information you have when working on Magento installment payments on your Check if you need a constructor not really when working with Magento
two to be able to set up custom made check information.
http://www.magebuzz.com/super-menu.html
Không có nhận xét nào:
Đăng nhận xét