-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphiteLogger.php
45 lines (39 loc) · 1.09 KB
/
GraphiteLogger.php
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
<?php
namespace OpenSky\Bundle\GraphiteBundle;
class GraphiteLogger
{
protected $connection;
protected $prefix;
protected $active;
/**
* @param Connection $connection
* @param string $prefix prefix for all stats, configure per server or environemnt
* @param bool $active if false, don't make udp call
*/
public function __construct(Connection $connection, $prefix = '')
{
$this->connection = $connection;
$this->prefix = $prefix;
}
/**
* Log metric to graphite
*
* @param string $metric The metric to log info for.
* @param float $value The value to log
* @param DateTime $time Optionally log the time, defaults to now
*/
public function log($metric, $value, \DateTime $time = null)
{
if ($time === null) {
$time = new \DateTime();
}
$this->connection->write(sprintf("%s%s %s %s", $this->prefix, $metric, $value, $time->getTimestamp()));
}
/**
* Close connection
*/
public function closeConnection()
{
$this->connection->close();
}
}