‪TYPO3CMS  9.5
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 
20 class ‪LogRecord implements \ArrayAccess
21 {
27  protected ‪$requestId = '';
28 
34  protected ‪$created = 0.0;
35 
41  protected ‪$component = '';
42 
48  protected ‪$level = ‪LogLevel::INFO;
49 
55  protected ‪$message = '';
56 
62  protected ‪$data = [];
63 
69  private ‪$gettableProperties = [
70  'requestId',
71  'created',
72  'component',
73  'level',
74  'message',
75  'data'
76  ];
77 
83  private ‪$settableProperties = [
84  'level',
85  'message',
86  'data'
87  ];
88 
98  public function ‪__construct(‪$component = '', ‪$level, ‪$message, array ‪$data = [], string ‪$requestId = '')
99  {
101  ->‪setCreated(microtime(true))
105  ->‪setData(‪$data);
106  }
107 
114  public function ‪setComponent(‪$component)
115  {
116  $this->component = ‪$component;
117  return $this;
118  }
119 
125  public function ‪getComponent()
126  {
127  return ‪$this->component;
128  }
129 
136  public function ‪setCreated(‪$created)
137  {
138  $this->created = ‪$created;
139  return $this;
140  }
141 
147  public function ‪getCreated()
148  {
149  return ‪$this->created;
150  }
151 
159  public function ‪setLevel(‪$level)
160  {
162  $this->level = ‪$level;
163  return $this;
164  }
165 
172  public function ‪getLevel()
173  {
174  return ‪$this->level;
175  }
176 
183  public function ‪setData(‪$data)
184  {
185  $this->data = ‪$data;
186  return $this;
187  }
188 
194  public function ‪getData()
195  {
196  return ‪$this->data;
197  }
198 
206  public function ‪addData(array ‪$data)
207  {
208  $this->data = array_merge($this->data, ‪$data);
209  return $this;
210  }
211 
218  public function ‪setMessage(‪$message)
219  {
220  $this->message = (string)‪$message;
221  return $this;
222  }
223 
229  public function ‪getMessage()
230  {
231  return ‪$this->message;
232  }
233 
240  public function ‪setRequestId(‪$requestId)
241  {
242  $this->requestId = ‪$requestId;
243  return $this;
244  }
245 
251  public function ‪getRequestId()
252  {
253  return ‪$this->requestId;
254  }
255 
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 
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 
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 
325  public function ‪offsetGet($offset)
326  {
327  if (!in_array($offset, $this->gettableProperties, true)) {
328  return null;
329  }
330  return $this->{$offset};
331  }
332 
339  public function ‪offsetSet($offset, $value)
340  {
341  if (in_array($offset, $this->settableProperties, true)) {
342  $this->{$offset} = $offset;
343  }
344  }
345 
351  public function ‪offsetUnset($offset)
352  {
353  if (in_array($offset, $this->settableProperties, true)) {
354  unset($this->{$offset});
355  }
356  }
357 }
‪TYPO3\CMS\Core\Log\LogRecord\setLevel
‪TYPO3 CMS Core Log LogRecord setLevel($level)
Definition: LogRecord.php:151
‪TYPO3\CMS\Core\Log\LogRecord\offsetGet
‪mixed offsetGet($offset)
Definition: LogRecord.php:317
‪TYPO3\CMS\Core\Log\LogRecord\$settableProperties
‪array $settableProperties
Definition: LogRecord.php:75
‪TYPO3\CMS\Core\Log\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Log\LogRecord\toArray
‪array toArray()
Definition: LogRecord.php:284
‪TYPO3\CMS\Core\Log\LogRecord\$requestId
‪string $requestId
Definition: LogRecord.php:26
‪TYPO3\CMS\Core\Log\LogRecord\addData
‪TYPO3 CMS Core Log LogRecord addData(array $data)
Definition: LogRecord.php:198
‪TYPO3\CMS\Core\Log\LogRecord\setRequestId
‪TYPO3 CMS Core Log LogRecord setRequestId($requestId)
Definition: LogRecord.php:232
‪TYPO3\CMS\Core\Log\LogRecord\$level
‪int $level
Definition: LogRecord.php:44
‪TYPO3\CMS\Core\Log\LogRecord\__construct
‪__construct($component='', $level, $message, array $data=[], string $requestId='')
Definition: LogRecord.php:90
‪TYPO3\CMS\Core\Log\LogLevel\INFO
‪const INFO
Definition: LogLevel.php:85
‪TYPO3\CMS\Core\Log\LogRecord\$message
‪string $message
Definition: LogRecord.php:50
‪TYPO3\CMS\Core\Log\LogRecord\$created
‪float $created
Definition: LogRecord.php:32
‪TYPO3\CMS\Core\Log\LogRecord\setCreated
‪TYPO3 CMS Core Log LogRecord setCreated($created)
Definition: LogRecord.php:128
‪TYPO3\CMS\Core\Log
‪TYPO3\CMS\Core\Log\LogRecord\setComponent
‪TYPO3 CMS Core Log LogRecord setComponent($component)
Definition: LogRecord.php:106
‪TYPO3\CMS\Core\Log\LogRecord\setData
‪TYPO3 CMS Core Log LogRecord setData($data)
Definition: LogRecord.php:175
‪TYPO3\CMS\Core\Log\LogRecord\$component
‪string $component
Definition: LogRecord.php:38
‪TYPO3\CMS\Core\Log\LogRecord\getData
‪array getData()
Definition: LogRecord.php:186
‪TYPO3\CMS\Core\Log\LogRecord\offsetSet
‪offsetSet($offset, $value)
Definition: LogRecord.php:331
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:21
‪TYPO3\CMS\Core\Log\LogRecord\offsetExists
‪bool offsetExists($offset)
Definition: LogRecord.php:302
‪TYPO3\CMS\Core\Log\LogRecord\getMessage
‪string getMessage()
Definition: LogRecord.php:221
‪TYPO3\CMS\Core\Log\LogRecord\getComponent
‪string getComponent()
Definition: LogRecord.php:117
‪TYPO3\CMS\Core\Log\LogRecord\$gettableProperties
‪array $gettableProperties
Definition: LogRecord.php:62
‪TYPO3\CMS\Core\Log\LogRecord\$data
‪array $data
Definition: LogRecord.php:56
‪TYPO3\CMS\Core\Log\LogRecord\setMessage
‪TYPO3 CMS Core Log LogRecord setMessage($message)
Definition: LogRecord.php:210
‪TYPO3\CMS\Core\Log\LogLevel\getName
‪static string getName($level)
Definition: LogLevel.php:117
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel($level)
Definition: LogLevel.php:141
‪TYPO3\CMS\Core\Log\LogRecord\getCreated
‪float getCreated()
Definition: LogRecord.php:139
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪int getLevel()
Definition: LogRecord.php:164
‪TYPO3\CMS\Core\Log\LogRecord\offsetUnset
‪offsetUnset($offset)
Definition: LogRecord.php:343
‪TYPO3\CMS\Core\Log\LogRecord\__toString
‪string __toString()
Definition: LogRecord.php:254
‪TYPO3\CMS\Core\Log\LogRecord\getRequestId
‪string getRequestId()
Definition: LogRecord.php:243