-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.php
192 lines (170 loc) · 3.92 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Rad\Logging;
use SplObjectStorage;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
/**
* Logger
*
* @package Rad\Logging
*/
class Logger implements LoggerInterface
{
/**
* @var SplObjectStorage
*/
protected $adapters;
protected $begun = false;
protected $transaction = [];
/**
* Rad\Logging\Logger constructor
*/
public function __construct()
{
$this->adapters = new SplObjectStorage();
}
/**
* Attach adapter
*
* @param AdapterInterface $adapter
*/
public function attachAdapter(AdapterInterface $adapter)
{
$this->adapters->attach($adapter);
}
/**
* Detach adapter
*
* @param AdapterInterface $adapter
*/
public function detachAdapter(AdapterInterface $adapter)
{
$this->adapters->detach($adapter);
}
/**
* Contains adapter
*
* @param AdapterInterface $adapter
*
* @return bool
*/
public function containsAdapter(AdapterInterface $adapter)
{
return $this->adapters->contains($adapter);
}
/**
* Begin transaction
*/
public function begin()
{
$this->begun = true;
}
/**
* Rollback transaction
*/
public function rollback()
{
$this->begun = false;
$this->transaction = [];
}
/**
* Commit transaction
*/
public function commit()
{
foreach ($this->transaction as $trans) {
$this->internalLog($trans['level'], $trans['message'], $trans['time'], $trans['context']);
}
$this->rollback();
}
/**
* {@inheritdoc}
*/
public function emergency($message, array $context = [])
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
/**
* {@inheritdoc}
*/
public function alert($message, array $context = [])
{
$this->log(LogLevel::ALERT, $message, $context);
}
/**
* {@inheritdoc}
*/
public function critical($message, array $context = [])
{
$this->log(LogLevel::CRITICAL, $message, $context);
}
/**
* {@inheritdoc}
*/
public function error($message, array $context = [])
{
$this->log(LogLevel::ERROR, $message, $context);
}
/**
* {@inheritdoc}
*/
public function warning($message, array $context = [])
{
$this->log(LogLevel::WARNING, $message, $context);
}
/**
* {@inheritdoc}
*/
public function notice($message, array $context = [])
{
$this->log(LogLevel::NOTICE, $message, $context);
}
/**
* {@inheritdoc}
*/
public function info($message, array $context = [])
{
$this->log(LogLevel::INFO, $message, $context);
}
/**
* {@inheritdoc}
*/
public function debug($message, array $context = [])
{
$this->log(LogLevel::DEBUG, $message, $context);
}
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = [])
{
if ($this->begun) {
$this->transaction[] = [
'level' => $level,
'message' => $message,
'time' => time(),
'context' => $context
];
} else {
$this->internalLog($level, $message, time(), $context);
}
}
/**
* Internal log
*
* @param string $level Log level
* @param string $message Log message
* @param int $time Log time
* @param array $context Log context
*/
protected function internalLog($level, $message, $time, array $context = [])
{
$this->adapters->rewind();
while ($this->adapters->valid()) {
/** @var AdapterInterface $adapter */
$adapter = $this->adapters->current();
$adapter->log($level, $message, $time, $context);
$this->adapters->next();
}
}
}