Jun 22, 2014
kalpesh

Magento: Enable logs on API calls

There is no setting to enable/disable API calls in Magento, but you can do so by modifying a file which handles all the API calls and logging the requests in a custom log file. This is useful when you know that there are many API calls being requested by third party applications to your website but not sure what is actually being called, how many times, and at what day and time.

With that said, open the file: app/code/core/Mage/Api/Model/Server/Handler/Abstract.php
and find the function “call” which handles all the API calls. Scroll down the function and find the if block which says:

1
2
3
4
5
6
7
8
9
10
11
if (method_exists($model, $method)) {
        if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
            return $model->$method((is_array($args) ? $args : array($args)));
        } elseif (!is_array($args)) {
            return $model->$method($args);
        } else {
            return call_user_func_array(array(&$model, $method), $args); 
        }       
} else {
        throw new Mage_Api_Exception('resource_path_not_callable');
}

and replace it with:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (method_exists($model, $method)) {
        Mage::log($method, null, 'api.log'); //logs the method
        Mage::log($args, null, 'api.log'); //logs the arguments
        if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
            return $model->$method((is_array($args) ? $args : array($args)));
        } elseif (!is_array($args)) {
            return $model->$method($args);
        } else {
            return call_user_func_array(array(&$model, $method), $args);
        }
} else {
        throw new Mage_Api_Exception('resource_path_not_callable');
}

Now you should have your var/log/api.log file filled with the “method” and “arguments” requested by third party systems on your server. This should help you in knowing what was requested and at what time in your new log file.

1 Comment

  • Great! I was looking for this exactly. Thank you so much.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions