InfluxdbLog.php 1.43 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * User: hbomb_000
 * Date: 2016/5/5
 * Time: 18:17
 */

require_once __DIR__.'/vendor/autoload.php';

use InfluxDB\Client;
use InfluxDB\Point;

/**
 * Class UdpLog
 * @useage:
 * UdpLog::info('get payment list begin',array('order_code'=>123231));
 * @package WebPlugin
 */
class InfluxdbLog
{
    
    const info_channel = 'YHInfo';
    const debug_channel = 'YHDebug';
    const shutdown_channel = 'YHShutdown';

    public static function handle($type, array $data,array $tags){
        $client = new Client(Config::influxdb_host,Config::influxdb_port);
        
        switch ($type){
            case 'error':
                $db = 'error';
                $measurement = self::debug_channel.date('_Ymd');
                break;
            case 'shutdown':
                $db = 'error';
                $measurement = self::shutdown_channel.date('_Ymd');
                break;
            default:
                $db = 'log';
                $measurement = self::info_channel.date('_Ymd');
        }
        
        $database = $client->selectDB($db);
        $points = array(
            new Point(
                $measurement, // name of the measurement
                null, // the measurement value
                $tags,
                $data,
                exec('date +%s%N') // timestamp in nanoseconds on Linux ONLY
            ),
        );
        
        return $database->writePoints($points);
    }
}