-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonLogger.php
178 lines (164 loc) · 6.27 KB
/
CommonLogger.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
<?php
namespace phpWTL;
use phpWTL\DataRetrievalPolicy;
use phpWTL\DataRetrievalPolicyHelper;
use phpWTL\DRP;
use phpWTL\aBasicLogger;
use phpWTL\CommonFormatDescriptor;
use phpWTL\LoggerContent;
use phpWTL\CommonDataRetriever;
use phpWTL\GenericDataValidator;
use phpWTL\CommonDataFormatter;
require_once 'DataRetrievalPolicy.php';
require_once 'DataRetrievalPolicyHelper.php';
require_once 'DRP.php';
require_once 'aBasicLogger.php';
require_once 'CommonFormatDescriptor.php';
require_once 'LoggerContent.php';
require_once 'CommonDataRetriever.php';
require_once 'GenericDataValidator.php';
require_once 'CommonDataFormatter.php';
/**
* Logger for the NCSA common log format (see: https://en.wikipedia.org/wiki/Common_Log_Format).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.3.3
* @api
*/
class CommonLogger extends aBasicLogger {
/**
* 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
*
* @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(CommonFormatDescriptor::getInstance());
static::$dataRetriever= CommonDataRetriever::getInstance(array(static::$loggerContent, static::$retrievalPolicies));
static::$dataValidator= GenericDataValidator::getInstance(static::$loggerContent);
static::$dataFormatter= CommonDataFormatter::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();
}
}
/**
* 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;
}
/**
* 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
)),
);
}
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
* - 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) {
$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();
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;
}
}
?>