PHP code example of ody / influxdb

1. Go to this page and download the library: Download ody/influxdb 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/ */

    

ody / influxdb example snippets


'providers' => [
    // Other providers
    InfluxDB\Logging\InfluxDB2ServiceProvider::class,
],

'channels' => [
    // Other channels...
    
    'influxdb' => [
        'driver' => 'influxdb',
        'url' => env('INFLUXDB_URL', 'http://127.0.0.1:8086'),
        'token' => env('INFLUXDB_TOKEN', ''),
        'org' => env('INFLUXDB_ORG', 'organization'),
        'bucket' => env('INFLUXDB_BUCKET', 'logs'),
        'measurement' => env('INFLUXDB_MEASUREMENT', 'logs'),
        'level' => env('INFLUXDB_LOG_LEVEL', 'debug'),
        'use_coroutines' => env('INFLUXDB_USE_COROUTINES', false),
        'tags' => [
            'service' => env('APP_NAME', 'ody-service'),
            'environment' => env('APP_ENV', 'production'),
            'instance' => env('INSTANCE_ID', gethostname()),
        ],
    ],
    
    // Optional: Add to stack to log to multiple destinations
    'production' => [
        'driver' => 'group',
        'channels' => ['file', 'influxdb'],
    ],
],

// Log to InfluxDB channel
logger('User registered', ['user_id' => 123], 'influxdb');

// Or configure influxdb as your default channel in config/logging.php
// and use logger normally
logger('System started');

// Log with custom tags
logger('API request processed', [
    'duration' => 120,
    'endpoint' => '/users',
    'tags' => [
        'module' => 'api',
        'method' => 'GET',
        'status' => 200
    ]
], 'influxdb');

use Psr\Log\LoggerInterface;

class UserService
{
    protected $logger;
    
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
    
    public function registerUser($data)
    {
        // Register user logic...
        
        $this->logger->info('User registered', [
            'id' => $user->id,
            'email' => $user->email
        ]);
    }
}

'influxdb' => [
    // ...other config
    'use_coroutines' => true,
],

'influxdb' => [
    // ...other config
    'measurement' => 'application_logs',
],

$requestId = uniqid();

logger('Request started', [
    'tags' => ['request_id' => $requestId]
]);

// Later in the code
logger('Database query executed', [
    'query' => 'SELECT * FROM users',
    'duration' => 25,
    'tags' => ['request_id' => $requestId]
]);

// And finally
logger('Request completed', [
    'duration' => 150,
    'tags' => ['request_id' => $requestId]
]);