When you are developing a website, you will probably find the very need to have that will diary the parameters or the custom messages designed for the website. While Magento some has a built-in log center based on Monolog library, there will probably magento store manager come a time when you need to make a custom logging. You can find this specific package in the following place: “MtAGENTO2_ROOT/vendor/monolog“.
Custom Logging inside Magento 2
The important Magento 2 journal facility elegance is “Magento\Framework\Logger\Monolog“, and this is usually defined in “MAGENTO2_ROOT/app/etc/di. xml” as:
<preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog" />
You can see that this training extends class “Monolog\Logger” coming from magento top menu extension monolog package.
<? php
/**
* Copyright © 2015 Magento. All privileges reserved.
* See REPLICATING. txt for license particulars.
*/
namespace Magento\Framework\Logger;
make use of Monolog\Logger;
class Monolog expands Logger
In class “Monolog\Logger” there are a couple of interesting means of creating logs.
These strategies allow couple of controversies, the 1st argument is message (string) and the second one is recommended array parameter (you can easily pass instance of object).
Some methods:
$this-> _logger-> addDebug($message); // log place: var/log/system. log
$this-> _logger-> addInfo($message); // log place: var/log/exception. log
$this-> _logger-> addNotice($message); // log place: var/log/exception. log
$this-> _logger-> addError($message); // log place: var/log/exception. log
$this-> _logger-> critical($e); // log place: var/log/exception. log
One beneficial example for logging php exception:
In Magento a single we are using static approach
Mage:: logException($e);
In Magento 2 we are using an occasion of “Magento\Framework\Logger\Monolog” and approach “critical” for logging exception to this rule from try-catch
$this-> _logger-> critical($e);
// instance involving $e will be converted to line (magic metod __toString() will probably be called).
Let’s start with one of how to get instance of Magento\Framework\Logger\Monolog in your class.
Magento two uses dependency injection all the things instances of classes come through group constructor.
If you want to use thing “Magento\Framework\Logger\Monolog” then the instance must be passed through constructor of your course.
Example of this code is definitely shown below:
<? php
namespace Inchoo\Test\Model;
class Case
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 could view in which many of us flushed “\Psr\Log\LoggerInterface $logger” in class thru constructor in order to be able to apply log object in this elegance.
After that we can use case in point “$this-> _logger” in class “Inchoo\Test\Model\Example“.
If you want to write a log in your current custom file name, inside a custom location, then the method is not that simple: -), you should create a custom Log handler.
Main Magento 2 firewood class has three handlers which are defined in the very same “MAGENTO2_ROOT/app/etc/di. xml” file. These kinds of handlers are: exception, method and debug.
<type name="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi: type="string"> major </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 rarely need to pass log target through constructor, because the check object already exists.
For instance , we have this situation on every html page block class which expands “\Magento\Framework\View\Element\Template” or in design class which extends “\Magento\Framework\Model\AbstractModel“.
These “parent” classes have property “$_logger” instance connected with: Magento\Framework\Logger. This is just a simple into creating custom fire wood when working on Magento a couple of stand. Everyone is going to look at the fact that everyone have to have to establish the custom file and also made to order holiday location to record important computer data when working on Magento 2 . not Check if you need a constructor not really when working with Magento
only two to help build personalized sign archives.
http://www.magebuzz.com/events-calendar.html
Không có nhận xét nào:
Đăng nhận xét