TYPO3 CMS  TYPO3_7-6
LogRecord.php
Go to the documentation of this file.
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 
18 
22 class LogRecord implements \ArrayAccess
23 {
29  protected $requestId = '';
30 
36  protected $created = 0.0;
37 
43  protected $component = '';
44 
50  protected $level = LogLevel::INFO;
51 
57  protected $message = '';
58 
64  protected $data = [];
65 
71  private $gettableProperties = [
72  'requestId',
73  'created',
74  'component',
75  'level',
76  'message',
77  'data'
78  ];
79 
85  private $settableProperties = [
86  'level',
87  'message',
88  'data'
89  ];
90 
99  public function __construct($component = '', $level, $message, array $data = [])
100  {
102  ->setCreated(microtime(true))
103  ->setComponent($component)
104  ->setLevel($level)
105  ->setMessage($message)
106  ->setData($data);
107  }
108 
115  public function setComponent($component)
116  {
117  $this->component = $component;
118  return $this;
119  }
120 
126  public function getComponent()
127  {
128  return $this->component;
129  }
130 
137  public function setCreated($created)
138  {
139  $this->created = $created;
140  return $this;
141  }
142 
148  public function getCreated()
149  {
150  return $this->created;
151  }
152 
160  public function setLevel($level)
161  {
163  $this->level = $level;
164  return $this;
165  }
166 
173  public function getLevel()
174  {
175  return $this->level;
176  }
177 
184  public function setData($data)
185  {
186  $this->data = $data;
187  return $this;
188  }
189 
195  public function getData()
196  {
197  return $this->data;
198  }
199 
207  public function addData(array $data)
208  {
209  $this->data = array_merge($this->data, $data);
210  return $this;
211  }
212 
219  public function setMessage($message)
220  {
221  $this->message = (string)$message;
222  return $this;
223  }
224 
230  public function getMessage()
231  {
232  return $this->message;
233  }
234 
241  public function setRequestId($requestId)
242  {
243  $this->requestId = $requestId;
244  return $this;
245  }
246 
252  public function getRequestId()
253  {
254  return $this->requestId;
255  }
256 
263  public function __toString()
264  {
265  $timestamp = date('r', (int)$this->created);
266  $levelName = LogLevel::getName($this->level);
267  $data = '';
268  if (!empty($this->data)) {
269  // According to PSR3 the exception-key may hold an \Exception
270  // Since json_encode() does not encode an exception, we run the _toString() here
271  if (isset($this->data['exception']) && $this->data['exception'] instanceof \Exception) {
272  $this->data['exception'] = (string)$this->data['exception'];
273  }
274  $data = '- ' . json_encode($this->data);
275  }
276  $logRecordString = sprintf(
277  '%s [%s] request="%s" component="%s": %s %s',
278  $timestamp,
279  $levelName,
280  $this->requestId,
281  $this->component,
282  $this->message,
283  $data
284  );
285  return $logRecordString;
286  }
287 
293  public function toArray()
294  {
295  return [
296  'requestId' => $this->requestId,
297  'created' => $this->created,
298  'component' => $this->component,
299  'level' => $this->level,
300  'message' => $this->message,
301  'data' => $this->data
302  ];
303  }
304 
311  public function offsetExists($offset)
312  {
313  $offsetExists = false;
314  if (in_array($offset, $this->gettableProperties, true) && isset($this->{$offset})) {
315  $offsetExists = true;
316  }
317  return $offsetExists;
318  }
319 
326  public function offsetGet($offset)
327  {
328  if (!in_array($offset, $this->gettableProperties, true)) {
329  return null;
330  }
331  return $this->{$offset};
332  }
333 
341  public function offsetSet($offset, $value)
342  {
343  if (in_array($offset, $this->settableProperties, true)) {
344  $this->{$offset} = $offset;
345  }
346  }
347 
354  public function offsetUnset($offset)
355  {
356  if (in_array($offset, $this->settableProperties, true)) {
357  unset($this->{$offset});
358  }
359  }
360 }
static validateLevel($level)
Definition: LogLevel.php:143
__construct($component='', $level, $message, array $data=[])
Definition: LogRecord.php:99
offsetSet($offset, $value)
Definition: LogRecord.php:341
static getName($level)
Definition: LogLevel.php:118