diff --git a/src/Logger/Adapter/Datadog.php b/src/Logger/Adapter/Datadog.php new file mode 100644 index 0000000..78cfc7b --- /dev/null +++ b/src/Logger/Adapter/Datadog.php @@ -0,0 +1,79 @@ +apiKey = $apiKey; + $this->apiEndpoint = "https://http-intake.logs.datadoghq.com/v1/input/{$this->apiKey}"; + } + + public function push(Log $log): int + { + $data = [ + "message" => $log->getMessage(), + "level" => $log->getLevel(), + "timestamp" => $log->getTimestamp(), + "tags" => $log->getTags(), + // Add any additional fields specific to Datadog logging + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->apiEndpoint); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); + + if (curl_errno($ch)) { + throw new \Exception('Error pushing log to Datadog: ' . curl_error($ch)); + } + + curl_close($ch); + + return $statusCode; + } + + 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_WARNING, + Log::TYPE_ERROR, + ]; + } +} diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index 714ae51..619759c 100644 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -13,6 +13,7 @@ class Logger 'sentry', 'appSignal', 'logOwl', + 'datadog', ]; /** diff --git a/tests/e2e/Adapter/DatadogTest.php b/tests/e2e/Adapter/DatadogTest.php new file mode 100644 index 0000000..d20b2e4 --- /dev/null +++ b/tests/e2e/Adapter/DatadogTest.php @@ -0,0 +1,15 @@ +adapter = new Datadog(\getenv('TEST_DATADOG_KEY') ?: ''); + } +}