diff --git a/Model/Clean.php b/Model/Clean.php index de26a75..7124e51 100644 --- a/Model/Clean.php +++ b/Model/Clean.php @@ -71,7 +71,7 @@ public function execute() $collection = $this->logCollectionFactory->create(); $collection = $collection->addFieldToSelect(LogResourceModel::LOG_ID) ->addFieldToFilter(LogResourceModel::CREATED_AT, ['lt' => $datetime]) - ->setPageSize(2); + ->setPageSize(1000); $pageCount = $collection->getLastPageNumber(); $currentPage = 1; diff --git a/Model/Config.php b/Model/Config.php index f6283cb..e7c912f 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -18,6 +18,8 @@ class Config */ protected const WEBAPI_LOGS_IS_ENABLED_CONFIG_PATH = 'webapi_logs/log/enabled'; + + protected const WEBAPI_LOGS_DISABLED_AJAX_CALLS = 'webapi_logs/log/disable_ajax_calls'; /** * string */ @@ -33,6 +35,11 @@ class Config */ protected const WEBAPI_LOGS_LOG_CLEAN_OLDER_THAN_HOURS = 'webapi_logs/log/clean_older_than_hours'; + /** + * @var string + */ + protected const WEBAPI_LOGS_LOG_FILTER_REQUEST_PATHS = 'webapi_logs/log/filter_request_paths'; + /** * @var ScopeConfigInterface */ @@ -58,6 +65,15 @@ public function isEnabled(): bool ); } + public function isAjaxCallsDisabled(): bool + { + return $this->scopeConfig->isSetFlag( + self::WEBAPI_LOGS_DISABLED_AJAX_CALLS, + ScopeInterface::SCOPE_WEBSITE + ); + } + + /** * @return bool */ @@ -91,4 +107,19 @@ public function getCleanOlderThanHours(): int ScopeInterface::SCOPE_WEBSITE ); } + + /** + * @return array + */ + public function getFilterRequestPaths(): array + { + $value = $this->scopeConfig->getValue( + self::WEBAPI_LOGS_LOG_FILTER_REQUEST_PATHS, + ScopeInterface::SCOPE_WEBSITE + ); + if ($value == null) { + return []; + } + return preg_split('/\n|\r\n?/', $value); + } } diff --git a/Model/LogHandle.php b/Model/LogHandle.php index af6d6c1..1522536 100644 --- a/Model/LogHandle.php +++ b/Model/LogHandle.php @@ -78,26 +78,26 @@ public function before( string $requestBody, string $requestDateTime ) { - try { - if ($this->config->isSecretMode()) { - $requestorIp = $this->secretParser->parseIp(); - $requestHeaders = $this->secretParser->parseHeades($requestHeaders); - $requestBody = $this->secretParser->parseBody($requestBody); - } + $filter = $this->config->getFilterRequestPaths(); + + // Only log this request if the path is not filtered. + if (!$this->filterRequestPath($requestPath, $filter)) { + if ($this->config->isSecretMode()) { + $requestorIp = $this->secretParser->parseIp(); + $requestHeaders = $this->secretParser->parseHeades($requestHeaders); + $requestBody = $this->secretParser->parseBody($requestBody); + } - $log = $this->logFactory->create(); - $log->setData([ - 'request_method' => $requestMethod, - 'requestor_ip' => $requestorIp, - 'request_url' => $requestPath, - 'request_headers' => $requestHeaders, - 'request_body' => $requestBody, - 'request_datetime' => $requestDateTime - ]); - $this->logResourceModel->save($log); - $this->lastLog = $log; - } catch (Exception $exception) { - $this->logger->error(__('Cant complete webapi log save because of error: %1', $exception->getMessage())); + $log = $this->logFactory->create(); + $log->setData([ + 'request_method' => $requestMethod, + 'requestor_ip' => $requestorIp, + 'request_url' => $requestPath, + 'request_headers' => $requestHeaders, + 'request_body' => $requestBody, + 'request_datetime' => $requestDateTime + ]); + $this->lastLog = $log; } } @@ -128,4 +128,26 @@ public function after( $this->logger->error(__('Cant complete webapi log save because of error: %1', $exception->getMessage())); } } + + /** + * Check if request path is among the filters. + * + * @param string $requestPath + * @param array $filters + * + * @return bool + */ + private function filterRequestPath( + string $requestPath, + array $filters + ): bool { + foreach ($filters as $filter) { + if ($filter != '') { + if (stripos($requestPath, $filter) !== false) { + return true; + } + } + } + return false; + } } diff --git a/Plugin/FrontControllerDispatchAfter.php b/Plugin/FrontControllerDispatchAfter.php index 1f194ae..119e02c 100644 --- a/Plugin/FrontControllerDispatchAfter.php +++ b/Plugin/FrontControllerDispatchAfter.php @@ -54,7 +54,12 @@ public function __construct( */ public function afterDispatch(Rest $subject, $result, RequestInterface $request) { - if ($this->config->isEnabled()) { + if ($this->config->isEnabled() + && ( + !$request->isXmlHttpRequest() + || !$this->config->isAjaxCallsDisabled() + ) + ) { $exceptions = $result->getException(); if (!empty($exceptions)) { diff --git a/Plugin/FrontControllerDispatchBefore.php b/Plugin/FrontControllerDispatchBefore.php index 35350e6..bf9d79d 100644 --- a/Plugin/FrontControllerDispatchBefore.php +++ b/Plugin/FrontControllerDispatchBefore.php @@ -53,7 +53,12 @@ public function __construct( */ public function beforeDispatch(Rest $subject, RequestInterface $request) { - if ($this->config->isEnabled()) { + if ($this->config->isEnabled() + && ( + !$request->isXmlHttpRequest() + || !$this->config->isAjaxCallsDisabled() + ) + ) { $requestMethod = $request->getMethod(); $requestorIp = $request->getClientIp(); $requestPath = $request->getUriString(); diff --git a/README.md b/README.md index 24dc0ff..b3e0143 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This module allows you to analyze all the webapi rest done call toward your mage # Install -`composer require ghostunicorns/module-webapi-logs` +`composer require youwe/module-webapi-logs` # Configure diff --git a/composer.json b/composer.json index 5142418..ed2b914 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "ghostunicorns/module-webapi-logs", + "name": "youwe/module-webapi-logs", "description": "Magento Webapi Log Details accessible via backend", "type": "magento2-module", "require": { diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 5dc6011..8a6f569 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -17,6 +17,11 @@ Magento\Config\Model\Config\Source\Yesno + + + + Magento\Config\Model\Config\Source\Yesno + Magento\Config\Model\Config\Source\Yesno @@ -39,6 +44,13 @@ validate-number validate-greater-than-zero + + + + 1 + + + diff --git a/etc/db_schema.xml b/etc/db_schema.xml index 4e20fc6..ad97d62 100644 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -24,5 +24,8 @@ + + +