‪TYPO3CMS  ‪main
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 
23 class ‪LogRecord implements \ArrayAccess
24 {
28  protected string ‪$requestId = '';
29 
33  protected float ‪$created = 0.0;
34 
38  protected string ‪$component = '';
39 
43  protected string ‪$level = \Psr\Log\LogLevel::INFO;
44 
48  protected string ‪$message = '';
49 
53  protected array ‪$data = [];
54 
58  private array ‪$gettableProperties = [
59  'requestId',
60  'created',
61  'component',
62  'level',
63  'message',
64  'data',
65  ];
66 
70  private array ‪$settableProperties = [
71  'level',
72  'message',
73  'data',
74  ];
75 
85  public function ‪__construct(string ‪$component, string ‪$level, string ‪$message, array ‪$data = [], string ‪$requestId = '')
86  {
87  $this->‪setRequestId($requestId)
88  ->‪setCreated(microtime(true))
93  }
94 
102  {
103  $this->component = ‪$component;
104  return $this;
105  }
106 
112  public function ‪getComponent()
113  {
114  return ‪$this->component;
115  }
116 
123  public function ‪setCreated(‪$created)
124  {
125  $this->created = ‪$created;
126  return $this;
127  }
128 
134  public function ‪getCreated()
135  {
136  return ‪$this->created;
137  }
138 
146  public function ‪setLevel(string ‪$level)
147  {
149  $this->level = ‪$level;
150  return $this;
151  }
152 
159  public function ‪getLevel(): string
160  {
161  return ‪$this->level;
162  }
163 
170  public function ‪setData(‪$data)
171  {
172  $this->data = ‪$data;
173  return $this;
174  }
175 
181  public function ‪getData()
182  {
183  return ‪$this->data;
184  }
185 
192  public function ‪addData(array ‪$data)
193  {
194  $this->data = array_merge($this->data, ‪$data);
195  return $this;
196  }
197 
204  public function ‪setMessage(‪$message)
205  {
206  $this->message = (string)‪$message;
207  return $this;
208  }
209 
215  public function ‪getMessage()
216  {
217  return ‪$this->message;
218  }
219 
227  {
228  $this->requestId = ‪$requestId;
229  return $this;
230  }
231 
237  public function ‪getRequestId()
238  {
239  return ‪$this->requestId;
240  }
241 
246  public function ‪__toString(): string
247  {
248  $timestamp = date('r', (int)$this->created);
249  $levelName = strtoupper($this->level);
250  ‪$data = '';
251  if (!empty($this->data)) {
252  // According to PSR3 the exception-key may hold an \Exception
253  // Since json_encode() does not encode an exception, we run the _toString() here
254  if (isset($this->data['exception']) && $this->data['exception'] instanceof \‪Exception) {
255  $this->data['exception'] = (string)$this->data['exception'];
256  }
257  ‪$data = '- ' . json_encode($this->data);
258  }
259  $logRecordString = sprintf(
260  '%s [%s] request="%s" component="%s": %s %s',
261  $timestamp,
262  $levelName,
263  $this->requestId,
264  $this->component,
265  $this->message,
266  ‪$data
267  );
268  return $logRecordString;
269  }
270 
276  public function ‪toArray()
277  {
278  return [
279  'requestId' => ‪$this->requestId,
280  'created' => ‪$this->created,
281  'component' => ‪$this->component,
282  'level' => ‪$this->level,
283  'message' => ‪$this->message,
284  'data' => ‪$this->data,
285  ];
286  }
287 
293  public function ‪offsetExists($offset): bool
294  {
295  $offsetExists = false;
296  if (in_array($offset, $this->gettableProperties, true) && isset($this->{$offset})) {
297  $offsetExists = true;
298  }
299  return $offsetExists;
300  }
301 
307  public function ‪offsetGet($offset): mixed
308  {
309  if (!in_array($offset, $this->gettableProperties, true)) {
310  return null;
311  }
312  return $this->{$offset};
313  }
314 
321  public function ‪offsetSet($offset, $value): void
322  {
323  if (in_array($offset, $this->settableProperties, true)) {
324  $this->{$offset} = $offset;
325  }
326  }
327 
333  public function ‪offsetUnset($offset): void
334  {
335  if (in_array($offset, $this->settableProperties, true)) {
336  unset($this->{$offset});
337  }
338  }
339 }
‪TYPO3\CMS\Core\Log\LogRecord\$settableProperties
‪array $settableProperties
Definition: LogRecord.php:70
‪TYPO3\CMS\Core\Log\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Log\LogRecord\toArray
‪array toArray()
Definition: LogRecord.php:276
‪TYPO3\CMS\Core\Log\LogRecord\$requestId
‪string $requestId
Definition: LogRecord.php:28
‪TYPO3\CMS\Core\Log\LogRecord\setLevel
‪LogRecord setLevel(string $level)
Definition: LogRecord.php:146
‪TYPO3\CMS\Core\Log\LogRecord\__construct
‪__construct(string $component, string $level, string $message, array $data=[], string $requestId='')
Definition: LogRecord.php:85
‪TYPO3\CMS\Core\Log\LogRecord\offsetExists
‪offsetExists($offset)
Definition: LogRecord.php:293
‪TYPO3\CMS\Core\Log\LogRecord\$message
‪string $message
Definition: LogRecord.php:48
‪TYPO3\CMS\Core\Log\LogRecord\setComponent
‪LogRecord setComponent($component)
Definition: LogRecord.php:101
‪TYPO3\CMS\Core\Log\LogRecord\$created
‪float $created
Definition: LogRecord.php:33
‪TYPO3\CMS\Core\Log\LogRecord\__toString
‪__toString()
Definition: LogRecord.php:246
‪TYPO3\CMS\Core\Log\LogRecord\offsetGet
‪offsetGet($offset)
Definition: LogRecord.php:307
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static normalizeLevel($level)
Definition: LogLevel.php:90
‪TYPO3\CMS\Core\Log
Definition: Channel.php:18
‪TYPO3\CMS\Core\Log\LogLevel\validateLevel
‪static validateLevel(int $level)
Definition: LogLevel.php:78
‪TYPO3\CMS\Core\Log\LogRecord\$component
‪string $component
Definition: LogRecord.php:38
‪TYPO3\CMS\Core\Log\LogRecord\getData
‪array getData()
Definition: LogRecord.php:181
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪string getLevel()
Definition: LogRecord.php:159
‪TYPO3\CMS\Core\Log\LogRecord\offsetSet
‪offsetSet($offset, $value)
Definition: LogRecord.php:321
‪TYPO3\CMS\Core\Log\LogRecord\setRequestId
‪LogRecord setRequestId($requestId)
Definition: LogRecord.php:226
‪TYPO3\CMS\Core\Log\LogRecord\setCreated
‪LogRecord setCreated($created)
Definition: LogRecord.php:123
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:24
‪TYPO3\CMS\Core\Log\LogRecord\getMessage
‪string getMessage()
Definition: LogRecord.php:215
‪TYPO3\CMS\Core\Log\LogRecord\getComponent
‪string getComponent()
Definition: LogRecord.php:112
‪TYPO3\CMS\Core\Log\LogRecord\$level
‪string $level
Definition: LogRecord.php:43
‪TYPO3\CMS\Core\Log\LogRecord\setData
‪LogRecord setData($data)
Definition: LogRecord.php:170
‪TYPO3\CMS\Core\Log\LogRecord\setMessage
‪LogRecord setMessage($message)
Definition: LogRecord.php:204
‪TYPO3\CMS\Core\Log\LogRecord\$gettableProperties
‪array $gettableProperties
Definition: LogRecord.php:58
‪TYPO3\CMS\Core\Log\LogRecord\$data
‪array $data
Definition: LogRecord.php:53
‪TYPO3\CMS\Core\Log\LogRecord\addData
‪LogRecord addData(array $data)
Definition: LogRecord.php:192
‪TYPO3\CMS\Core\Log\LogRecord\getCreated
‪float getCreated()
Definition: LogRecord.php:134
‪TYPO3\CMS\Core\Log\LogRecord\offsetUnset
‪offsetUnset($offset)
Definition: LogRecord.php:333
‪TYPO3\CMS\Core\Log\LogRecord\getRequestId
‪string getRequestId()
Definition: LogRecord.php:237