Logging is an important part of every development process. Logs provide insights into Magento 2 system processes and are a great tool for tracking errors, critical events, and more. For the logging library to work with Magento 2, it must be compatible with the PSR-3 standards. Magento Agency 2 logging comes out with an integrated logging solution based on the Monolog Library, which will be further analyzed in this SmartOSC article.
Magento 2 logging
Magento 2 main log class is “Magento\Framework\Logger\Monolog” and is formatted in “MAGENTO2_ROOT/app/etc/di.xml”.
<preference for=”Psr\Log\LoggerInterface” type=”Magento\Framework\Logger\Monolog”> |
As mentioned before, Monolog is a logging tool that allows users to use a range of handlers to their advantage, such as logging to files and system logs, sending notifications and emails. , logging database activity, etc.
Logger class has 8 log levels with corresponding methods for each level, the use of these methods depends on the type of message the user is using.
Each of these methods accepts 2 arguments, the first being the message itself (the string) and the second an array of options (e.g. the Exception object).
$this->_logger-> emergency($message, array $context = array()); //saved in var/log/system.log
$this->_logger-> alert($message, array $context = array()) //saved in var/log/system.log
$this->_logger-> critical($message, array $context = array()) //saved in var/log/system.log
$this->_logger-> error($message, array $context = array()) //saved in var/log/system.log
$this->_logger-> warning($message, array $context = array()) //saved in var/log/system.log
$this->_logger-> notice($message, array $context = array()) //saved in var/log/system.log
►►►► Please visit our products: Magento POS, BigCommerce POS, Shopify POS, Woocommerce POS, NetSuite POS, Mobile POS, White label POS, Reseller POS, POS System for Retail and Commercetools POS
$this->_logger-> info($message, array $context = array()) //saved in var/log/system.log$this->_logger-> debug($message, array $context = array()) //saved in var/log/debug.log (does not work in production mode)
There is another method that can be used to log something at an arbitrary level with the first parameter role.
$this->_logger-> log($level, $message, array $context = array()) |
All exceptions, regardless of the log level used, are stored in var/log/exception.log
>>>> See more: https://www.smartosc.com/insights/Dich-vu-phat-trien-Magento-tron-goi
Using logger class
Magento 2 uses dependency injection so to use logger in class, logger needs to be passed to class constructor.
Here is an example of how to use a logger in a custom class to log an exception.
class Example
{
private $logger;
public function __construct(\Psr\Log\LoggerInterface $logger) {
$this->logger = $logger;
}
public function exampleMethod()
{
try {
//do something
} catch (\Exception $e) {
$this->logger->critical(‘Error message’, [‘exception’ => $e]);
}
}
}
Notice that we are passing \Psr\Log\LoggerInterface in the normally preferred constructor to LoggerInterface which is set to the Monolog class in di.xml, as mentioned in the preamble.
In some cases, it is possible to pass loggers to constructors such as the “Magento\Framework\View\Element\Template” and “Magento\Framework\Model\AbstractModel” classes. These classes already have a $_logger section so if you extend these classes, one of the logger classes is already there.
=>>>> Refer to the article: https://www.smartosc.com/insights/Huong-dan-chon-lua-cong-ty-Magento-uy-tin
Debugging Magento 2 logging
By default, Magento only writes to the debug log in default and development mode, but not in production mode.
To change the default value in default and development mode the following commands can be used:
1. bin/magento setup:config:set –enable-debug-logging=true|false
2. bin/magento cache:flush
>>>> See more: https://www.smartosc.com/insights/Diem-mat-top-cac-cong-ty-Magento-tai-Vietnam
Magento 2 database logging
Another great function of Magento 2 is database activity logging. By default, database logs are saved in var/debug/db.log. The use of database logging is highly recommended as it allows us to track all database queries and can help us find performance impediments.
Use the following command to enable/disable database logging:
1. bin/magento dev:query-log:enable|disable
2. bin/magento cache:flush
Journaling
As of Magento 2.3.1, logging is extended and is now logged at in var/log/cron.log instead of var/log/system.log
Above are the basics of Magento 2 Logging. Although Magento 2 uses the Monolog logger by default, other logging solutions can be used by setting the option for LoggerInterface as long as it complies with the PSR-3 standard.
There are also many other functions using logging such as creating custom handlers for custom log files, sending emails with log data, sending log records to Slack accounts, etc.
Article source: https://www.smartosc.com/
►►►► Our related services: Bigcommerce BackOrder, Bigcommerce automation, BigCommerce Integration, Automation Template, jobs in Vietnam
Hi there,
We run an Instagram growth service, which increases your number of followers both safely and practically.
– Guaranteed: We guarantee to gain you 300-1000+ followers per month.
– Real, human followers: People follow you because they are interested in your business or niche.
– Safe: All actions are made manually. We do not use any bots.
The price is just $60 (USD) per month, and we can start immediately.
If you are interested, and have any questions, reply back and we can discuss further.
Kind Regards,
Libby
Unsubscribe here: https://removeme.click/unsubscribe.php?d=ecommerceplatformthailand.com
Hi,
We run an Instagram service, where we can increase your followers both safely and effectively.
We don’t use bots – everything is done manually to attract real, targeted followers who care about your content.
The result? Increased brand awareness, higher engagement, and ultimately, more customers.
The price is just $60 (USD) per month.
If you would like more information, get back in touch.
Kind Regards,
Megan
Unsubscribe here: https://removeme.click/unsubscribe.php?d=ecommerceplatformthailand.com
You actually make it appear really easy with your presentation however I in finding this matter to be
actually something that I think I would by no means understand.
It kind of feels too complicated and extremely vast for me.
I’m having a look forward in your subsequent put up, I will try
to get the hold of it! Lista escape roomów