Send Logs Directly to Datadog API Using PHP

{ // deep_execution_view
const authorName = "Ankit Agrawal";
//
const publishDate = "March 10, 2026";

Monitoring and observability are essential for modern applications. One effective way to improve visibility is to send logs directly to Datadog API from your application.

In this guide, you will learn how to send logs directly to Datadog API using PHP. This approach works well for production environments because it provides structured logging, centralized monitoring, and better debugging.

Instead of storing logs only on the server, sending logs to Datadog allows you to analyze events, detect issues quickly, and monitor application health in real time.

Why Send Logs Directly to Datadog API?

Many production systems send logs to centralized monitoring platforms. When you send logs directly to Datadog API, you gain several advantages.

  • Centralized logging for all services
  • Real-time monitoring and alerts
  • Better debugging during production incidents
  • Easy log searching and filtering
  • Integration with metrics and dashboards

Because of these benefits, Datadog has become a popular logging platform for modern infrastructure.

Step 1: Generate Datadog API Key

Before sending logs, you need a Datadog API key. Log in to your Datadog dashboard and navigate to the API key section. Create a new key and store it securely in your application configuration.

Step 2: Datadog Logs HTTP Endpoint

Datadog provides an HTTP endpoint where applications can send logs. Your application will send a POST request containing JSON formatted logs.

The basic endpoint format looks like this:

https://http-intake.logs.datadoghq.com/v1/input/YOUR_DATADOG_API_KEY

Step 3: PHP Function to Send Logs Directly to Datadog API

The following PHP function uses cURL to send logs directly to Datadog API.

function sendLogToDatadog($apiKey, $message, $service = "php-app", $level = "info")
{
    $url = "https://http-intake.logs.datadoghq.com/v1/input/" . $apiKey;

    $payload = [
        "message" => $message,
        "service" => $service,
        "level" => $level,
        "timestamp" => time()
    ];

    $ch = curl_init($url);

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json"
        ],
        CURLOPT_POSTFIELDS => json_encode($payload),
        CURLOPT_TIMEOUT => 10
    ]);

    $response = curl_exec($ch);

    if (curl_errno($ch)) {
        $error = curl_error($ch);
        curl_close($ch);

        return [
            "success" => false,
            "error" => $error
        ];
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    return [
        "success" => $httpCode === 200,
        "status_code" => $httpCode,
        "response" => $response
    ];
}

Step 4: Example Usage

After creating the function, you can send logs to Datadog like this:

$apiKey = "YOUR_DATADOG_API_KEY";

$result = sendLogToDatadog(
    $apiKey,
    "User login successful",
    "authentication-service",
    "info"
);

if ($result["success"]) {
    echo "Log sent successfully";
} else {
    print_r($result);
}

Log Structure Explained

The log payload contains several useful fields that help Datadog categorize logs.

  • message – The actual log message
  • service – Name of your application or microservice
  • level – Log severity such as info, warning, or error
  • timestamp – Unix timestamp of the event

These fields allow Datadog to filter logs efficiently and generate meaningful dashboards.

Best Practices for Production Logging

When sending logs directly to Datadog API, follow these best practices to maintain reliability and performance.

  • Store API keys in environment variables
  • Avoid sending sensitive user data in logs
  • Use structured logging whenever possible
  • Log errors and warnings with appropriate severity levels
  • Monitor log ingestion volume to control costs

When Should You Send Logs Directly to Datadog API?

This approach is especially useful when you need centralized monitoring across distributed systems.

  • SaaS platforms
  • Microservice architectures
  • Cloud native applications
  • High traffic production systems

Conclusion

Now you know how to send logs directly to Datadog API using PHP. By integrating Datadog logging into your application, you can improve observability, detect issues faster, and maintain a stable production environment.

With a simple cURL implementation, PHP applications can easily stream logs to Datadog and benefit from powerful monitoring capabilities.

}