From d836c0bb1bdd5eef29813e66aa54c6445831ba6c Mon Sep 17 00:00:00 2001 From: devblin Date: Fri, 14 Oct 2022 01:09:57 +0530 Subject: [PATCH 1/3] added airbrake adapter support --- src/Logger/Adapter/Airbrake.php | 162 ++++++++++++++++++++++++++++++++ tests/LoggerTest.php | 8 +- 2 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 src/Logger/Adapter/Airbrake.php diff --git a/src/Logger/Adapter/Airbrake.php b/src/Logger/Adapter/Airbrake.php new file mode 100644 index 0000000..86c660a --- /dev/null +++ b/src/Logger/Adapter/Airbrake.php @@ -0,0 +1,162 @@ +airbrakeAPIHost = 'https://api.airbrake.io'; + + $configChunks = \explode(';', $configKey); + $this->projectId = $configChunks[0]; + $this->projectKey = $configChunks[1]; + + $this->airbrakeCreateNoticeURL = $this->airbrakeAPIHost . "/api/v3/projects/" . $this->projectId . "/notices?key=" . $this->projectKey; + } + + /** + * Unique adapter name + * + * @return string + */ + public static function getName(): string + { + return "airbrake"; + } + + /** + * Push log to airbrake + * + * @param Log $log + * @return int Response status code + * @throws Exception When status code is >= 400 + */ + public function push(Log $log): int + { + $breadcrumbObjects = $log->getBreadcrumbs(); + $userLog = $log->getUser(); + $user = [ + "id" => $userLog->getId(), + "name" => $userLog->getUsername(), + "email" => $userLog->getEmail() + ]; + $context = [ + "hostname" => $log->getServer(), + "environment" => $log->getEnvironment(), + "severity" => $log->getType(), + "version" => $log->getVersion(), + "user" => $user, + "action" => $log->getAction() + ]; + $errors = []; + + foreach ($breadcrumbObjects as $breadcrumbObject) { + $error = [ + "type" => $breadcrumbObject->getType(), + "message" => $breadcrumbObject->getMessage() + ]; + + \array_push($errors, $error); + } + + $requestBody = [ + "errors" => $errors, + "context" => $context, + 'lastNoticeAt' => $log->getTimestamp() + ]; + + $curlHandler = \curl_init(); + + $options = array( + CURLOPT_URL => $this->airbrakeCreateNoticeURL, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => \json_encode($requestBody), + CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED, + CURLOPT_HTTPHEADER => array('Content-Type: application/json') + ); + + \curl_setopt_array($curlHandler, $options); + + $result = curl_exec($curlHandler); + + $response = curl_getinfo($curlHandler, \CURLINFO_HTTP_CODE); + + if ($result && $response >= 400) { + throw new Exception("Log could not be pushed with status code " . $response . ": " . \curl_error($curlHandler)); + } + + \curl_close($curlHandler); + + return $response; + } + + public function getSupportedTypes(): array + { + return [ + Log::TYPE_DEBUG, + Log::TYPE_INFO, + Log::TYPE_WARNING, + Log::TYPE_ERROR, + ]; + } + + public function getSupportedEnvironments(): array + { + return [ + Log::ENVIRONMENT_STAGING, + Log::ENVIRONMENT_PRODUCTION, + ]; + } + + public function getSupportedBreadcrumbTypes(): array + { + return [ + Log::TYPE_INFO, + Log::TYPE_DEBUG, + Log::TYPE_WARNING, + Log::TYPE_ERROR + ]; + } +} diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index bece437..d33524e 100755 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -12,7 +12,7 @@ */ use PHPUnit\Framework\TestCase; - +use Utopia\Logger\Adapter\Airbrake; use Utopia\Logger\Adapter\AppSignal; use Utopia\Logger\Adapter\LogOwl; use Utopia\Logger\Adapter\Raygun; @@ -190,5 +190,11 @@ public function testAdapters() $logger = new Logger($adapter); $response = $logger->addLog($log); $this->assertEquals(200, $response); + + // Test Airbrake + $adapter = new Airbrake(\getenv("TEST_AIRBRAKE_ID") . ';' . \getenv("TEST_AIRBRAKE_KEY")); + $logger = new Logger($adapter); + $response = $logger->addLog($log); + $this->assertEquals(201, $response); } } From 6ebd8999c0cb9cae79510146ad7b15d17e33f007 Mon Sep 17 00:00:00 2001 From: devblin Date: Fri, 14 Oct 2022 01:20:37 +0530 Subject: [PATCH 2/3] added airbrake in logger providers --- src/Logger/Logger.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index d81a5db..431ea38 100644 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -11,7 +11,8 @@ class Logger "raygun", "sentry", "appSignal", - "logOwl" + "logOwl", + "airbrake" ]; /** From e7aabd80be56db6ee43ae4523cb99ec09354366a Mon Sep 17 00:00:00 2001 From: devblin Date: Fri, 14 Oct 2022 01:25:16 +0530 Subject: [PATCH 3/3] updated readme with airbrake details --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 3f182ac..cce19b9 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,10 @@ $adapter = new LogOwl("[YOUR_SERVICE_TICKET]"); $logger = new Logger($adapter); $logger->addLog($log); +// Airbrake +$adapter = new Airbrake("[YOUR_PROJECT_ID];[YOUR_PROJECT_KEY]"); +$logger = new Logger($adapter); +$logger->addLog($log); ``` ### Adapters @@ -85,6 +89,7 @@ Below is a list of supported adapters, and thier compatibly tested versions alon | AppSignal | ✅ | | Raygun | ✅ | | Log Owl | ✅ | +| Airbrake | ✅ | ` ✅ - supported, 🛠 - work in progress`