-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogBuffer.php
191 lines (174 loc) · 4.61 KB
/
LogBuffer.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
<?php
namespace phpWTL;
use phpWTL\LogBufferHelper;
require_once 'LogBufferHelper.php';
/**
* Aggregator class for buffered logging.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.2
* @api
*/
class LogBuffer {
/** Array of LoggerContent objects */
protected $_content_buffer= null;
/** Store the logger object */
protected $_logger= null;
/** Store the writer object */
protected $_writer= null;
/** Store callback functions */
protected $_callbacks= null;
/** Store parameters */
protected $_params= null;
/**
* @param object $logger Provide the logger to use.
* @param object $writer Provide the log writer to use.
* @param array $callbacks Provide array of callback functions.
* @param array $params Provide parameters.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function __construct($logger, $writer, $callbacks, $params= null) {
if ($logger && $writer && $callbacks && is_array($callbacks)) {
$this->_logger= $logger;
$this->_writer= $writer;
if (!$params) {
$params= $this->loadDefaultParameters();
}
if (array_key_exists("buffer_size", $params)) {
if ($params["buffer_size"] < LogBufferHelper::BUFFER_OFF) $params["buffer_size"]= LogBufferHelper::BUFFER_OFF;
}
$this->_params= $params;
$this->_callbacks= $callbacks;
$this->_content_buffer= array();
}
}
/**
* @return object Get the logger object.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function getLogger() {
if (!(null === $this->_logger)) return $this->_logger;
}
/**
* @return object Get the writer object.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function getWriter() {
if (!(null === $this->_writer)) return $this->_writer;
}
/**
* @return int Get the buffer size (= maximum size).
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function getBufferSize() {
return $this->_params["buffer_size"];
}
/**
* @return int Get the content buffer size (= actual size).
*
* @author Michael Beyer <[email protected]>
* @version v0.1.1
* @api
*/
public function getContentBufferSize() {
$ret= null;
if (!(null === $this->_content_buffer) && is_array($this->_content_buffer)) {
$ret= count($this->_content_buffer);
}
return $ret;
}
/**
* @return array Get the content buffer.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.1
* @api
*/
public function getContentBuffer() {
return $this->_content_buffer;
}
/**
* Log to buffer (wrapper for logger log() method)
*
* @param array $params Parameters to pass through to the logger's log() method.
* @return array/mixed Errors (validation or logger specific, null if none)
*
* @author Michael Beyer <[email protected]>
* @version v0.1.1
* @api
*/
public function log($params= null) {
$err= null;
if ($this->getLogger()) {
if (array_key_exists(LogBufferHelper::CALLBACK_LOG_BEFORE, $this->_callbacks)) {
$this->_callbacks[LogBufferHelper::CALLBACK_LOG_BEFORE]($this->getLogger());
}
$err= $this->getLogger()->log($params);
if (array_key_exists(LogBufferHelper::CALLBACK_LOG_AFTER, $this->_callbacks)) {
$this->_callbacks[LogBufferHelper::CALLBACK_LOG_AFTER]($this->getLogger());
}
$content= $this->getLogger()->getLoggerContent();
if ($content && !$err) $this->store($content);
}
return $err;
}
/**
* Store content object to buffer
*
* @param object $content
* @author Michael Beyer <[email protected]>
* @version v0.1.2
* @api
*/
public function store($content) {
if ($content) {
$this->_content_buffer[]= clone $content;
if ($this->getBufferSize() < LogBufferHelper::BUFFER_INFINITE) {
if ($this->getContentBufferSize()>=$this->getBufferSize()) {
$this->flush();
}
}
}
}
/**
* Flush buffer
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function flush() {
if ($this->getWriter() && array_key_exists(LogBufferHelper::CALLBACK_FLUSH_EACH, $this->_callbacks)) {
foreach ($this->getContentBuffer() as $k=>$v) {
$this->_callbacks[LogBufferHelper::CALLBACK_FLUSH_EACH]($this->getWriter(), $v);
}
$this->_content_buffer= array();
}
}
/**
* Initialize default parameters
*
* @return array
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
*/
protected function loadDefaultParameters() {
return array (
'buffer_size' => LogBufferHelper::BUFFER_SIZE_DEFAULT
);
}
}
?>