[BUGFIX] Moving files in filelist renames file to "1"
[Packages/TYPO3.CMS.git] / t3lib / log / class.t3lib_log_record.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011-2012 Ingo Renner (ingo@typo3.org)
6 * (c) 2012 Steffen Müller (typo3@t3node.com)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25
26
27 /**
28 * Log record
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 * @author Steffen Müller (typo3@t3node.com)
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_log_Record implements ArrayAccess {
36
37 /**
38 * Unique ID of the request
39 *
40 * @var string
41 */
42 protected $requestId = '';
43
44 /**
45 * Creation timestamp with microseconds
46 *
47 * @var float
48 */
49 protected $created = 0.0;
50
51 /**
52 * The component where the record was created
53 *
54 * @var string
55 */
56 protected $component = '';
57
58 /**
59 * Severity level
60 *
61 * @var integer
62 */
63 protected $level = t3lib_log_Level::INFO;
64
65 /**
66 * Log message one-liner
67 *
68 * @var string
69 */
70 protected $message = '';
71
72 /**
73 * Additional log data
74 *
75 * @var array
76 */
77 protected $data = array();
78
79 /**
80 * Gettable properties for ArrayAccess
81 *
82 * @var array
83 */
84 private $gettableProperties = array(
85 'requestId',
86 'created',
87 'component',
88 'level',
89 'message',
90 'data'
91 );
92
93 /**
94 * Settable properties for ArrayAccess
95 *
96 * @var array
97 */
98 private $settableProperties = array(
99 'level',
100 'message',
101 'data'
102 );
103
104 /**
105 * Constructor.
106 *
107 * @param string $component Affected component
108 * @param integer $level Severity level (see t3lib_log_Level)
109 * @param string $message Log message
110 * @param array $data Additional data
111 */
112 public function __construct($component = '', $level, $message, array $data = array()) {
113
114 $this->setRequestId(Typo3_Bootstrap::getInstance()->getRequestId())
115 ->setCreated(microtime(TRUE))
116 ->setComponent($component)
117 ->setLevel($level)
118 ->setMessage($message)
119 ->setData($data);
120 }
121
122 /**
123 * Sets the affected component
124 *
125 * @param string $component Component key
126 * @return t3lib_log_Record
127 */
128 public function setComponent($component) {
129 $this->component = $component;
130 return $this;
131 }
132
133 /**
134 * Returns the component
135 *
136 * @return string Component key
137 */
138 public function getComponent() {
139 return $this->component;
140 }
141
142 /**
143 * Sets the the creation time
144 *
145 * @param float $created Creation time as float
146 * @return t3lib_log_Record
147 */
148 public function setCreated($created) {
149 $this->created = $created;
150 return $this;
151 }
152
153 /**
154 * Returns the creation time
155 *
156 * @return float Creation time as float
157 */
158 public function getCreated() {
159 return $this->created;
160 }
161
162 /**
163 * Sets the severity level
164 *
165 * @param integer $level Severity level
166 * @return t3lib_log_Record
167 * @throws RangeException if the given log level is invalid
168 * @see t3lib_log_Level
169 */
170 public function setLevel($level) {
171 t3lib_log_Level::validateLevel($level);
172 $this->level = $level;
173 return $this;
174 }
175
176 /**
177 * Returns the severity level
178 *
179 * @see t3lib_log_Level
180 * @return int Severity level
181 */
182 public function getLevel() {
183 return $this->level;
184 }
185
186 /**
187 * Sets log data array
188 *
189 * @param array $data
190 * @return t3lib_log_Record
191 */
192 public function setData($data) {
193 $this->data = $data;
194 return $this;
195 }
196
197 /**
198 * Returns the log data
199 *
200 * @return array
201 */
202 public function getData() {
203 return $this->data;
204 }
205
206 /**
207 * Adds additional log data to already existing data
208 * and overwrites previously data using the same array keys.
209 *
210 * @param array $data
211 * @return t3lib_log_Record
212 */
213 public function addData(array $data) {
214 $this->data = array_merge($this->data, $data);
215 return $this;
216 }
217
218 /**
219 * Sets the log message
220 *
221 * @param string $message Log message
222 * @return t3lib_log_Record
223 */
224 public function setMessage($message) {
225 $this->message = $message;
226 return $this;
227 }
228
229 /**
230 * Returns the log message
231 *
232 * @return string Log message
233 */
234 public function getMessage() {
235 return $this->message;
236 }
237
238 /**
239 * Sets the request ID
240 *
241 * @param string $requestId
242 * @return t3lib_log_Record
243 */
244 public function setRequestId($requestId) {
245 $this->requestId = $requestId;
246 return $this;
247 }
248
249 /**
250 * Returns the request ID
251 *
252 * @return string
253 */
254 public function getRequestId() {
255 return $this->requestId;
256 }
257
258 /**
259 * Convert record to string for simple output, like echo().
260 * Contents of data array is appended as JSON-encoded string
261 *
262 * @return string
263 */
264 public function __toString() {
265 $timestamp = date('r', (int) $this->created);
266 $levelName = t3lib_log_Level::getName($this->level);
267 $data = (!empty($this->data)) ? '- ' . json_encode($this->data) : '';
268
269 $logRecordString = sprintf(
270 '%s [%s] request="%s" component="%s": %s %s',
271 $timestamp,
272 $levelName,
273 $this->requestId,
274 $this->component,
275 $this->message,
276 $data
277 );
278
279 return $logRecordString;
280 }
281
282 /**
283 * Convert record to array
284 *
285 * @return array
286 */
287 public function toArray() {
288 return array(
289 'requestId' => $this->requestId,
290 'created' => $this->created,
291 'component' => $this->component,
292 'level' => $this->level,
293 'message' => $this->message,
294 'data' => $this->data
295 );
296 }
297
298 /**
299 * Checks whether an offset exists, required by ArrayAccess interface
300 *
301 * @param mixed $offset
302 * @return boolean
303 */
304 public function offsetExists($offset) {
305 $offsetExists = FALSE;
306
307 if (in_array($offset, $this->gettableProperties, TRUE) && isset($this->$offset)) {
308 $offsetExists = TRUE;
309 }
310
311 return $offsetExists;
312 }
313
314 /**
315 * Offset to retrieve, required by ArrayAccess interface
316 *
317 * @param mixed $offset
318 * @return mixed
319 */
320 public function offsetGet($offset) {
321 if (!in_array($offset, $this->gettableProperties, TRUE)) {
322 return NULL;
323 }
324
325 return $this->$offset;
326 }
327
328 /**
329 * Offset to set, required by ArrayAccess interface
330 *
331 * @param mixed $offset
332 * @param mixed $value
333 * @return void
334 */
335 public function offsetSet($offset, $value) {
336 if (in_array($offset, $this->settableProperties, TRUE)) {
337 $this->$offset = $offset;
338 }
339 }
340
341 /**
342 * Offset to unset, required by ArrayAccess interface
343 *
344 * @param mixed $offset
345 * @return void
346 */
347 public function offsetUnset($offset) {
348 if (in_array($offset, $this->settableProperties, TRUE)) {
349 unset($this->$offset);
350 }
351 }
352 }
353
354 ?>