‪TYPO3CMS  ‪main
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 ConnectionPool usage prior boot completion (see #96291).
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\LogLevel\normalizeLevel
‪static normalizeLevel($level)
Definition: LogLevel.php:90
‪TYPO3\CMS\Core\Log\Writer\DatabaseWriter\setLogTable
‪TYPO3 CMS Core Log Writer AbstractWriter setLogTable($tableName)
Definition: DatabaseWriter.php:40
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:24
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪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\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24