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.
►►►► Please visit our products: Magento POS, BigCommerce POS, Shopify POS, Woocommerce POS, Restaurant POS, NetSuite POS, Thailand POS, South Africa POS and Commercetools POS, system integration services, workflow management software, enterprise integration platform

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
$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/
►►►► Dịch vụ liên quan của chúng tôi: BackOrder, Bigcommerce automation, Bigcommerce product recommendation, Phần Mềm Logistics, Hệ Thống MES, Phần Mềm Quản Lý Nhân Sự, Phần Mềm CRM, Phần Mềm Quản Lý Tuyển Dụng, Hệ Thống Văn Phòng Điện Tử