Skip to content

Commit

Permalink
Merge pull request #10 from bona-kim/exclude-endpoints
Browse files Browse the repository at this point in the history
Add a configuration to exclude specific endpoints
  • Loading branch information
stefliekens authored Apr 28, 2023
2 parents 1e933f2 + 47f0589 commit 3431280
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Config/SystemConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

class SystemConfiguration implements LogConfiguration
{
const XML_LOG_FILE_NAME = 'system/api_log/file';
const XML_LOG_LEVEL = 'system/api_log/level';
const XML_ADD_SLASHES = 'system/api_log/add_slashes';
public const XML_LOG_FILE_NAME = 'system/api_log/file';
public const XML_LOG_LEVEL = 'system/api_log/level';
public const XML_ADD_SLASHES = 'system/api_log/add_slashes';
public const XML_ENDPOINTS_TO_EXCLUDE = 'system/api_log/endpoints_to_exclude';

const LOG_DIR = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
public const LOG_DIR = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;

/**
* @var ScopeConfigInterface
Expand All @@ -39,4 +40,14 @@ public function getAddSlashes(): bool
{
return (bool)$this->config->getValue(self::XML_ADD_SLASHES);
}

public function getEndpointToExclude(): array
{
$endPoints = $this->config->getValue(self::XML_ENDPOINTS_TO_EXCLUDE);
if (null === $endPoints || '' === trim($endPoints)) {
return [];
}

return array_map("trim", preg_split('/\R/', trim($endPoints)));
}
}
21 changes: 20 additions & 1 deletion Plugin/RestApiLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public function aroundDispatch(
callable $proceed,
HttpRequest $request
) {
$time_pre = microtime(true);
$response = $proceed($request);
if ($this->IsEndpointToExclude($request->getRequestUri())) {
return $response;
}

$time_pre = microtime(true);
list($responseStatusCode, $responseBody) = $this->getResponseData($response);

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

return ($this->configuration->getAddSlashes()) ? addslashes($message) : $message;
}

private function isEndpointToExclude(string $endpoint): bool
{
if (0 >= count($this->configuration->getEndpointToExclude())) {
return false;
}

$combinedString = implode('|', $this->configuration->getEndpointToExclude());
$combinedString = addcslashes($combinedString, '/');
if (preg_match('/(' . $combinedString . ')/i', $endpoint)) {
return true;
}

return false;
}
}
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<label>Add (back)slashes for the request and response messages</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="endpoints_to_exclude" translate="label" type="textarea" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Endpoints to exclude</label>
<comment>Separate endpoints by putting them on new lines.</comment>
</field>
</group>
</section>
</system>
Expand Down

0 comments on commit 3431280

Please sign in to comment.