Hello, Magento Folks,
Last time we learned how you can load products from ID and SKU. Today, we are going to talk about how you can create a custom log file using a module in Magento 2.
Logging is an essential part that provides insights for various system-related processes in Magento 2. With log data, you can easily track errors, important events, exceptions, and more. When we are creating a website or a module on Magento 2, it is very important that we also create a log file to monitor/debug the errors.
Default Magento 2 comes with built logging components based on the monolog library. But today, we are going to see how you can create a custom log file using module programmatically in Magento 2. So, without further ado, let’s get started.
First, create a custom log file at the below path:
1.create file at given below path:
app\code\Vendor\Extension\etc\module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Extension" setup_version="1.0.0" schema_version="1.0.0" /> </config>
2.create second file at given below path:
app\code\Vendor\Extension\etc\di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <type name="Vendor\Extension\Logger\Handler"> <arguments> <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument> </arguments> </type> <type name="Vendor\Extension\Logger\Logger"> <arguments> <argument name="name" xsi:type="string">Extension</argument> <argument name="handlers" xsi:type="array"> <item name="system" xsi:type="object">Vendor\Extension\Logger\Handler</item> </argument> </arguments> </type> </config>
3.create Third file at given below path:
app\code\Vendor\Extension\Logger\Handler.php
<?php namespace Vendor\Extension\Logger; use Monolog\Logger; class Handler extends \Magento\Framework\Logger\Handler\Base { protected $loggerType = Logger::INFO; protected $fileName = '/var/log/vendor_extension_log_file.log'; }
4.create fourth file at given below path:
app\code\Vendor\Extension\Logger\Logger.php
<?php namespace Vendor\Extension\Logger; class Logger extends \Monolog\Logger { }
5.create fifth file at given below path:
app\code\Vendor\Extension\etc\crontab.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> <group id="default"> <job name="custom_cronjob" instance="Vendor\Extension\Cron\Cronrun" method="execute"> <schedule>0 */12 * * *</schedule> </job> </group> </config>
6.create sixth file at given below path:
app\code\Vendor\Extension\Cron\Cronrun.php
<?php namespace Vendor\Extension\Cron; class Cronrun { protected $_logger; public function __construct( \Vendor\Extension\Logger\Logger $logger ) { $this->_logger = $logger; } public function execute() { $this->_logger->info(“ Hello This is our Custom log file example”); } }
After running this module, Your file will be generated on below path -Magento2_root_directory\var\log\vendor_extension_log_file.log
So, this was it for the day. You can implement and customize these codes right away as per your needs. If you face any problem while implementation, then you can get in touch with us at our support desk.
Lastly, please share this with your Magento partners and let us know what they feel about this.
Happy Coding!
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…