‪TYPO3CMS  10.4
LogRecord.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Log;
17 
21 class ‪LogRecord implements \ArrayAccess
22 {
28  protected ‪$requestId = '';
29 
35  protected ‪$created = 0.0;
36 
42  protected ‪$component = '';
43 
49  protected ‪$level = LogLevel::INFO;
50 
56  protected ‪$message = '';
57 
63  protected ‪$data = [];
64 
70  private ‪$gettableProperties = [
71  'requestId',
72  'created',
73  'component',
74  'level',
75  'message',
76  'data'
77  ];
78 
84  private ‪$settableProperties = [
85  'level',
86  'message',
87  'data'
88  ];
89 
99  public function ‪__construct(string ‪$component, string ‪$level, string ‪$message, array ‪$data = [], string ‪$requestId = '')
100  {
102  ->‪setCreated(microtime(true))
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(string ‪$level)
161  {
163  $this->level = ‪$level;
164  return $this;
165  }
166 
173  public function ‪getLevel(): string
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 = strtoupper($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 
340  public function ‪offsetSet($offset, $value)
341  {
342  if (in_array($offset, $this->settableProperties, true)) {
343  $this->{$offset} = $offset;
344  }
345  }
346 
352  public function ‪offsetUnset($offset)
353  {
354  if (in_array($offset, $this->settableProperties, true)) {
355  unset($this->{$offset});
356  }
357  }
358 }
‪TYPO3\CMS\Core\Log\LogRecord\offsetGet
‪mixed offsetGet($offset)
Definition: LogRecord.php:318
‪TYPO3\CMS\Core\Log\LogRecord\$settableProperties
‪array $settableProperties
Definition: LogRecord.php:76
‪TYPO3\CMS\Core\Log\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Log\LogRecord\toArray
‪array toArray()
Definition: LogRecord.php:285
‪TYPO3\CMS\Core\Log\LogRecord\$requestId
‪string $requestId
Definition: LogRecord.php:27
‪TYPO3\CMS\Core\Log\LogRecord\setLevel
‪LogRecord setLevel(string $level)
Definition: LogRecord.php:152
‪TYPO3\CMS\Core\Log\LogRecord\__construct
‪__construct(string $component, string $level, string $message, array $data=[], string $requestId='')
Definition: LogRecord.php:91
‪TYPO3\CMS\Core\Log\LogRecord\$message
‪string $message
Definition: LogRecord.php:51
‪TYPO3\CMS\Core\Log\LogRecord\setComponent
‪LogRecord setComponent($component)
Definition: LogRecord.php:107
‪TYPO3\CMS\Core\Log\LogRecord\$created
‪float $created
Definition: LogRecord.php:33
‪TYPO3\CMS\Core\Log
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel(int $level)
Definition: LogLevel.php:81
‪TYPO3\CMS\Core\Log\LogRecord\$component
‪string $component
Definition: LogRecord.php:39
‪TYPO3\CMS\Core\Log\LogRecord\getData
‪array getData()
Definition: LogRecord.php:187
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪string getLevel()
Definition: LogRecord.php:165
‪TYPO3\CMS\Core\Log\LogRecord\offsetSet
‪offsetSet($offset, $value)
Definition: LogRecord.php:332
‪TYPO3\CMS\Core\Log\LogRecord\setRequestId
‪LogRecord setRequestId($requestId)
Definition: LogRecord.php:233
‪TYPO3\CMS\Core\Log\LogRecord\setCreated
‪LogRecord setCreated($created)
Definition: LogRecord.php:129
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:22
‪TYPO3\CMS\Core\Log\LogRecord\offsetExists
‪bool offsetExists($offset)
Definition: LogRecord.php:303
‪TYPO3\CMS\Core\Log\LogRecord\getMessage
‪string getMessage()
Definition: LogRecord.php:222
‪TYPO3\CMS\Core\Log\LogRecord\getComponent
‪string getComponent()
Definition: LogRecord.php:118
‪TYPO3\CMS\Core\Log\LogRecord\$level
‪string $level
Definition: LogRecord.php:45
‪TYPO3\CMS\Core\Log\LogRecord\setData
‪LogRecord setData($data)
Definition: LogRecord.php:176
‪TYPO3\CMS\Core\Log\LogRecord\setMessage
‪LogRecord setMessage($message)
Definition: LogRecord.php:211
‪TYPO3\CMS\Core\Log\LogRecord\$gettableProperties
‪array $gettableProperties
Definition: LogRecord.php:63
‪TYPO3\CMS\Core\Log\LogRecord\$data
‪array $data
Definition: LogRecord.php:57
‪TYPO3\CMS\Core\Log\LogRecord\addData
‪LogRecord addData(array $data)
Definition: LogRecord.php:199
‪TYPO3\CMS\Core\Log\LogRecord\getCreated
‪float getCreated()
Definition: LogRecord.php:140
‪TYPO3\CMS\Core\Log\LogRecord\offsetUnset
‪offsetUnset($offset)
Definition: LogRecord.php:344
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int normalizeLevel($level)
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\LogRecord\__toString
‪string __toString()
Definition: LogRecord.php:255
‪TYPO3\CMS\Core\Log\LogRecord\getRequestId
‪string getRequestId()
Definition: LogRecord.php:244