Skip to content

Commit 014b5b1

Browse files
authored
Merge pull request #1 from igorwulff/igorwulff-patch-2
Add created_at index to schema Increase clean batch_size Avoid extra db update call by only saving the api log in the after pl… Added configuration for ajax calls Added config method for disabled ajax calls Update FrontControllerDispatchAfter.php Update FrontControllerDispatchBefore.php
2 parents 34a1cab + c05fb47 commit 014b5b1

7 files changed

Lines changed: 47 additions & 23 deletions

File tree

Model/Clean.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function execute()
7171
$collection = $this->logCollectionFactory->create();
7272
$collection = $collection->addFieldToSelect(LogResourceModel::LOG_ID)
7373
->addFieldToFilter(LogResourceModel::CREATED_AT, ['lt' => $datetime])
74-
->setPageSize(2);
74+
->setPageSize(1000);
7575

7676
$pageCount = $collection->getLastPageNumber();
7777
$currentPage = 1;

Model/Config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Config
1818
*/
1919
protected const WEBAPI_LOGS_IS_ENABLED_CONFIG_PATH = 'webapi_logs/log/enabled';
2020

21+
22+
protected const WEBAPI_LOGS_DISABLED_AJAX_CALLS = 'webapi_logs/log/disable_ajax_calls';
2123
/**
2224
* string
2325
*/
@@ -58,6 +60,15 @@ public function isEnabled(): bool
5860
);
5961
}
6062

63+
public function isAjaxCallsDisabled(): bool
64+
{
65+
return $this->scopeConfig->isSetFlag(
66+
self::WEBAPI_LOGS_DISABLED_AJAX_CALLS,
67+
ScopeInterface::SCOPE_WEBSITE
68+
);
69+
}
70+
71+
6172
/**
6273
* @return bool
6374
*/

Model/LogHandle.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,22 @@ public function before(
7878
string $requestBody,
7979
string $requestDateTime
8080
) {
81-
try {
82-
if ($this->config->isSecretMode()) {
83-
$requestorIp = $this->secretParser->parseIp();
84-
$requestHeaders = $this->secretParser->parseHeades($requestHeaders);
85-
$requestBody = $this->secretParser->parseBody($requestBody);
86-
}
87-
88-
$log = $this->logFactory->create();
89-
$log->setData([
90-
'request_method' => $requestMethod,
91-
'requestor_ip' => $requestorIp,
92-
'request_url' => $requestPath,
93-
'request_headers' => $requestHeaders,
94-
'request_body' => $requestBody,
95-
'request_datetime' => $requestDateTime
96-
]);
97-
$this->logResourceModel->save($log);
98-
$this->lastLog = $log;
99-
} catch (Exception $exception) {
100-
$this->logger->error(__('Cant complete webapi log save because of error: %1', $exception->getMessage()));
81+
if ($this->config->isSecretMode()) {
82+
$requestorIp = $this->secretParser->parseIp();
83+
$requestHeaders = $this->secretParser->parseHeades($requestHeaders);
84+
$requestBody = $this->secretParser->parseBody($requestBody);
10185
}
86+
87+
$log = $this->logFactory->create();
88+
$log->setData([
89+
'request_method' => $requestMethod,
90+
'requestor_ip' => $requestorIp,
91+
'request_url' => $requestPath,
92+
'request_headers' => $requestHeaders,
93+
'request_body' => $requestBody,
94+
'request_datetime' => $requestDateTime
95+
]);
96+
$this->lastLog = $log;
10297
}
10398

10499
/**

Plugin/FrontControllerDispatchAfter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ public function __construct(
5454
*/
5555
public function afterDispatch(Rest $subject, $result, RequestInterface $request)
5656
{
57-
if ($this->config->isEnabled()) {
57+
if ($this->config->isEnabled()
58+
&& (
59+
!$request->isXmlHttpRequest()
60+
|| !$this->config->isAjaxCallsDisabled()
61+
)
62+
) {
5863
$exceptions = $result->getException();
5964

6065
if (!empty($exceptions)) {

Plugin/FrontControllerDispatchBefore.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ public function __construct(
5353
*/
5454
public function beforeDispatch(Rest $subject, RequestInterface $request)
5555
{
56-
if ($this->config->isEnabled()) {
56+
if ($this->config->isEnabled()
57+
&& (
58+
!$request->isXmlHttpRequest()
59+
|| !$this->config->isAjaxCallsDisabled()
60+
)
61+
) {
5762
$requestMethod = $request->getMethod();
5863
$requestorIp = $request->getClientIp();
5964
$requestPath = $request->getUriString();

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<label>Enable Webapi Logs</label>
1818
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1919
</field>
20+
<field id="disable_ajax_calls" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
21+
<label>Disable Webapi logs for AJAX calls</label>
22+
<comment><![CDATA[Ajax calls are used in cases of the checkout for example]]></comment>
23+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
24+
</field>
2025
<field id="secret_mode" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
2126
<label>Enable Secret Mode</label>
2227
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>

etc/db_schema.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424
<index referenceId="WEBAPILOGS_ENTITY_ID_ENTITY_ID" indexType="btree">
2525
<column name="log_id"/>
2626
</index>
27+
<index referenceId="WEBAPILOGS_CREATED_AT_CREATED_AT" indexType="btree">
28+
<column name="created_at"/>
29+
</index>
2730
</table>
2831
</schema>

0 commit comments

Comments
 (0)