How to log all rest api calls in magento2?
-
07-10-2020 - |
Question
Is there any way to log all the requested rest API with details (requested URL, methods, parameters, time etc.,) in Magento2?
earlier I have logged all the rest API which was related to products using model (V1.php) in Magento 1. but in magento2 all the methods called in the same location for both API and backend usage to create/update/get.
here how can I know is the request from API?
Please suggest any solution to log APIs.
Thanks
Solution
[This may not be a good way to log the Rest API]
We should try with Plugin - Magento\Webapi\Controller\Rest::dispatch()
:
app/code/Vendor/WebApiLog/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Webapi\Controller\Rest">
<plugin name="rest-api-log"
type="Vendor\WebApiLog\Model\Plugin\RestApiLog"/>
</type>
</config>
app/code/Vendor/WebApiLog/Model/Plugin/RestApiLog.php
<?php
namespace Vendor\WebApiLog\Model\Plugin;
class RestApiLog
{
public function beforeDispatch(
\Magento\Webapi\Controller\Rest $subject,
\Magento\Framework\App\RequestInterface $request
)
{
$request->getModuleName();
$request->getActionName();
$request->getPathInfo();
}
}
OTHER TIPS
You can use the following FOSS module:
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange