Thứ Hai, 27 tháng 6, 2016

Customized Logging for Magento

Customized Logging for Magento two When you are developing a website, you might find the main demand so that you can fire wood the factors magento add to cart ajax  or the custom messages made for the website. While Magento a couple of has a built-in log service based on Monolog library, people come a time when you need to produce a custom logging. You can find this particular package in the following area: “MAGENTO2_ROOT/vendor/monolog“.


Custom Logging within Magento 2

The significant Magento 2 check facility course is “Magento\Framework\Logger\Monolog“, and this is actually defined in “MAGENTO2_ROOT/app/etc/di. xml” as:

  <preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog" />
You can see that this school extends class “Monolog\Logger” through monolog package.

 <? php
/**
* Copyright © 2015 Magento. All legal rights reserved.
* See DUPLICATING. txt for license information.
 */

namespace Magento\Framework\Logger;

utilize Monolog\Logger;

class Monolog stretches Logger


In class “Monolog\Logger” there are a couple of interesting techniques for creating logs.
These techniques settle for only two justifications,best magento affiliate extension the very first argument is message (string) and the second one is optionally available array parameter (you may pass instance of object).

Some methods:

$this->  _logger->  addDebug($message); // log area: var/log/system. log
$this->  _logger->  addInfo($message); // log area: var/log/exception. log
$this->  _logger->  addNotice($message); // log area: var/log/exception. log
$this->  _logger->  addError($message); // log area: var/log/exception. log
$this->  _logger->  critical($e); // log area: var/log/exception. log
One helpful example for logging php exception:

In Magento one we are using static technique

Mage:: logException($e);
In Magento 2 we are using an example of “Magento\Framework\Logger\Monolog” and technique “critical” for logging exclusion from try-catch

$this->  _logger->  critical($e);
// instance associated with $e will be converted to thread (magic metod __toString() is going to be called).
Let’s start with a good example of how to get instance of Magento\Framework\Logger\Monolog in your class.

Magento only two uses dependency injection and instances of classes come through category constructor.
If you want to use item “Magento\Framework\Logger\Monolog” then the instance ought to be passed through constructor of your type.

Example of this code will be shown below:

 <? php
namespace Inchoo\Test\Model;

class Instance
    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 be able to find out which will we tend to handed down “\Psr\Log\LoggerInterface $logger” in class via constructor in order to be able to utilize log object in this course.
After that we can use example “$this->  _logger” in class “Inchoo\Test\Model\Example“.

If you want to write a log in your own custom file name, within a custom location, then the procedure is not that simple: -), you will need to create a custom Log handler.

Main Magento 2 record class has three handlers which are defined in the exact same “MAGENTO2_ROOT/app/etc/di. xml” file. These types of handlers are: exception, program and debug.

  <type name="Magento\Framework\Logger\Monolog">
  <arguments>
  <argument name="name" xsi: type="string">  primary  </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 do not need to pass log subject through constructor, because the sign object already exists.
Like we have this situation on every code block class which stretches “\Magento\Framework\View\Element\Template” or in product class which extends “\Magento\Framework\Model\AbstractModel“.

These “parent” classes have previously property “$_logger” instance regarding: Magento\Framework\Logger. This is just a fundamental into creating custom journal when working on Magento 3 stage. One definitely will find out which will one have to determine the custom file along with customizable selection to diary crucial computer data when working on Magento second . Check if you need a constructor not really when working with Magento
 couple of towards establish tailor-made firewood computer files.
http://www.magebuzz.com/birthday-coupon.html

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

Đăng nhận xét