1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yep\WorkflowLogger; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface as PsrLoggerInterface; |
6
|
|
|
use Yep\WorkflowLogger\Exception\LevelIsNotDefinedException; |
7
|
|
|
use Yep\WorkflowLogger\Exception\WorkflowIsLockedException; |
8
|
|
|
use Yep\WorkflowLogger\Formatter\FormatterInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Workflow |
12
|
|
|
* |
13
|
|
|
* @package Yep\WorkflowLogger |
14
|
|
|
* @author Martin Zeman (Zemistr) <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Workflow implements PsrLoggerInterface |
17
|
|
|
{ |
18
|
|
|
const EMERGENCY = 'emergency'; |
19
|
|
|
const ALERT = 'alert'; |
20
|
|
|
const CRITICAL = 'critical'; |
21
|
|
|
const ERROR = 'error'; |
22
|
|
|
const WARNING = 'warning'; |
23
|
|
|
const NOTICE = 'notice'; |
24
|
|
|
const INFO = 'info'; |
25
|
|
|
const DEBUG = 'debug'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Logging levels from syslog protocol defined in RFC 5424 |
29
|
|
|
* |
30
|
|
|
* @var array|string[] $levels Logging levels |
31
|
|
|
*/ |
32
|
|
|
protected static $levels = [ |
33
|
|
|
self::DEBUG => 'DEBUG', |
34
|
|
|
self::INFO => 'INFO', |
35
|
|
|
self::NOTICE => 'NOTICE', |
36
|
|
|
self::WARNING => 'WARNING', |
37
|
|
|
self::ERROR => 'ERROR', |
38
|
|
|
self::CRITICAL => 'CRITICAL', |
39
|
|
|
self::ALERT => 'ALERT', |
40
|
|
|
self::EMERGENCY => 'EMERGENCY', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int|string |
45
|
|
|
*/ |
46
|
|
|
protected $level; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var FormatterInterface |
50
|
|
|
*/ |
51
|
|
|
protected $formatter; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var PsrLoggerInterface |
55
|
|
|
*/ |
56
|
|
|
protected $logger; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array|Record[] |
60
|
|
|
*/ |
61
|
|
|
protected $records = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var \DateTimeZone |
65
|
|
|
*/ |
66
|
|
|
protected $timezone; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
protected $locked = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Workflow constructor. |
75
|
|
|
* |
76
|
|
|
* @param PsrLoggerInterface $logger Main logger |
77
|
|
|
* @param FormatterInterface $formatter Workflow records formatter |
78
|
|
|
* @param \DateTimeZone $timezone Current timezone |
79
|
|
|
* @param int|string $level Workflow level code |
80
|
|
|
*/ |
81
|
|
|
public function __construct( |
82
|
|
|
PsrLoggerInterface $logger, |
83
|
|
|
FormatterInterface $formatter, |
84
|
|
|
\DateTimeZone $timezone, |
85
|
|
|
$level |
86
|
|
|
) { |
87
|
|
|
$this->logger = $logger; |
88
|
|
|
$this->formatter = $formatter; |
89
|
|
|
$this->timezone = $timezone; |
90
|
|
|
$this->level = $level; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return \DateTime |
95
|
|
|
*/ |
96
|
|
|
protected function getCurrentDateTime() |
97
|
|
|
{ |
98
|
|
|
$time = new \DateTime(null, $this->timezone); |
99
|
|
|
$time->setTimezone($this->timezone); |
100
|
|
|
|
101
|
|
|
return $time; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return int|string |
106
|
|
|
*/ |
107
|
|
|
public function getLevel() |
108
|
|
|
{ |
109
|
|
|
return $this->level; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Gets the name of the logging level. |
114
|
|
|
* |
115
|
|
|
* @param string $level |
116
|
|
|
* @return string |
117
|
|
|
* @throws LevelIsNotDefinedException |
118
|
|
|
*/ |
119
|
|
|
public function getLevelName($level) |
120
|
|
|
{ |
121
|
|
|
if (!isset(static::$levels[$level])) { |
122
|
|
|
throw LevelIsNotDefinedException::create($level, static::$levels); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return static::$levels[$level]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function isLocked() |
132
|
|
|
{ |
133
|
|
|
return $this->locked; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function lock() |
140
|
|
|
{ |
141
|
|
|
$this->locked = true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Logs with a workflow level and with all workflow records |
146
|
|
|
* |
147
|
|
|
* @param string $message The log message |
148
|
|
|
* @param array $context The log context |
149
|
|
|
* @return void |
150
|
|
|
* @throws WorkflowIsLockedException |
151
|
|
|
*/ |
152
|
|
|
public function finish($message = '', array $context = []) |
153
|
|
|
{ |
154
|
|
|
if ($this->isLocked()) { |
155
|
|
|
throw WorkflowIsLockedException::create(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->lock(); |
159
|
|
|
$message .= $message === '' ? '' : "\n\n"; |
160
|
|
|
$message .= "Workflow:\n"; |
161
|
|
|
|
162
|
|
|
foreach ($this->records as $record) { |
163
|
|
|
$message .= $this->formatter->format($record); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->logger->log($this->level, $message, $context); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Logs workflow record with an arbitrary level. |
171
|
|
|
* |
172
|
|
|
* @param string $level The logging level |
173
|
|
|
* @param string $message The log message |
174
|
|
|
* @param array $context The log context |
175
|
|
|
* @return void |
176
|
|
|
* @throws LevelIsNotDefinedException |
177
|
|
|
* @throws WorkflowIsLockedException |
178
|
|
|
*/ |
179
|
|
|
public function log($level, $message, array $context = []) |
180
|
|
|
{ |
181
|
|
|
if ($this->isLocked()) { |
182
|
|
|
throw WorkflowIsLockedException::create(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->records[] = new Record( |
186
|
|
|
$this->getCurrentDateTime(), |
187
|
|
|
$message, |
188
|
|
|
$this->getLevelName($level), |
189
|
|
|
$context |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* System is unusable. |
195
|
|
|
* |
196
|
|
|
* @param string $message The log message |
197
|
|
|
* @param array $context The log context |
198
|
|
|
* @return void |
199
|
|
|
* @throws WorkflowIsLockedException |
200
|
|
|
* @throws LevelIsNotDefinedException |
201
|
|
|
*/ |
202
|
|
|
public function emergency($message, array $context = []) |
203
|
|
|
{ |
204
|
|
|
$this->log(static::EMERGENCY, $message, $context); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Action must be taken immediately. |
209
|
|
|
* |
210
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
211
|
|
|
* trigger the SMS alerts and wake you up. |
212
|
|
|
* |
213
|
|
|
* @param string $message The log message |
214
|
|
|
* @param array $context The log context |
215
|
|
|
* @return void |
216
|
|
|
* @throws WorkflowIsLockedException |
217
|
|
|
* @throws LevelIsNotDefinedException |
218
|
|
|
*/ |
219
|
|
|
public function alert($message, array $context = []) |
220
|
|
|
{ |
221
|
|
|
$this->log(static::ALERT, $message, $context); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Critical conditions. |
226
|
|
|
* |
227
|
|
|
* Example: Application component unavailable, unexpected exception. |
228
|
|
|
* |
229
|
|
|
* @param string $message The log message |
230
|
|
|
* @param array $context The log context |
231
|
|
|
* @return void |
232
|
|
|
* @throws WorkflowIsLockedException |
233
|
|
|
* @throws LevelIsNotDefinedException |
234
|
|
|
*/ |
235
|
|
|
public function critical($message, array $context = []) |
236
|
|
|
{ |
237
|
|
|
$this->log(static::CRITICAL, $message, $context); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Runtime errors that do not require immediate action but should typically |
242
|
|
|
* be logged and monitored. |
243
|
|
|
* |
244
|
|
|
* @param string $message The log message |
245
|
|
|
* @param array $context The log context |
246
|
|
|
* @return void |
247
|
|
|
* @throws WorkflowIsLockedException |
248
|
|
|
* @throws LevelIsNotDefinedException |
249
|
|
|
*/ |
250
|
|
|
public function error($message, array $context = []) |
251
|
|
|
{ |
252
|
|
|
$this->log(static::ERROR, $message, $context); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Exceptional occurrences that are not errors. |
257
|
|
|
* |
258
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
259
|
|
|
* that are not necessarily wrong. |
260
|
|
|
* |
261
|
|
|
* @param string $message The log message |
262
|
|
|
* @param array $context The log context |
263
|
|
|
* @return void |
264
|
|
|
* @throws WorkflowIsLockedException |
265
|
|
|
* @throws LevelIsNotDefinedException |
266
|
|
|
*/ |
267
|
|
|
public function warning($message, array $context = []) |
268
|
|
|
{ |
269
|
|
|
$this->log(static::WARNING, $message, $context); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Normal but significant events. |
274
|
|
|
* |
275
|
|
|
* @param string $message The log message |
276
|
|
|
* @param array $context The log context |
277
|
|
|
* @return void |
278
|
|
|
* @throws WorkflowIsLockedException |
279
|
|
|
* @throws LevelIsNotDefinedException |
280
|
|
|
*/ |
281
|
|
|
public function notice($message, array $context = []) |
282
|
|
|
{ |
283
|
|
|
$this->log(static::NOTICE, $message, $context); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Interesting events. |
288
|
|
|
* |
289
|
|
|
* Example: User logs in, SQL logs. |
290
|
|
|
* |
291
|
|
|
* @param string $message The log message |
292
|
|
|
* @param array $context The log context |
293
|
|
|
* @return void |
294
|
|
|
* @throws WorkflowIsLockedException |
295
|
|
|
* @throws LevelIsNotDefinedException |
296
|
|
|
*/ |
297
|
|
|
public function info($message, array $context = []) |
298
|
|
|
{ |
299
|
|
|
$this->log(static::INFO, $message, $context); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Detailed debug information. |
304
|
|
|
* |
305
|
|
|
* @param string $message The log message |
306
|
|
|
* @param array $context The log context |
307
|
|
|
* @return void |
308
|
|
|
* @throws WorkflowIsLockedException |
309
|
|
|
* @throws LevelIsNotDefinedException |
310
|
|
|
*/ |
311
|
|
|
public function debug($message, array $context = []) |
312
|
|
|
{ |
313
|
|
|
$this->log(static::DEBUG, $message, $context); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|