Skip to main content

PHP

This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

Prerequisites

  • PHP 7.4+ for manual instrumentation or PHP 8.0+ for auto-instrumentation.
note

If you need an example aplication to test this integration, please refer to our PHP OpenTelemetry repository.

Configure the instrumentation

Install the necessary extensions and dependencies

To enable OpenTelemetry logging in PHP, follow these steps:

  1. Install the OpenTelemetry PHP extension using PECL:

    pecl install opentelemetry
  2. Add the OpenTelemetry extension to your php.ini file:

    [opentelemetry]
    extension=opentelemetry.so
  3. Install the necessary Composer packages:

    composer require \
    monolog/monolog \
    open-telemetry/opentelemetry-logger-monolog
  4. Set OpenTelemetry environment variables:

    Add the following OpenTelemetry environment variables in your PHP environment or programmatically in your PHP code:

    // Set OpenTelemetry environment variables programmatically
    putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
    putenv('OTEL_LOGS_EXPORTER=otlp');
    putenv('OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=http/protobuf');
    putenv('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://otlp-listener.logz.io/v1/logs');
    putenv('OTEL_EXPORTER_OTLP_LOGS_HEADERS=Authorization=Bearer <<LOG-SHIPPING-TOKEN>>,user-agent=logzio-php-logs-otlp');
    putenv('OTEL_RESOURCE_ATTRIBUTES=service.name=<YOUR-SERVICE-NAME>');

    Replace <YOUR-SERVICE-NAME> with the required service name.

    Your Logz.io log shipping token directs the data securely to your Logz.io Log Management account. The default token is auto-populated in the examples when you're logged into the Logz.io app as an Admin. Manage your tokens.

  5. Logging integration with Monolog:

    After setting up the environment variables, you can integrate OpenTelemetry with Monolog for logging. Below is an example that demonstrates how to configure and use Monolog with OpenTelemetry:

    <?php

    use Monolog\Logger;
    use OpenTelemetry\Contrib\Logs\Monolog\Handler as MonologHandler;
    use OpenTelemetry\SDK\Logs\LoggerProviderFactory;
    use Psr\Log\LogLevel;

    require __DIR__ . '/vendor/autoload.php';

    // Initialize LoggerProviderFactory
    $loggerFactory = new LoggerProviderFactory();
    $loggerProvider = $loggerFactory->create();
    $handler = new MonologHandler(
    $loggerProvider,
    LogLevel::DEBUG,
    );

    // Initialize Monolog
    $monolog = new Logger('otel-logger', [$handler]);

    // Example logging with Monolog
    $monolog->info('This is an informational log message');
    $monolog->error('This is an error log message');

    // Shutdown the logger provider after logging
    $loggerProvider->shutdown();
  6. Run the application:

    You can now run the PHP application and generate logs:

    php your_script.php

Replace your_script.php with the name of your PHP script. This will send the logs to Logz.io via the OpenTelemetry OTLP endpoint.

Check Logz.io for your logs

Allow some time for data ingestion, then open Open Search Dashboards.

Encounter an issue? See our log shipping troubleshooting guide.