Skip to content

Commit 3431280

Browse files
authored
Merge pull request #10 from bona-kim/exclude-endpoints
Add a configuration to exclude specific endpoints
2 parents 1e933f2 + 47f0589 commit 3431280

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

Config/SystemConfiguration.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
class SystemConfiguration implements LogConfiguration
1111
{
12-
const XML_LOG_FILE_NAME = 'system/api_log/file';
13-
const XML_LOG_LEVEL = 'system/api_log/level';
14-
const XML_ADD_SLASHES = 'system/api_log/add_slashes';
12+
public const XML_LOG_FILE_NAME = 'system/api_log/file';
13+
public const XML_LOG_LEVEL = 'system/api_log/level';
14+
public const XML_ADD_SLASHES = 'system/api_log/add_slashes';
15+
public const XML_ENDPOINTS_TO_EXCLUDE = 'system/api_log/endpoints_to_exclude';
1516

16-
const LOG_DIR = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
17+
public const LOG_DIR = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
1718

1819
/**
1920
* @var ScopeConfigInterface
@@ -39,4 +40,14 @@ public function getAddSlashes(): bool
3940
{
4041
return (bool)$this->config->getValue(self::XML_ADD_SLASHES);
4142
}
43+
44+
public function getEndpointToExclude(): array
45+
{
46+
$endPoints = $this->config->getValue(self::XML_ENDPOINTS_TO_EXCLUDE);
47+
if (null === $endPoints || '' === trim($endPoints)) {
48+
return [];
49+
}
50+
51+
return array_map("trim", preg_split('/\R/', trim($endPoints)));
52+
}
4253
}

Plugin/RestApiLog.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ public function aroundDispatch(
3535
callable $proceed,
3636
HttpRequest $request
3737
) {
38-
$time_pre = microtime(true);
3938
$response = $proceed($request);
39+
if ($this->IsEndpointToExclude($request->getRequestUri())) {
40+
return $response;
41+
}
42+
43+
$time_pre = microtime(true);
4044
list($responseStatusCode, $responseBody) = $this->getResponseData($response);
4145

4246
$this->logger->info(sprintf(
@@ -77,4 +81,19 @@ private function parseMessage(string $message): string
7781

7882
return ($this->configuration->getAddSlashes()) ? addslashes($message) : $message;
7983
}
84+
85+
private function isEndpointToExclude(string $endpoint): bool
86+
{
87+
if (0 >= count($this->configuration->getEndpointToExclude())) {
88+
return false;
89+
}
90+
91+
$combinedString = implode('|', $this->configuration->getEndpointToExclude());
92+
$combinedString = addcslashes($combinedString, '/');
93+
if (preg_match('/(' . $combinedString . ')/i', $endpoint)) {
94+
return true;
95+
}
96+
97+
return false;
98+
}
8099
}

etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<label>Add (back)slashes for the request and response messages</label>
2121
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
2222
</field>
23+
<field id="endpoints_to_exclude" translate="label" type="textarea" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0">
24+
<label>Endpoints to exclude</label>
25+
<comment>Separate endpoints by putting them on new lines.</comment>
26+
</field>
2327
</group>
2428
</section>
2529
</system>

0 commit comments

Comments
 (0)