1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2017 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Parser; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use s9e\TextFormatter\Parser; |
12
|
|
|
|
13
|
|
|
class Logger |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string Name of the attribute being processed |
17
|
|
|
*/ |
18
|
|
|
protected $attrName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array Log entries in the form [[<type>,<msg>,<context>]] |
22
|
|
|
*/ |
23
|
|
|
protected $logs = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Tag Tag being processed |
27
|
|
|
*/ |
28
|
|
|
protected $tag; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Add a log entry |
32
|
|
|
* |
33
|
|
|
* @param string $type |
34
|
|
|
* @param string $msg |
35
|
|
|
* @param array $context |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
11 |
|
protected function add($type, $msg, array $context) |
39
|
|
|
{ |
40
|
11 |
|
if (!isset($context['attrName']) && isset($this->attrName)) |
41
|
|
|
{ |
42
|
1 |
|
$context['attrName'] = $this->attrName; |
43
|
|
|
} |
44
|
|
|
|
45
|
11 |
|
if (!isset($context['tag']) && isset($this->tag)) |
46
|
|
|
{ |
47
|
1 |
|
$context['tag'] = $this->tag; |
48
|
|
|
} |
49
|
|
|
|
50
|
11 |
|
$this->logs[] = [$type, $msg, $context]; |
51
|
11 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Clear the log |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
1 |
|
public function clear() |
59
|
|
|
{ |
60
|
1 |
|
$this->logs = []; |
61
|
1 |
|
$this->unsetAttribute(); |
62
|
1 |
|
$this->unsetTag(); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the logs |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
11 |
|
public function getLogs() |
71
|
|
|
{ |
72
|
11 |
|
return $this->logs; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Record the name of the attribute being processed |
77
|
|
|
* |
78
|
|
|
* @param string $attrName |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
3 |
|
public function setAttribute($attrName) |
82
|
|
|
{ |
83
|
3 |
|
$this->attrName = $attrName; |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Record the tag being processed |
88
|
|
|
* |
89
|
|
|
* @param Tag $tag |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
3 |
|
public function setTag(Tag $tag) |
93
|
|
|
{ |
94
|
3 |
|
$this->tag = $tag; |
95
|
3 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Unset the name of the attribute being processed |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
2 |
|
public function unsetAttribute() |
103
|
|
|
{ |
104
|
2 |
|
unset($this->attrName); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Unset the tag being processed |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
2 |
|
public function unsetTag() |
113
|
|
|
{ |
114
|
2 |
|
unset($this->tag); |
115
|
2 |
|
} |
116
|
|
|
|
117
|
|
|
//========================================================================== |
118
|
|
|
// Log levels |
119
|
|
|
//========================================================================== |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add a "debug" type log entry |
123
|
|
|
* |
124
|
|
|
* @param string $msg Log message |
125
|
|
|
* @param array $context |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
8 |
|
public function debug($msg, array $context = []) |
129
|
|
|
{ |
130
|
8 |
|
$this->add('debug', $msg, $context); |
131
|
8 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Add an "err" type log entry |
135
|
|
|
* |
136
|
|
|
* @param string $msg Log message |
137
|
|
|
* @param array $context |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
1 |
|
public function err($msg, array $context = []) |
141
|
|
|
{ |
142
|
1 |
|
$this->add('err', $msg, $context); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add an "info" type log entry |
147
|
|
|
* |
148
|
|
|
* @param string $msg Log message |
149
|
|
|
* @param array $context |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
1 |
|
public function info($msg, array $context = []) |
153
|
|
|
{ |
154
|
1 |
|
$this->add('info', $msg, $context); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Add a "warn" type log entry |
159
|
|
|
* |
160
|
|
|
* @param string $msg Log message |
161
|
|
|
* @param array $context |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
1 |
|
public function warn($msg, array $context = []) |
165
|
|
|
{ |
166
|
1 |
|
$this->add('warn', $msg, $context); |
167
|
|
|
} |
168
|
|
|
} |