a4fc912fe3f4653bc9e4d85c66866e9c33c17793
[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 class LogRecord implements \ArrayAccess
21 {
22 /**
23 * Unique ID of the request
24 *
25 * @var string
26 */
27 protected $requestId = '';
28
29 /**
30 * Creation timestamp with microseconds
31 *
32 * @var float
33 */
34 protected $created = 0.0;
35
36 /**
37 * The component where the record was created
38 *
39 * @var string
40 */
41 protected $component = '';
42
43 /**
44 * Severity level
45 *
46 * @var int
47 */
48 protected $level = LogLevel::INFO;
49
50 /**
51 * Log message one-liner
52 *
53 * @var string
54 */
55 protected $message = '';
56
57 /**
58 * Additional log data
59 *
60 * @var array
61 */
62 protected $data = [];
63
64 /**
65 * Gettable properties for ArrayAccess
66 *
67 * @var array
68 */
69 private $gettableProperties = [
70 'requestId',
71 'created',
72 'component',
73 'level',
74 'message',
75 'data'
76 ];
77
78 /**
79 * Settable properties for ArrayAccess
80 *
81 * @var array
82 */
83 private $settableProperties = [
84 'level',
85 'message',
86 'data'
87 ];
88
89 /**
90 * Constructor.
91 *
92 * @param string $component Affected component
93 * @param int $level Severity level (see \TYPO3\CMS\Core\Log\Level)
94 * @param string $message Log message
95 * @param array $data Additional data
96 * @param string $requestId Unique ID of the request
97 */
98 public function __construct($component = '', $level, $message, array $data = [], string $requestId = '')
99 {
100 $this->setRequestId($requestId)
101 ->setCreated(microtime(true))
102 ->setComponent($component)
103 ->setLevel($level)
104 ->setMessage($message)
105 ->setData($data);
106 }
107
108 /**
109 * Sets the affected component
110 *
111 * @param string $component Component key
112 * @return \TYPO3\CMS\Core\Log\LogRecord
113 */
114 public function setComponent($component)
115 {
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 {
127 return $this->component;
128 }
129
130 /**
131 * Sets the the creation time
132 *
133 * @param float $created Creation time as float
134 * @return \TYPO3\CMS\Core\Log\LogRecord
135 */
136 public function setCreated($created)
137 {
138 $this->created = $created;
139 return $this;
140 }
141
142 /**
143 * Returns the creation time
144 *
145 * @return float Creation time as float
146 */
147 public function getCreated()
148 {
149 return $this->created;
150 }
151
152 /**
153 * Sets the severity level
154 *
155 * @param int $level Severity level
156 * @return \TYPO3\CMS\Core\Log\LogRecord
157 * @see \TYPO3\CMS\Core\Log\Level
158 */
159 public function setLevel($level)
160 {
161 LogLevel::validateLevel($level);
162 $this->level = $level;
163 return $this;
164 }
165
166 /**
167 * Returns the severity level
168 *
169 * @see \TYPO3\CMS\Core\Log\Level
170 * @return int Severity level
171 */
172 public function getLevel()
173 {
174 return $this->level;
175 }
176
177 /**
178 * Sets log data array
179 *
180 * @param array $data
181 * @return \TYPO3\CMS\Core\Log\LogRecord
182 */
183 public function setData($data)
184 {
185 $this->data = $data;
186 return $this;
187 }
188
189 /**
190 * Returns the log data
191 *
192 * @return array
193 */
194 public function getData()
195 {
196 return $this->data;
197 }
198
199 /**
200 * Adds additional log data to already existing data
201 * and overwrites previously data using the same array keys.
202 *
203 * @param array $data
204 * @return \TYPO3\CMS\Core\Log\LogRecord
205 */
206 public function addData(array $data)
207 {
208 $this->data = array_merge($this->data, $data);
209 return $this;
210 }
211
212 /**
213 * Sets the log message
214 *
215 * @param string|object $message Log message. Usually a string, or an object that can be casted to string (implements __toString())
216 * @return \TYPO3\CMS\Core\Log\LogRecord
217 */
218 public function setMessage($message)
219 {
220 $this->message = (string)$message;
221 return $this;
222 }
223
224 /**
225 * Returns the log message
226 *
227 * @return string Log message
228 */
229 public function getMessage()
230 {
231 return $this->message;
232 }
233
234 /**
235 * Sets the request ID
236 *
237 * @param string $requestId
238 * @return \TYPO3\CMS\Core\Log\LogRecord
239 */
240 public function setRequestId($requestId)
241 {
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 {
253 return $this->requestId;
254 }
255
256 /**
257 * Convert record to string for simple output, like echo().
258 * Contents of data array is appended as JSON-encoded string
259 *
260 * @return string
261 */
262 public function __toString()
263 {
264 $timestamp = date('r', (int)$this->created);
265 $levelName = LogLevel::getName($this->level);
266 $data = '';
267 if (!empty($this->data)) {
268 // According to PSR3 the exception-key may hold an \Exception
269 // Since json_encode() does not encode an exception, we run the _toString() here
270 if (isset($this->data['exception']) && $this->data['exception'] instanceof \Exception) {
271 $this->data['exception'] = (string)$this->data['exception'];
272 }
273 $data = '- ' . json_encode($this->data);
274 }
275 $logRecordString = sprintf(
276 '%s [%s] request="%s" component="%s": %s %s',
277 $timestamp,
278 $levelName,
279 $this->requestId,
280 $this->component,
281 $this->message,
282 $data
283 );
284 return $logRecordString;
285 }
286
287 /**
288 * Convert record to array
289 *
290 * @return array
291 */
292 public function toArray()
293 {
294 return [
295 'requestId' => $this->requestId,
296 'created' => $this->created,
297 'component' => $this->component,
298 'level' => $this->level,
299 'message' => $this->message,
300 'data' => $this->data
301 ];
302 }
303
304 /**
305 * Checks whether an offset exists, required by ArrayAccess interface
306 *
307 * @param mixed $offset
308 * @return bool
309 */
310 public function offsetExists($offset)
311 {
312 $offsetExists = false;
313 if (in_array($offset, $this->gettableProperties, true) && isset($this->{$offset})) {
314 $offsetExists = true;
315 }
316 return $offsetExists;
317 }
318
319 /**
320 * Offset to retrieve, required by ArrayAccess interface
321 *
322 * @param mixed $offset
323 * @return mixed
324 */
325 public function offsetGet($offset)
326 {
327 if (!in_array($offset, $this->gettableProperties, true)) {
328 return null;
329 }
330 return $this->{$offset};
331 }
332
333 /**
334 * Offset to set, required by ArrayAccess interface
335 *
336 * @param mixed $offset
337 * @param mixed $value
338 */
339 public function offsetSet($offset, $value)
340 {
341 if (in_array($offset, $this->settableProperties, true)) {
342 $this->{$offset} = $offset;
343 }
344 }
345
346 /**
347 * Offset to unset, required by ArrayAccess interface
348 *
349 * @param mixed $offset
350 */
351 public function offsetUnset($offset)
352 {
353 if (in_array($offset, $this->settableProperties, true)) {
354 unset($this->{$offset});
355 }
356 }
357 }