Made to order Logging throughout Magento
Made to order Logging throughout Magento some When you are developing a website, often typically the have towards sign the features or the custom magento ajax cart messages modeled on the website. While Magento two has a built-in log option based on Monolog library, people come a time when you need to brew a custom logging. You can find the following package in the following site: “MAGENTO2_ROOT/vendor/monolog“.Custom Logging around Magento 2
The principal Magento 2 record facility category is “Magento\Framework\Logger\Monolog“, and this will 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 type extends class “Monolog\Logger” out of monolog package.
<? php
/**
* Copyright © 2015 Magento. All the law reserved.
* See DUPLICATION. txt for license points.
*/
namespace Magento\Framework\Logger;
usage Monolog\Logger;
class Monolog provides Logger
In class “Monolog\Logger” there are a couple of interesting strategies to creating logs.
These tactics take a couple of fights, the best argument is message (string) and the second one is an option array parameter (you will pass instance of object).
Some methods:
$this-> _logger-> addDebug($message); // log holiday location: var/log/system. log
$this-> _logger-> addInfo($message); // log holiday location: var/log/exception. log
$this-> _logger-> addNotice($message); // log holiday location: var/log/exception. log
$this-> _logger-> addError($message); // log holiday location: var/log/exception. log
$this-> _logger-> critical($e); // log holiday location: var/log/exception. log
One important example for logging php exception:
In Magento 4 we are using static system
Mage:: logException($e);
In Magento 2 we are using an situation of “Magento\Framework\Logger\Monolog” and system “critical” for logging omission from try-catch
$this-> _logger-> critical($e);
// instance regarding $e will be converted to archipelago (magic metod __toString() shall be called).
Let’s start with certainly how to get magento affiliate extension instance of Magento\Framework\Logger\Monolog in your class.
Magento a couple of uses dependency injection and many types of instances of classes come through elegance constructor.
If you want to use subject “Magento\Framework\Logger\Monolog” then the instance need to be passed through constructor of your training.
Example of this code is usually shown below:
<? php
namespace Inchoo\Test\Model;
class Model
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 can easily observe that will we all went by “\Psr\Log\LoggerInterface $logger” in class as a result of constructor in order to be able to usage log object in this training.
After that we can use situation “$this-> _logger” in class “Inchoo\Test\Model\Example“.
If you want to write a log in your individual custom file name, within the custom location, then the approach is not that simple: -), you have got to create a custom Log handler.
Main Magento 2 journal class has three handlers which are defined in the exact “MAGENTO2_ROOT/app/etc/di. xml” file. Most of these handlers are: exception, procedure and debug.
<type name="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi: type="string"> most important </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 dont need to pass log thing through constructor, because the diary object already exists.
Like we have this situation on every html document block class which offers “\Magento\Framework\View\Element\Template” or in style class which extends “\Magento\Framework\Model\AbstractModel“.
These “parent” classes have formerly property “$_logger” instance involving: Magento\Framework\Logger. This is just a primary into creating custom firewood when working on Magento only two software. Anyone can view in which anyone need to have to identify the custom file together with tailor-made spot to check crucial computer data when working on Magento second . Check if you need a constructor not really when working with Magento
3 that will make made to order fire wood records.
http://www.magebuzz.com/birthday-coupon.html