-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLogger.php
168 lines (149 loc) · 2.78 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @package s9e\TextFormatter
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Parser;
use InvalidArgumentException;
use s9e\TextFormatter\Parser;
class Logger
{
/**
* @var string Name of the attribute being processed
*/
protected $attrName;
/**
* @var array Log entries in the form [[<type>,<msg>,<context>]]
*/
protected $logs = [];
/**
* @var Tag Tag being processed
*/
protected $tag;
/**
* Add a log entry
*
* @param string $type
* @param string $msg
* @param array $context
* @return void
*/
protected function add($type, $msg, array $context)
{
if (!isset($context['attrName']) && isset($this->attrName))
{
$context['attrName'] = $this->attrName;
}
if (!isset($context['tag']) && isset($this->tag))
{
$context['tag'] = $this->tag;
}
$this->logs[] = [$type, $msg, $context];
}
/**
* Clear the log
*
* @return void
*/
public function clear()
{
$this->logs = [];
$this->unsetAttribute();
$this->unsetTag();
}
/**
* Return the logs
*
* @return array
*/
public function getLogs()
{
return $this->logs;
}
/**
* Record the name of the attribute being processed
*
* @param string $attrName
* @return void
*/
public function setAttribute($attrName)
{
$this->attrName = $attrName;
}
/**
* Record the tag being processed
*
* @param Tag $tag
* @return void
*/
public function setTag(Tag $tag)
{
$this->tag = $tag;
}
/**
* Unset the name of the attribute being processed
*
* @return void
*/
public function unsetAttribute()
{
unset($this->attrName);
}
/**
* Unset the tag being processed
*
* @return void
*/
public function unsetTag()
{
unset($this->tag);
}
//==========================================================================
// Log levels
//==========================================================================
/**
* Add a "debug" type log entry
*
* @param string $msg Log message
* @param array $context
* @return void
*/
public function debug($msg, array $context = [])
{
$this->add('debug', $msg, $context);
}
/**
* Add an "err" type log entry
*
* @param string $msg Log message
* @param array $context
* @return void
*/
public function err($msg, array $context = [])
{
$this->add('err', $msg, $context);
}
/**
* Add an "info" type log entry
*
* @param string $msg Log message
* @param array $context
* @return void
*/
public function info($msg, array $context = [])
{
$this->add('info', $msg, $context);
}
/**
* Add a "warn" type log entry
*
* @param string $msg Log message
* @param array $context
* @return void
*/
public function warn($msg, array $context = [])
{
$this->add('warn', $msg, $context);
}
}