PHP code example of nodepile / phptelemetry

1. Go to this page and download the library: Download nodepile/phptelemetry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nodepile / phptelemetry example snippets




odePile\PHPTelemetry\Drivers\CliDriver;
use NodePile\PHPTelemetry\Drivers\FileDriver;
use NodePile\PHPTelemetry\Support\TimeProvider;
use NodePile\PHPTelemetry\Support\IdGenerator;
use NodePile\PHPTelemetry\DriverManagerFactory;
use NodePile\PHPTelemetry\Logger;

// For now i created just a simple cli and a file driver but you can expand
// this by creating however many drivers you need.
$config = [
	'default' => 'cli',

	'drivers' => [
		'cli' => [
			'driver' => CliDriver::class,
		],

		'file' => [
			'driver' => FileDriver::class,
			'settings' => [
				'file' => __DIR__ . '/my.log',
			],
		],
	],
];

$driverManagerFactory = new DriverManagerFactory();
$driverManager = $driverManagerFactory->create($config);

// You can pass your own time provider or the built in one.
$timeProvider = new TimeProvider();

// You can pass your own id generator
// (useful if you already have a standardized transaction/trace id format).
$idGenerator = new IdGenerator();

$logger = new Logger($driverManager, $timeProvider, $idGenerator);

// Simple logging with existing levels.
$logger->info("User logged in successfully.", ['id' => 50]);
$logger->warning("SSL handshake failed.", ['ip' => '123.4.5.6']);
$logger->error("DB conn failed. The server went away.", ['code' => 2006]);

// Add custom log levels
$logger->addLevel("custom");

// Logging with a custom level that was not previously added using ->addLevel(...)
// will throw an InvalidLevelException
$logger->log("custom", "Log some custom message.", ['with' => 'or without context']);

// Log during a transaction.
$transaction = $logger->startTransaction(['user' => 'login']);

$transaction->info("Received auth request.", ['ip' => '127.0.0.1']);
$transaction->info("Credentials validated successfully.");
$transaction->error("Failed to log the user in.", ['reasons' => 'can be logged']);

$transaction->endTransaction();
// Trying to use the same transaction after it ended will result in a TransactionEndedException
// Running $transaction->info("Will throw a TransactionEndedException.");

// Switch driver
$logger->useDriver('cli');
$logger->info("I am logged to CLI.", ['with' => 'some context']);

$logger->useDriver('file');
$logger->info("I am logged to a file.", ['with' => 'a different context']);
file