6

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 of the API which was related to products using the 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 if the request from API?

Please suggest any solution to log APIs.

Thanks

4 Answers 4

7

[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();

    }
}
3
  • how to get the params in this dispatch method, i tried $request->getParams(); it returns the empty array. even $request->getModuleName(); $request->getActionName(); these two methods also returns null value.
    – Sathish
    Mar 30, 2017 at 7:49
  • @Sathish all of the methods above returns null value? Apr 2, 2017 at 14:56
  • 3
    not all some of the methods. i used $request->getContent() to collect the inputs. thanks
    – Sathish
    Apr 4, 2017 at 6:40
5

You can use the following FOSS module:

https://github.com/vladflonta/magento2-webapi-log

1
  • Does not seem to work on Magento 2.4 at this time. Jan 21, 2021 at 3:41
1

Check this out, under MIT license:

https://github.com/ghostunicorns/module-webapi-logs

enter image description here

1
  • Thanks. I just needed something like that for Magento 2.4 :-). Dec 16, 2021 at 12:50
0

You can also check this light weight API logging module https://github.com/mosesdinakaran/magento-log

enter image description here

It has the below features

  • Able to log all the API logs
  • Able to log logged in users API calls
  • Able to log guest user API calls
  • Able to log only a specific api calls such as custom api call, cart, CMS, Product, Order etc
  • Able to log a specific user api calls.
  • The API logs contain both the request and Response
  • The logs are made in a separate file
  • This extension doesn't override any CORE Api class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.