borzenkovdev/php-telemetry

This package offer a variety of features for meaningful logging.

Installs: 5

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/borzenkovdev/php-telemetry

1.0.0 2024-10-27 20:21 UTC

This package is auto-updated.

Last update: 2025-12-28 13:27:25 UTC


README

Packagist Version Packagist PHP Version Support Packagist Downloads

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.

Features

  • 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.

Installation

Install the latest version with:

$ composer require borzenkovdev/php-telemetry

Usage

Basic Usage

<?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']);

Basic Usage with transaction

<?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.

Configuring Date Format and Time Zone

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.

Adding a Custom Driver

To add a custom driver, implement the DriverInterface and define the write method to handle log storage.

Example

<?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
    }
}

Usage

<?php

$customDriver = new CustomDriver();
$telemetry = new Telemetry($customDriver);
$telemetry->log(LogLevel::INFO, "Testing custom driver");

UML Diagram

UML Class Diagram