-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtendedLogger.php
350 lines (321 loc) · 11.6 KB
/
ExtendedLogger.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace phpWTL;
use phpWTL\DataRetrievalPolicy;
use phpWTL\DataRetrievalPolicyHelper;
use phpWTL\DRP;
use phpWTL\aBasicLogger;
use phpWTL\ExtendedFormatDescriptor;
use phpWTL\LoggerContent;
use phpWTL\ExtendedDataRetriever;
use phpWTL\GenericDataValidator;
use phpWTL\ExtendedDataFormatter;
require_once 'DataRetrievalPolicy.php';
require_once 'DataRetrievalPolicyHelper.php';
require_once 'DRP.php';
require_once 'aBasicLogger.php';
require_once 'ExtendedFormatDescriptor.php';
require_once 'LoggerContent.php';
require_once 'ExtendedDataRetriever.php';
require_once 'GenericDataValidator.php';
require_once 'ExtendedDataFormatter.php';
/**
* Logger for the extended log file format (see: https://www.w3.org/TR/WD-logfile.html).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.3.4
* @api
*/
class ExtendedLogger extends aBasicLogger {
protected static $time_taken_start= null;
protected static $time_taken_stop= null;
/**
* The constructor must perform the following taks:
*
* - Call "loadRetrievalPolicies" or handle the policies otherwise appropriately
* - Instantiate and store a LoggerContent object based on the required static FormatDescriptor
* - Handle/store all other (optional) parts neccessary and/or wanted (data retriever, validator, formatter)
* - Handle buffer initialization for ContentLengthRetrieval policy
* - Handle start timestamp measurement for time-taken field
*
* @param object $retrievalPolicies Provide policies for data retrieval (if applicable).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
protected function __construct($retrievalPolicies= null) {
static::setDataRetrievalPolicies($retrievalPolicies);
static::$loggerContent= new LoggerContent(ExtendedFormatDescriptor::getInstance());
static::$dataRetriever= ExtendedDataRetriever::getInstance(array(static::$loggerContent, static::$retrievalPolicies));
static::$dataValidator= GenericDataValidator::getInstance(static::$loggerContent);
static::$dataFormatter= ExtendedDataFormatter::getInstance(static::$loggerContent);
if (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_BUFFER) {
static::initializeBuffering();
}
static::takeTime();
}
/**
* Buffer initialization for ContentLengthRetrieval policy.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.1
*/
protected function initializeBuffering() {
if (count(ob_get_status())>0) ob_end_clean();
ob_start();
}
/**
* Buffer finalization for ContentLengthRetrieval policy (flush, store content-length).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
protected function finalizeBuffering() {
$ob_size= ob_get_length();
ob_end_flush();
if (static::$dataRetriever) {
DataRetrievalPolicyHelper::setDataRetrievalPolicyParameter(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL, $ob_size);
static::$dataRetriever->setDataRetrievalPolicies(static::$retrievalPolicies);
}
}
/**
* Return PHP output buffer.
*
* @return string buffer content (null if logging mode is not buffered)
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getBuffer() {
$ret= null;
if (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_BUFFER) {
$ret= ob_get_contents();
}
return $ret;
}
/**
* Handle time measurement.
*
* @param boolean $stop Start or stop measurement (default= false= start).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
public function takeTime($stop= false) {
if ($stop) {
static::$time_taken_stop= static::microtimeMilis();
} else {
static::$time_taken_start= static::microtimeMilis();
}
}
/**
* Handle time measurement.
*
* @return miliseconds.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
public function timeTaken() {
$ret= 0;
if (static::$time_taken_start && static::$time_taken_stop) {
$ret= (static::$time_taken_stop - static::$time_taken_start);
}
return $ret;
}
/**
* microtime float value.
*
* @return miliseconds.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
public function microtimeMilis() {
list($usec, $sec)= explode(" ", microtime());
return ($sec*1000) + round($usec*1000);
}
/**
* Set default data retrieval policies.
*
* @param object $retrievalPolicies Provide policies for data retrieval.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
protected function loadDataRetrievalPoliciesDefault() {
static::$retrievalPolicies= array(
new DataRetrievalPolicy(array(
'name' => DRP::DRP_CONTENT_LENGTH_RETRIEVAL,
'flag' => DRP::DRP_CLR_SCRIPT
)),
);
}
/**
* Set data retrieval policies after initialization:
*
* - Also do this in the associated data retriever
* - If null restore the default
* - Handle buffer reset/initialization for ContentLengthRetrieval policy
*
* @param array $retrievalPolicies Provide policies for data retrieval.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setDataRetrievalPolicies($retrievalPolicies= null) {
if ($retrievalPolicies) {
static::$retrievalPolicies= $retrievalPolicies;
} else {
static::loadDataRetrievalPoliciesDefault();
}
if (static::$dataRetriever) static::$dataRetriever->setDataRetrievalPolicies(static::$retrievalPolicies);
if (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_BUFFER) {
static::initializeBuffering();
}
}
/**
* Perform the actual logging process:
*
* - Handle buffer flush and buffer-size measurement for ContentLengthRetrieval policy
* - Handle stop timestamp measurement for time-taken field
* - Retrieve log data thru retriever
* - Validate data fields with validator (can be turned off)
* - Format/prefix/suffix fields via formatter (can be turned off), set the datatype class in content object accordingly
*
* @param array $params Logger parameters (bool "validate" default "false", bool "format" default "true")
* @return array Validation errors (null if none)
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.3
* @api
*/
public function log($params= null) {
static::takeTime(true);
$ret= null;
$validate= false;
$format= true;
if ($params && is_array($params)) {
if (array_key_exists("validate", $params)) {
if ($params["validate"]) $validate= true; else $validate= false;
}
if (array_key_exists("format", $params)) {
if ($params["format"]) $format= true; else $format= false;
}
}
if (DataRetrievalPolicyHelper::existsDataRetrievalPolicy(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL) &&
DataRetrievalPolicyHelper::getDataRetrievalPolicyFlag(static::$retrievalPolicies, DRP::DRP_CONTENT_LENGTH_RETRIEVAL)==DRP::DRP_CLR_BUFFER) {
static::finalizeBuffering();
}
if (static::$dataRetriever) {
static::$dataRetriever->retrieve();
static::$dataRetriever->setFieldContent("time-taken", static::timeTaken());
static::setDirectives();
}
if ($validate && static::$dataValidator) $ret= static::$dataValidator->validate();
if ($format && static::$dataFormatter) {
static::$dataFormatter->formatAll();
static::$loggerContent->setDatatypeClass(FormatDescriptorHelper::DATATYPE_FORMATTED);
} else {
static::$loggerContent->setDatatypeClass(FormatDescriptorHelper::DATATYPE_RAW);
}
return $ret;
}
/**
* Build directives (meta data) content lines for a file writer
*
* @param array $fields_whitelist If given two things will be done: a) only fields matching the list will be included in the "Fields" directive and b) they will be sorted according to the order of the whitelist.
* @return array Lines
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.2
* @api
*/
public function buildDirectivesForFileWriter($fields_whitelist= null) {
$ret= array();
if (static::$loggerContent) {
$meta= static::getDirectives($fields_whitelist);
if ($meta && is_array($meta)) foreach ($meta as $k=>$f) {
$ret[$k]= static::$loggerContent->getFormatDescriptor()->getCaption($k).": ".$f;
}
}
return $ret;
}
/**
* Set directives (meta data) array
*
* @param array $fields_whitelist If given two things will be done: a) only fields matching the list will be included in the "Fields" directive and b) they will be sorted according to the order of the whitelist.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setDirectives($fields_whitelist= null) {
if (static::$loggerContent) {
static::$loggerContent->__set("dir_fields", static::retrieveFieldsDirectiveContent($fields_whitelist));
}
}
/**
* Get directives (meta data) array
*
* @param array $fields_whitelist If given two things will be done: a) only fields matching the list will be included in the "Fields" directive and b) they will be sorted according to the order of the whitelist.
* @return array
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
protected function getDirectives($fields_whitelist= null) {
$ret= array();
if (static::$loggerContent) {
static::$loggerContent->__set("dir_fields", static::retrieveFieldsDirectiveContent($fields_whitelist));
$meta= static::$loggerContent->toArrayMeta();
if ($meta && is_array($meta)) {
$ret= $meta;
}
}
return $ret;
}
/**
* Return String for "Fields" Directive
*
* @param array $fields_whitelist If given two things will be done: a) only fields matching the list will be included in the "Fields" directive and b) they will be sorted according to the order of the whitelist.
* @return string
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.2
*/
protected function retrieveFieldsDirectiveContent($fields_whitelist= null) {
$value= "";
if ($fields_whitelist && is_array($fields_whitelist)) {
$default= FormatDescriptorHelper::DEFAULT_ANY;
} else {
$default= FormatDescriptorHelper::DEFAULT_ONLY;
}
$field_names= static::$loggerContent->getFormatDescriptor()->getRegularFieldNames($default);
if ($fields_whitelist && is_array($fields_whitelist)) {
// check if $fields_whitlist contains only valid field names, delete invalid names
$valid_fields= static::$loggerContent->getFormatDescriptor()->getFieldNames();
foreach ($fields_whitelist as $k=>$f) {
if (!in_array($f, $valid_fields)) unset($fields_whitelist[$k]);
}
// only consider fields contained in $fields_whitelist
$field_names= $fields_whitelist;
}
$content= static::$loggerContent->getFormatDescriptor()->getFieldData("caption", $field_names);
// order according to $fields_whitelist
if ($fields_whitelist && is_array($fields_whitelist)) {
$content= static::$loggerContent->sortArray($content, $fields_whitelist);
}
$maxk= count($content)-1;
foreach ($content as $k=>$f) {
$value.= $f;
if ($k != $maxk) $value.= " ";
}
return $value;
}
}
?>