From 5eeda6b6825c55d488645fa0aa7c71ac67450506 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 18 Jan 2022 11:51:54 +0100 Subject: [PATCH] new: [localtool:commonConnectorTools] Added new logger for each local tools --- .../CommonConnectorTools.php | 31 +++++++++++++++++++ .../local_tool_connectors/MispConnector.php | 13 ++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Lib/default/local_tool_connectors/CommonConnectorTools.php b/src/Lib/default/local_tool_connectors/CommonConnectorTools.php index 179a238..213db17 100644 --- a/src/Lib/default/local_tool_connectors/CommonConnectorTools.php +++ b/src/Lib/default/local_tool_connectors/CommonConnectorTools.php @@ -2,6 +2,8 @@ namespace CommonConnectorTools; use Cake\ORM\Locator\LocatorAwareTrait; +use Cake\Log\Log; +use Cake\Log\Engine\FileLog; class CommonConnectorTools { @@ -20,6 +22,35 @@ class CommonConnectorTools const STATE_CANCELLED = 'Request cancelled'; const STATE_DECLINED = 'Request declined by remote'; + public function __construct() + { + Log::setConfig("LocalToolDebug", [ + 'className' => FileLog::class, + 'path' => LOGS, + 'file' => "{$this->connectorName}-debug", + 'scopes' => [$this->connectorName], + 'levels' => ['notice', 'info', 'debug'], + ]); + Log::setConfig("LocalToolError", [ + 'className' => FileLog::class, + 'path' => LOGS, + 'file' => "{$this->connectorName}-error", + 'scopes' => [$this->connectorName], + 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], + ]); + + } + + protected function logDebug($message) + { + Log::debug($message, [$this->connectorName]); + } + + protected function logError($message, $scope=[]) + { + Log::error($message, [$this->connectorName]); + } + public function addExposedFunction(string $functionName): void { $this->exposedFunctions[] = $functionName; diff --git a/src/Lib/default/local_tool_connectors/MispConnector.php b/src/Lib/default/local_tool_connectors/MispConnector.php index a021839..4b6c653 100644 --- a/src/Lib/default/local_tool_connectors/MispConnector.php +++ b/src/Lib/default/local_tool_connectors/MispConnector.php @@ -188,6 +188,7 @@ class MispConnector extends CommonConnectorTools $settings = json_decode($connection->settings, true); $http = $this->genHTTPClient($connection, $options); $url = sprintf('%s%s', $settings['url'], $relativeURL); + $this->logDebug(sprintf('%s %s %s', __('Posting data') . PHP_EOL, "POST {$url}" . PHP_EOL, json_encode($data))); return $http->post($url, $data, $options); } @@ -239,14 +240,18 @@ class MispConnector extends CommonConnectorTools if (!empty($params['softError'])) { return $response; } - throw new NotFoundException(__('Could not retrieve the requested resource.')); + $errorMsg = __('Could not post to the requested resource for `{0}`. Remote returned:', $url) . PHP_EOL . $response->getStringBody(); + $this->logError($errorMsg); + throw new NotFoundException($errorMsg); } } private function postData(string $url, array $params): Response { if (empty($params['connection'])) { - throw new NotFoundException(__('No connection object received.')); + $errorMsg = __('No connection object received.'); + $this->logError($errorMsg); + throw new NotFoundException($errorMsg); } $url = $this->urlAppendParams($url, $params); if (!is_string($params['body'])) { @@ -256,7 +261,9 @@ class MispConnector extends CommonConnectorTools if ($response->isOk()) { return $response; } else { - throw new NotFoundException(__('Could not post to the requested resource. Remote returned:') . PHP_EOL . $response->getStringBody()); + $errorMsg = __('Could not post to the requested resource for `{0}`. Remote returned:', $url) . PHP_EOL . $response->getStringBody(); + $this->logError($errorMsg); + throw new NotFoundException($errorMsg); } }