Thứ Hai, 27 tháng 6, 2016

Personalized Logging with Magento

Personalized Logging with Magento a couple of When you are developing a website, you can definitely find often the require in order to firewood the aspects magento mobile menu extension or the custom messages intended for the website. While Magento only two has a built-in log capability based on Monolog library, there will probably come a time when you need to manufacture a custom logging. You can find that package in the following position: “MAGENTO2_ROOT/vendor/monolog“.


Custom Logging with Magento 2

The key Magento 2 diary facility training is “Magento\Framework\Logger\Monolog“, and this is normally defined in “MAGENTO2_ROOT/app/etc/di. xml” as:

  <preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog" />
You can see that this group extends class “Monolog\Logger” by 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 techniques for creating logs.
These strategies allow couple of controversies, the mangento banner manager 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 exemption from try-catch

$this->  _logger->  critical($e);
// instance about $e will be converted to cord (magic metod __toString() will likely be called).
Let’s start with certainly one of the how to get instance of Magento\Framework\Logger\Monolog in your class.

Magento 3 uses dependency injection or any instances of classes come through course constructor.
If you want to use objective “Magento\Framework\Logger\Monolog” then the instance really should be passed through constructor of your school.

Example of this code is actually shown below:

 <? php
namespace Inchoo\Test\Model;

class Illustration
    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 will look at the fact that people handed “\Psr\Log\LoggerInterface $logger” in class by means of constructor in order to be able to make use of log object in this school.
After that we can use occasion “$this->  _logger” in class “Inchoo\Test\Model\Example“.

If you want to write a log in your personal custom file name, in a very custom location, then the practice is not that simple: -), you must create a custom Log handler.

Main Magento 2 check class has three handlers which are defined in the identical “MAGENTO2_ROOT/app/etc/di. xml” file. These kind of handlers are: exception, process and debug.

  <type name="Magento\Framework\Logger\Monolog">
  <arguments>
  <argument name="name" xsi: type="string">  key  </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 would not need to pass log item through constructor, because the fire wood object already exists.
For instance , we have this situation on every php block class which exercises “\Magento\Framework\View\Element\Template” or in unit class which extends “\Magento\Framework\Model\AbstractModel“.

These “parent” classes formerly property “$_logger” instance associated with: Magento\Framework\Logger. This is just a essential into creating custom record when working on Magento couple of podium. You actually will probably find this you actually demand to explain the custom file and customized position to sign your computer data when working on Magento minimal payments Check if you need a constructor not really when working with Magento
 a pair of so that you can develop customizable journal data
http://www.magebuzz.com/ajax-cart.html.

Không có nhận xét nào:

Đăng nhận xét