Skip to main content

Logging

The Market Data PHP SDK supports PSR-3 compatible logging for debugging and monitoring API interactions.

Default Logging

The SDK creates a default logger based on the MARKETDATA_LOGGING_LEVEL environment variable:

<?php

// Set logging level via environment variable
putenv('MARKETDATA_LOGGING_LEVEL=debug');

use MarketDataApp\Client;

$client = new Client();

// Debug messages will be logged to stderr
$quote = $client->stocks->quote('AAPL');

Available Log Levels

The SDK supports standard PSR-3 log levels:

LevelDescription
emergencySystem is unusable
alertAction must be taken immediately
criticalCritical conditions
errorError conditions
warningWarning conditions
noticeNormal but significant conditions
infoInformational messages
debugDebug-level messages (most verbose)

Custom Logger

Inject your own PSR-3 compatible logger:

<?php

use MarketDataApp\Client;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;

// Create Monolog logger
$logger = new Logger('marketdata');

// Log to file with rotation
$logger->pushHandler(new RotatingFileHandler(
'/var/log/marketdata/api.log',
maxFiles: 7,
level: Logger::DEBUG
));

// Also log errors to stderr
$logger->pushHandler(new StreamHandler(
'php://stderr',
level: Logger::ERROR
));

// Pass logger to client
$client = new Client(logger: $logger);

$quote = $client->stocks->quote('AAPL');

What Gets Logged

The SDK logs the following events:

Debug Level

  • API request URLs and parameters
  • Response status codes
  • Retry attempts

Info Level

  • Successful API responses
  • Rate limit information

Warning Level

  • Retry attempts after transient failures
  • Deprecated endpoint usage

Error Level

  • API errors and exceptions
  • Authentication failures
  • Network errors

Example Log Output

[2024-01-15 10:30:00] [debug] Making request: GET https://api.marketdata.app/v1/stocks/quotes/?symbol=AAPL
[2024-01-15 10:30:01] [debug] Response received: 200 OK
[2024-01-15 10:30:01] [info] Rate limits - Used: 1, Remaining: 9999, Limit: 10000

Disabling Logging

To disable logging, set the log level to a high threshold or provide a null logger:

<?php

use MarketDataApp\Client;
use Psr\Log\NullLogger;

// Option 1: Use NullLogger
$client = new Client(logger: new NullLogger());

// Option 2: Set high log level via environment
putenv('MARKETDATA_LOGGING_LEVEL=emergency');
$client = new Client();

Production Recommendations

  • Use warning or error level in production to reduce log volume
  • Use debug level only during development or troubleshooting
  • Configure log rotation to prevent disk space issues
  • Consider using structured logging (JSON format) for log aggregation systems