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