In this article we will explore how to inspect and debug information about events that are being fired. We will also take a look on how we can create a log file with all the events.
A better approach would be to use the native Magento 2 profiler to gather this and a lot more information. For practice purposes or when you simply need to quickly gather some logs and can not afford to fire up the profile - this can come in handy.
Lets get started.
The events are dispatches using the interface which is using the following class \Magento\Framework\Event\Manager. We can hook into the before method and add logs to see the events that are dispatched one each request.
First thing we will need to do is declare our plugin. We will define in it the global dependency injection file.
Once we have our plugin defined we should create the class.
A few things to note in the class:
- We are creating a before plugin for dispatch() method
- We are using Psr\Log\LoggerInterface to add logs to native logging system. For developers coming from Magento 1 this is similar to Mage::log() and can be used for custom logging anywhere. The difference is that we have to inject the class using di.
- Our plugin does not change any arguments so we are returning null
Enable the module, clear caches and you will see logs in your var/log/debug.log
[2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] [] [2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] [] [2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] [] [2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] [] [2021-01-26 20:42:36] main.INFO: EVENT:catalog_category_collection_add_is_active_filter [] [] [2021-01-26 20:42:36] main.INFO: EVENT:eav_collection_abstract_load_before [] [] [2021-01-26 20:42:36] main.INFO: EVENT:catalog_category_collection_load_before [] [] [2021-01-26 20:42:36] main.INFO: EVENT:catalog_category_collection_load_after [] [] [2021-01-26 20:42:36] main.INFO: EVENT:session_abstract_clear_messages [] [] [2021-01-26 20:42:36] main.INFO: EVENT:controller_front_send_response_before [] []