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