InfluxdbLog.php
1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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);
}
}