How to Create Custom Log File in Magento 2

Logging is crucial in e-commerce, aiding developers in error tracking, debugging, and overall system monitoring. You require Magento 2 custom log files to organize and manage specific data on Magento, from promotions, product reviews, and page optimization to improved layered navigation. Here is a guide on how to create a Magento 2 log-to-file tailored to your e-commerce needs within Magento 2. 

Preparation Phase

Prepare Your Module

You can use Magento 2 modules like the Promotional Suite Pro for unique promotions, a product recommendation module like the Automatic Related Product to suggest related items or a customer review module like the Advanced Products Review for in-depth feedback.

Here is how you prepare your chosen module

  • Structure the module in the app/code.

  • Create necessary files like registration.php and module.xml

  • Define the frontend/backend components of the module

  • Set up dependencies in di.xml 

  • Activate the module using the 'bin/magento' commands.

Magento 2 Add Custom Log Phase

Define Your Logger Class

A logger class is a programming component that records and stores events, messages, or errors generated by an application for debugging and analysis. You can use the logger classes and Magento exception logs to manage and monitor application behavior in the e-commerce platform. The logger class handles general logging needs, while the exception log captures and reports errors and exceptions.

Here are five steps to define your module's logger class:
Step 1. Create a PHP file within your custom module's directory.
Step 2. Write a class that handles logging. You can name it something like "CustomLogger."
Step 3. Ensure your CustomLogger class extends the appropriate logger class provided by Magento 2.
Step 4. Specify different log levels like info, debug, error, etc., depending on the type of information you want to log.
Step 5. Use your CustomLogger class to log messages, errors, or events within your module's code.

Create a Handler for Your Logger

The next significant step in developing a Magento-2-create custom log file is the handler class for your logger. The handler determines where and how log entries get stored. Start by choosing a Magento 2 log file location to store your log file, like / var / log / custom.log . Determine the detail level for your logs. Make a PHP file for your handler class, extending Magento's handler base. Set the log file path and the logging level within your handler class. Finally, connect your handler to the custom logger class you created earlier.

Once you have set up your custom logger and handler, you can use Magento 2's programmatically login functionality within your module's code. You can set up the Magento 2 login customer programmatically with a password to log successful or failed login attempts to your custom log file as part of auditing or monitoring user activity.

Configuration and Usage Phase

Configure Dependency Injection

Define dependencies in the di.xml configuration file to configure dependency injection. For instance, for custom logging, specify dependencies for your logger class and handler class, ensuring proper integration within Magento's structure. The process facilitates modular and organized code in Magento.

Using Your Custom Logger

With your custom logger set in Magento 2, write a log to a custom file using your defined custom logger class and handler for your module. You can use the custom log files to

  • Debug your module, allowing you to track the execution flow, variable values, or any issues within your module's code.

  • Log in custom events to a custom file to monitor and analyze specific actions.

  • Capture and investigate errors or exceptions that occur within your module.

  • Gain insights into how your custom business logic functions.

  • Log performance-related data to a custom file, such as execution times or resource usage, to analyze and optimize your module's performance.

View Your Custom Log

In Magento 2, log file locations are typically in the / var / log / . You can view the log file using your preferred text editor or a terminal command.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.