forked from etsy/wpt-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplunkLogger.php
46 lines (35 loc) · 1.11 KB
/
SplunkLogger.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
46
<?php
class SplunkLogger {
public $path;
function __construct($path, $logging_ns) {
if (empty($path)) {
throw new InvalidArgumentException("Must include path to log file");
}
if (!file_exists($path)) {
throw new InvalidArgumentException("Path supplied must exist.");
}
$this->path = $path;
$this->logging_ns = $logging_ns;
}
function log($result) {
// Stick the logging namespace in there:
$result['logging_ns'] = $this->logging_ns;
$date = $result['date'];
// Lazy flattening.
$data = $result['data'];
$result = array_merge($result, $data);
unset($result['data']);
$line = date('c', $date) . $this->formatData($result) . "\n";
file_put_contents($this->path, $line, FILE_APPEND);
}
function formatData($data) {
$str = "";
foreach($data as $key => $val) {
if (strpos($val, " ") !== false) {
$val = "\"$val\"";
}
$str .= sprintf(" %s=%s", $key, $val);
}
return $str;
}
}