‪TYPO3CMS  11.5
DatabaseWriter.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 
17 
22 
27 {
33  protected ‪$logTable = 'sys_log';
34 
41  public function ‪setLogTable($tableName)
42  {
43  $this->logTable = $tableName;
44  return $this;
45  }
46 
52  public function ‪getLogTable()
53  {
54  return ‪$this->logTable;
55  }
56 
63  public function ‪writeLog(‪LogRecord $record)
64  {
65  try {
66  // Avoid database writes until boot has been completed.
67  if (!GeneralUtility::getContainer()->get('boot.state')->complete) {
68  return $this;
69  }
70  } catch (\LogicException $e) {
71  // LogicException will be thrown if the container isn't available yet.
72  return $this;
73  }
74 
75  $data = '';
76  $context = $record->‪getData();
77  if (!empty($context)) {
78  // Fold an exception into the message, and string-ify it into context so it can be jsonified.
79  if (isset($context['exception']) && $context['exception'] instanceof \Throwable) {
80  $context['exception'] = (string)$context['exception'];
81  }
82  $data = '- ' . json_encode($context);
83  }
84 
85  $fieldValues = [
86  'request_id' => $record->‪getRequestId(),
87  'time_micro' => $record->‪getCreated(),
88  'component' => $record->‪getComponent(),
89  'level' => ‪LogLevel::normalizeLevel($record->‪getLevel()),
90  'message' => $record->‪getMessage(),
91  'data' => $data,
92  ];
93 
94  GeneralUtility::makeInstance(ConnectionPool::class)
95  ->getConnectionForTable($this->logTable)
96  ->insert($this->logTable, $fieldValues);
97 
98  return $this;
99  }
100 }
‪TYPO3\CMS\Core\Log\Writer\DatabaseWriter
Definition: DatabaseWriter.php:27
‪TYPO3\CMS\Core\Log\Writer\DatabaseWriter\$logTable
‪string $logTable
Definition: DatabaseWriter.php:32
‪TYPO3\CMS\Core\Log\Writer\DatabaseWriter\setLogTable
‪TYPO3 CMS Core Log Writer AbstractWriter setLogTable($tableName)
Definition: DatabaseWriter.php:40
‪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
Definition: LogRecord.php:22
‪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\Writer\DatabaseWriter\getLogTable
‪string getLogTable()
Definition: DatabaseWriter.php:51
‪TYPO3\CMS\Core\Log\Writer
Definition: AbstractWriter.php:16
‪TYPO3\CMS\Core\Log\Writer\DatabaseWriter\writeLog
‪TYPO3 CMS Core Log Writer WriterInterface writeLog(LogRecord $record)
Definition: DatabaseWriter.php:62
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter
Definition: AbstractWriter.php:25
‪TYPO3\CMS\Core\Log\LogRecord\getCreated
‪float getCreated()
Definition: LogRecord.php:140
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static int normalizeLevel($level)
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\LogRecord\getRequestId
‪string getRequestId()
Definition: LogRecord.php:244
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24