A flexible and configurable logging library for PHP, supporting transactions with unique IDs, customizable date formats, and time zones. This library simplifies tracking logs with transaction IDs and performance timings and implements the PSR-3 specification.
- Supports logging messages with various log levels.
- Provides unique transaction IDs for tracking grouped logs.
- Customizable date and time formats.
- Allows setting a custom time zone.
- Supports PSR-3 log levels for compatibility.
Install the latest version with:
$ composer require borzenkovdev/php-telemetry
<?php
use Telemetry\Telemetry;
use Telemetry\drivers\CliDriver;
use Psr\Log\LogLevel;
$telemetry = new Telemetry(new CliDriver());
// Set log level manually
$telemetry->log(LogLevel::INFO, 'Service started', ['origin' => 'http', 'customerId' => '123']);
// Quick methods for different log levels types
$telemetry->info('Info level log', ['user' => '123']);
$telemetry->debug('Debug level log', ['details' => 'Step 1 completed']);
$telemetry->error('Error level log', ['errorCode' => '404']);
$telemetry->critical('Critical level log', ['errorCode' => '500']);
$telemetry->alert('Alert level log', ['errorCode' => '500']);
$telemetry->emergency('Emergency level log', ['errorCode' => '500']);
<?php
use Telemetry\Telemetry;
use Telemetry\drivers\CliDriver;
use Psr\Log\LogLevel;
$telemetry = new Telemetry(new CliDriver());
$telemetry->beginTransaction();
$telemetry->log(LogLevel::DEBUG, 'Processing order', ['step' => '1']);
$telemetry->log(LogLevel::WARNING, 'Slow response from DB', ['db' => 'orders']);
$telemetry->endTransaction();
For more examples, see the examples folder.
You can set a custom date format and time zone using setDateFormat
and setTimeZone
.
$telemetry->setDateFormat('Y-m-d H:i:s');
$telemetry->setTimeZone('America/New_York');
This configuration will format timestamps according to the specified format and time zone.
To add a custom driver, implement the DriverInterface and define the write method to handle log storage.
<?php
namespace Telemetry\Drivers;
use Telemetry\DriverInterface;
class CustomDriver implements DriverInterface
{
public function write(string $message): void
{
// Custom logic to store or display log message
}
}
<?php
$customDriver = new CustomDriver();
$telemetry = new Telemetry($customDriver);
$telemetry->log(LogLevel::INFO, "Testing custom driver");