The development process is a complex matter. And as you create software, you might have lots of questions. And in this blog, we are trying to help you answer at least some of them.
Today, we will discuss the issue of creating a message to a system log. If you are looking for a way to create a custom variable or a system message with Magento 2, then here is how you can do it.
Programming is a straightforward industry: there are always clear ways to achieve specific goals. So, after you finish reading this article, you will have a solid understanding of the process. It is no easy rogerian essay topics. So, without further ado, let’s dive into the process itself.
What are logs?
First things first. Let’s clarify the definitions to make sure we are all on the same page.
Logs are used to track record of all data input and output, both major and minor processes in the program as well as its execution results. They help you see the processes that would otherwise be left unseen or unclear.
Thus, all the events in the system are stored in the form of logs. Later information stored there can be used for analysis. Like in the case of debugging, logs allow you to track the error, what led to them and when exactly it took place. So, when performing this operation, a developer has to use logs.
How to create a custom log in Magento 2 (OOP)
The main log facility class here is called “Magento\Framework\Logger\Monolog“. This is a log used by the system by default, and it is one of the most popular logging solutions. You can find it at “MAGENTO2_ROOT/vendor/monolog”.
In this case, the logger has its own set of handlers that can classified according to their functions:
- files and system logger;
- emails and notifications;
- servers and network logger;
- development logger;
- DB log.
Each one of the handlers above either processes the message or sends it to the next handler in line.
Typically, Magento’s set of handlers are as follows:
- <preference for=”Psr\Log\LoggerInterface” type=”Magento\Framework\Logger\Monolog” />
- <type name=”Magento\Framework\Logger\Monolog”>
- <arguments>
- <argument name=”name” xsi:type=”string”>main</argument>
- <argument name=”handlers” xsi:type=”array”>
- <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>
There is an option of customizing the logic of this message by specifying your own set of handlers instead of “debug” and “system” ones mentioned above.
As simple as that, this logging method can be a great solution.
If you are willing to write a message to a system log in aspect-oriented programming, then you must remember that in this case business logic is separated from the service code (which in our case is logging).
For instance, here is what the logging logic combined with the business logic would look like:
- public function doSomething()
- {
- try {
- //do something
- } catch (\Exception $e) {
- $this->logger->critical($e);
- }
- }
Meanwhile, the code with the business logic alone would look like as follows:
- /**
- * @Logging
- */
- public function doSomething()
- {
- //do something
- }
If you need to write a message for a log in Magento 2 with method “critical”, you must use this snippet of code with the exception from try-catch:
$this->_logger->critical($e);
// instance of $e will be converted to string (magic metod __toString() will be called).
According to the Inchoo.net, all instances of classes go through class constructor. The code in this case would look like this:
<?php
namespace Inchoo\Test\Model;
class Example{
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’);
}
}
All in all, logging in Magento 2 is possible thanks to the Monolog for logging. If you are trying to code this feature with Magento 2, then our article provides you with all the necessary information.
Should you have more questions, you can always contact us or drop us a line in the comments section. We will be happy to help you solve various programming-related issues.
Author’s Bio:
Kevin is a professional educator and a private tutor with over 8 years of experience. He is also a content writer for various blogs about higher education, entertainment, social media & blogging. During his off time, Kevin enjoys traveling and cooking. Feel free to connect with him on Twitter, Linkedin & Google+.