-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.php
66 lines (52 loc) · 1.51 KB
/
logger.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Adds a log message
*
* @author Faizan Ayubi
*/
class Logger {
protected $_file;
protected $_entries;
protected $_start;
protected $_end;
protected function _sum($values) {
$count = 0;
foreach ($values as $value) {
$count += $value;
}
return $count;
}
protected function _average($values) {
return $this->_sum($values) / sizeof($values);
}
public function __construct($options) {
if (!isset($options["file"])) {
throw new Exception("Log file invalid.");
}
$this->_file = $options["file"];
$this->_entries = array();
$this->_start = microtime();
}
public function log($message) {
$this->_entries[] = array(
"message" => "[" . date("Y-m-d H:i:s") . "]" . $message,
"time" => microtime()
);
}
public function __destruct() {
$messages = "";
$last = $this->_start;
$times = array();
foreach ($this->_entries as $entry) {
$messages .= $entry["message"] . "\n";
$times[] = $entry["time"] - $last;
$last = $entry["time"];
}
$messages .= "Average: " . $this->_average($times);
$messages .= ", Longest: " . max($times);
$messages .= ", Shortest: " . min($times);
$messages .= ", Total: " . (microtime() - $this->_start);
$messages .= "\n";
file_put_contents($this->_file, $messages, FILE_APPEND);
}
}