PHP code example of jmluang / logtailer-client-sdk

1. Go to this page and download the library: Download jmluang/logtailer-client-sdk 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/ */

    

jmluang / logtailer-client-sdk example snippets


'channels' => [
    'logtailer' => [
        'driver' => 'custom',
        'via' => function () {
            $handler = new \Jmluang\Logtailer\Monolog\LogtailHandler(
                env('LOGTAILER_TOKEN'),
                \Monolog\Level::Debug,
                true,
                env('LOGTAILER_ENDPOINT', 'http://localhost:8080/logs')
            );
            return new \Monolog\Logger('logtailer', [$handler]);
        },
    ],

    'stack' => [
        'driver' => 'stack',
        'channels' => ['single', 'logtailer'],
    ],
],

Log::info('User logged in', ['user_id' => $user->id]);
Log::error('Payment failed', ['order_id' => $orderId]);

use Monolog\Logger;
use Monolog\Level;
use Jmluang\Logtailer\Monolog\LogtailHandler;

$logger = new Logger('my-app');

$handler = new LogtailHandler(
    'your-source-token',
    Level::Debug,
    true,
    'http://localhost:8080/logs'
);

$logger->pushHandler($handler);

// Use logger as normal
$logger->info('Application started');
$logger->error('An error occurred', ['error_code' => 500]);

use Jmluang\Logtailer\Monolog\LogtailHandlerBuilder;

$handler = LogtailHandlerBuilder::withSourceToken('your-token')
    ->withEndpoint('http://localhost:8080/logs')
    ->withLevel(Level::Info)
    ->withBufferLimit(500)
    ->withFlushIntervalMilliseconds(3000)
    ->build();

$logger->pushHandler($handler);

$logger->info('User action', [
    'user_id' => 123,
    'action' => 'login',
    'ip' => '192.168.1.1',
    'metadata' => [
        'browser' => 'Chrome',
        'os' => 'macOS'
    ]
]);

try {
    // Some operation
} catch (\Exception $e) {
    $logger->error('Operation failed', [
        'exception' => $e
    ]);
}

$apiLogger = new Logger('api');
$apiLogger->pushHandler($handler);

$dbLogger = new Logger('database');
$dbLogger->pushHandler($handler);

$apiLogger->info('API request received');
$dbLogger->info('Database query executed');

tests/
├── Monolog/
│   ├── LogtailClientTest.php       # HTTP client tests
│   ├── LogtailFormatterTest.php    # JSON formatter tests
│   ├── LogtailHandlerTest.php      # Buffer handler tests
│   ├── LogtailHandlerBuilderTest.php # Builder pattern tests
│   └── SynchronousLogtailHandlerTest.php # Sync handler tests