TYPO3 CMS  TYPO3_6-2
LogRecord.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Log;
3 
18 
25 class LogRecord implements \ArrayAccess {
26 
32  protected $requestId = '';
33 
39  protected $created = 0.0;
40 
46  protected $component = '';
47 
54 
60  protected $message = '';
61 
67  protected $data = array();
68 
74  private $gettableProperties = array(
75  'requestId',
76  'created',
77  'component',
78  'level',
79  'message',
80  'data'
81  );
82 
88  private $settableProperties = array(
89  'level',
90  'message',
91  'data'
92  );
93 
102  public function __construct($component = '', $level, $message, array $data = array()) {
104  ->setCreated(microtime(TRUE))
105  ->setComponent($component)
106  ->setLevel($level)
107  ->setMessage($message)
108  ->setData($data);
109  }
110 
117  public function setComponent($component) {
118  $this->component = $component;
119  return $this;
120  }
121 
127  public function getComponent() {
128  return $this->component;
129  }
130 
137  public function setCreated($created) {
138  $this->created = $created;
139  return $this;
140  }
141 
147  public function getCreated() {
148  return $this->created;
149  }
150 
159  public function setLevel($level) {
161  $this->level = $level;
162  return $this;
163  }
164 
171  public function getLevel() {
172  return $this->level;
173  }
174 
181  public function setData($data) {
182  $this->data = $data;
183  return $this;
184  }
185 
191  public function getData() {
192  return $this->data;
193  }
194 
202  public function addData(array $data) {
203  $this->data = array_merge($this->data, $data);
204  return $this;
205  }
206 
213  public function setMessage($message) {
214  $this->message = (string)$message;
215  return $this;
216  }
217 
223  public function getMessage() {
224  return $this->message;
225  }
226 
233  public function setRequestId($requestId) {
234  $this->requestId = $requestId;
235  return $this;
236  }
237 
243  public function getRequestId() {
244  return $this->requestId;
245  }
246 
253  public function __toString() {
254  $timestamp = date('r', (int)$this->created);
255  $levelName = \TYPO3\CMS\Core\Log\LogLevel::getName($this->level);
256  $data = '';
257  if (!empty($this->data)) {
258  // According to PSR3 the exception-key may hold an \Exception
259  // Since json_encode() does not encode an exception, we run the _toString() here
260  if (isset($this->data['exception']) && $this->data['exception'] instanceof \Exception) {
261  $this->data['exception'] = (string)$this->data['exception'];
262  }
263  $data = '- ' . json_encode($this->data);
264  }
265  $logRecordString = sprintf(
266  '%s [%s] request="%s" component="%s": %s %s',
267  $timestamp,
268  $levelName,
269  $this->requestId,
270  $this->component,
271  $this->message,
272  $data
273  );
274  return $logRecordString;
275  }
276 
282  public function toArray() {
283  return array(
284  'requestId' => $this->requestId,
285  'created' => $this->created,
286  'component' => $this->component,
287  'level' => $this->level,
288  'message' => $this->message,
289  'data' => $this->data
290  );
291  }
292 
299  public function offsetExists($offset) {
300  $offsetExists = FALSE;
301  if (in_array($offset, $this->gettableProperties, TRUE) && isset($this->{$offset})) {
302  $offsetExists = TRUE;
303  }
304  return $offsetExists;
305  }
306 
313  public function offsetGet($offset) {
314  if (!in_array($offset, $this->gettableProperties, TRUE)) {
315  return NULL;
316  }
317  return $this->{$offset};
318  }
319 
327  public function offsetSet($offset, $value) {
328  if (in_array($offset, $this->settableProperties, TRUE)) {
329  $this->{$offset} = $offset;
330  }
331  }
332 
339  public function offsetUnset($offset) {
340  if (in_array($offset, $this->settableProperties, TRUE)) {
341  unset($this->{$offset});
342  }
343  }
344 
345 }
static validateLevel($level)
Definition: LogLevel.php:134
offsetSet($offset, $value)
Definition: LogRecord.php:327
__construct($component='', $level, $message, array $data=array())
Definition: LogRecord.php:102
static getName($level)
Definition: LogLevel.php:111