Logging From PHP

This feature requires AppSignal Collector version 0.7.0 or higher.

This documentation outlines how to configure logging with the AppSignal for PHP integration.

Configure Logging

Do not send Personal Identifiable Information (PII) to AppSignal. Filter PII (e.g., names, emails) from logs and use an ID, hash, or pseudonymized identifier instead.

For HIPAA-covered entities, more information about signing a Business Associate Agreement (BAA) can be found in our Business Add-Ons documentation.

You do not need to create a log source to send logs from the AppSignal for PHP integration. An "application" log source will be created automatically.

The PHP integration uses OpenTelemetry's logging capabilities to send logs to AppSignal.

Stand-alone usage

To use logging with OpenTelemetry in PHP, you need to set up the logger using the OpenTelemetry LoggerProvider.

PHP
<?php use OpenTelemetry\API\Globals; use OpenTelemetry\API\Logs\LogRecord; use OpenTelemetry\API\Logs\Severity; // Get the logger from the global logger provider $loggerProvider = Globals::loggerProvider(); $logger = $loggerProvider->getLogger('my-app'); // Log a message $logger->emit( (new LogRecord('Log message line')) ->setSeverityNumber(Severity::INFO) );

Usage with Monolog

Monolog is the most popular logging library for PHP. You can configure Monolog to send logs to AppSignal using the auto-instrumentation package.

If you're using the Laravel framework, the Laravel auto-instrumentation package will automatically instrument Laravel's logging facade to send logs to AppSignal. You do not need to configure Monolog directly.

Install the Monolog OpenTelemetry handler package:

Shell
composer require open-telemetry/opentelemetry-logger-monolog

Configure Monolog with the OpenTelemetry handler to send logs to AppSignal:

PHP
<?php use OpenTelemetry\API\Globals; use OpenTelemetry\Contrib\Logs\Monolog\Handler; use Monolog\Logger; // Get the global logger provider $loggerProvider = Globals::loggerProvider(); // Create the OpenTelemetry handler $handler = new Handler($loggerProvider, 'info', true); // Create Monolog logger with the OpenTelemetry handler $logger = new Logger('app', [$handler]); $logger->info('Log message with context', ['user_id' => 123]); $logger->warning('Something might be wrong'); $logger->error('An error occurred', ['error_code' => 500]);

Usage

Sending Logs

Using the OpenTelemetry logger directly, you can define the severity level of your logs:

PHP
<?php use OpenTelemetry\API\Globals; use OpenTelemetry\API\Logs\LogRecord; use OpenTelemetry\API\Logs\Severity; // Get the logger from the global logger provider $loggerProvider = Globals::loggerProvider(); $logger = $loggerProvider->getLogger('my-app'); // Different severity levels $logger->emit( (new LogRecord("Something's gone terribly wrong here")) ->setSeverityNumber(Severity::WARN) ); $logger->emit( (new LogRecord('User action completed successfully')) ->setSeverityNumber(Severity::INFO) ); $logger->emit( (new LogRecord('Database connection failed')) ->setSeverityNumber(Severity::ERROR) );

You can define custom attributes to send log information that can be used when filtering and querying logs:

PHP
<?php use OpenTelemetry\API\Globals; use OpenTelemetry\API\Logs\LogRecord; use OpenTelemetry\API\Logs\Severity; function processInvoice($customer) { $loggerProvider = Globals::loggerProvider(); $logger = $loggerProvider->getLogger('invoice_helper'); $logger->emit( (new LogRecord('Generating invoice for customer')) ->setSeverityNumber(Severity::INFO) ->setAttributes(['customer_id' => $customer->getId()]) ); }

You can query and filter on message contents and attribute values from within the Log Management tool.

Once configured, the desired attributes will be sent to AppSignal as log tags, and be queryable in the AppSignal logging interface.

Structured Logging

OpenTelemetry supports structured logging out of the box. You can send structured data as log attributes:

PHP
<?php $logger->emit( (new LogRecord('User action completed')) ->setSeverityNumber(Severity::INFO) ->setAttributes([ 'user_id' => $userId, 'action' => 'purchase', 'product_id' => $productId, 'amount' => $amount, 'currency' => 'USD' ]) );

Filtering Logs

You can use the ignore_logs configuration option to ignore log lines. See our Ignore Logs guide to learn more.

Need Help?

After configuring your PHP application to send logs, logs should appear in AppSignal. If you are unsure about this step or AppSignal is not receiving any logs, you can always reach out for assistance. We'll help get you back on track!