first commit
This commit is contained in:
commit
1218dd1517
12716 files changed
+989233
No files matched your search
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace TYPO3\CMS\Adminpanel\Log;
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use TYPO3\CMS\Adminpanel\Utility\MemoryUtility;
|
||||
|
||||
/**
|
||||
* Doctrine SQL Logger implementation for recording queries for the admin panel
|
||||
*/
|
||||
class DoctrineSqlLogger implements SQLLogger, LoggerAwareInterface
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/**
|
||||
* Executed SQL queries.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $queries = [];
|
||||
|
||||
/**
|
||||
* If Debug Stack is enabled (log queries) or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $enabled = true;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
protected $start;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $currentQuery = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function startQuery($sql, array $params = null, array $types = null)
|
||||
{
|
||||
if ($this->enabled && MemoryUtility::isMemoryConsumptionTooHigh()) {
|
||||
$this->enabled = false;
|
||||
$this->logger->warning('SQL Logging consumed too much memory, aborted. Not all queries have been recorded.');
|
||||
}
|
||||
if ($this->enabled) {
|
||||
$this->start = microtime(true);
|
||||
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7);
|
||||
// remove this method
|
||||
array_shift($backtrace);
|
||||
// remove doctrine execute query
|
||||
array_shift($backtrace);
|
||||
// remove queryBuilder execute
|
||||
array_shift($backtrace);
|
||||
$this->queries[++$this->currentQuery] = [
|
||||
'sql' => $sql,
|
||||
'params' => $params,
|
||||
'types' => $types,
|
||||
'executionMS' => 0,
|
||||
'backtrace' => $backtrace
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function stopQuery()
|
||||
{
|
||||
if ($this->enabled) {
|
||||
$this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getQueries(): array
|
||||
{
|
||||
return $this->queries;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace TYPO3\CMS\Adminpanel\Log;
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
use TYPO3\CMS\Adminpanel\Utility\MemoryUtility;
|
||||
use TYPO3\CMS\Core\Log\LogLevel;
|
||||
use TYPO3\CMS\Core\Log\LogRecord;
|
||||
use TYPO3\CMS\Core\Log\Writer\AbstractWriter;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Log writer that writes the log records into a static public class variable
|
||||
* for InMemory processing
|
||||
*/
|
||||
class InMemoryLogWriter extends AbstractWriter
|
||||
{
|
||||
public static $log = [];
|
||||
|
||||
private static $memoryLock = false;
|
||||
|
||||
/**
|
||||
* Writes the log record
|
||||
*
|
||||
* @param LogRecord $record Log record
|
||||
* @return self
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function writeLog(LogRecord $record): self
|
||||
{
|
||||
// Guard: Locked Writer
|
||||
if (self::$memoryLock === true) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Guard: Memory Usage
|
||||
if (!self::$memoryLock && MemoryUtility::isMemoryConsumptionTooHigh()) {
|
||||
$this->lockWriter();
|
||||
return $this;
|
||||
}
|
||||
|
||||
self::$log[] = $record;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock writer and add a info message that there may potentially be more entries.
|
||||
*/
|
||||
protected function lockWriter(): void
|
||||
{
|
||||
self::$memoryLock = true;
|
||||
/** @var LogRecord $record */
|
||||
$record = GeneralUtility::makeInstance(
|
||||
LogRecord::class,
|
||||
'TYPO3.CMS.AdminPanel.Log.InMemoryLogWriter',
|
||||
LogLevel::INFO,
|
||||
'... Further log entries omitted, memory usage too high.'
|
||||
);
|
||||
self::$log[] = $record;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user