-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArrayLogger.php
More file actions
89 lines (80 loc) · 2.65 KB
/
ArrayLogger.php
File metadata and controls
89 lines (80 loc) · 2.65 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Midweste\SimpleLogger;
use Psr\Log\LogLevel;
class ArrayLogger extends AbstractSimpleLogger
{
protected $memory = array();
public function __construct($min_level = LogLevel::DEBUG)
{
$this->min_level = $min_level;
}
public function log($level, $message, array $context = array())
{
if (!$this->min_level_reached($level)) {
return;
}
$this->memory[] = array(
'timestamp' => date('Y-m-d H:i:s'),
'level' => $level,
'message' => $message,
'context' => $context
);
}
/**
* Get all log entries
*
* @param callable|null $formatter An optional formatting function called on every log entry.
* This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context'
* and must return the new log entry.
* @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context',
* unless the log entries are converted to something else by the $formatter parameter.
*/
public function get($formatter = null)
{
$r = $this->memory;
if (is_callable($formatter)) {
foreach ($r as $i => $a) {
$r[$i] = call_user_func($formatter, $a);
}
}
return $r;
}
/**
* Get all log entries and clear the log
*
* @param callable|null $formatter An optional formatting function called on every log entry.
* This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context'
* and must return the new log entry.
* @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context'
* unless the log entries are converted to something else by the $formatter parameter.
*/
public function getClear($formatter = null)
{
$r = $this->memory;
$this->clear();
if (is_callable($formatter)) {
foreach ($r as $i => $a) {
$r[$i] = call_user_func($formatter, $a);
}
}
return $r;
}
/**
* Clear the log
*
*/
public function clear()
{
$this->memory = array();
}
/**
* Formatter function that can be used as parameter for the get() and getClear() methods
*
* @param array $a
* @return string
*/
public function formatter(array $a)
{
return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']);
}
}